1犀盟、拍照相片存儲(chǔ)的臨時(shí)文件
File savePath=new File(Environment.getExternalStorageDirectory(),"/temp/temp.jpg");
2、開啟系統(tǒng)相機(jī)
Intent cameraIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri imageUri = Uri.fromFile(savePath);
// 指定照片保存路徑(SD卡),temp.jpg為一個(gè)臨時(shí)文件宏赘,每次拍照后這個(gè)圖片都會(huì)被替換
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, Constant.PHOTO_GRAPH);
3、當(dāng)拍照完畢后系統(tǒng)會(huì)調(diào)用onActivityResult
@Override
public void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case Constant.PHOTO_GRAPH:
if(RESULT_CANCELED!= resultCode) {
//根據(jù)savePath.getAbsolutePath()絕對(duì)路徑獲取當(dāng)前拍攝的圖片去做相關(guān)邏輯操作
//保存圖片到系統(tǒng)內(nèi)容提供器
url[0] = MediaStore.Images.Media.insertImage(appContext.getContentResolver(),
picPath,null,null);
}
}
break;
4蝌数、創(chuàng)建本地圖片存放路徑
private String createPicPath(String path) {
File dirFile =new File(Environment.getExternalStorageDirectory().getPath()
+ path);
if(!dirFile.exists()) {
dirFile.mkdirs();
}
// 格式化時(shí)間
String dataTake =new SimpleDateFormat(
"yyyyMMddHHmmssSSS").format(newDate());
return dirFile.getPath() + File.separator+ dataTake +".jpg";
}
4艰管、保存本地圖片
public boolean saveImage(String path, String spath) {
BufferedOutputStream bos =null;
try{
bos =new BufferedOutputStream(
new FileOutputStream(spath,false));
Bitmap photo = getBitmap(path);
photo.compress(Bitmap.CompressFormat.JPEG,80, bos);
bos.flush();
}catch(Exception e) {
e.printStackTrace();
return false;
}finally{
try{
if(bos !=null)
bos.close();
}catch(IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
5、縮放圖片狈醉,壓縮質(zhì)量
public boolean saveImage(String path, String spath) {
BufferedOutputStream bos =null;
try{
bos =new BufferedOutputStream(
newFileOutputStream(spath,false));
Bitmap photo = getBitmap(path);
photo.compress(Bitmap.CompressFormat.JPEG,80, bos);
bos.flush();
}catch(Exception e) {
e.printStackTrace();
return false;
}finally{
try{
if(bos !=null)
bos.close();
}catch(IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
6廉油、壓縮比例
public Bitmap getBitmap(String filePath) {
final BitmapFactory.Options options =newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize=calculateInSampleSize1(options,480,800);
options.inJustDecodeBounds=false;
Bitmap bitmap = toRotationBitmap(BitmapFactory.decodeFile(filePath, options), getExifOrientation(filePath));
returnbitmap;
}
7、旋轉(zhuǎn)圖片
private Bitmap toRotationBitmap(Bitmap bitmap,final introtation) {
if(rotation ==0)
return bitmap;
Matrix matrix =new Matrix();
matrix.postRotate(rotation);
int height = bitmap.getHeight();
int width = bitmap.getWidth();
float scale =1f;
// 防止內(nèi)存溢出苗傅,先添加一個(gè)基本判斷抒线,根據(jù)測(cè)試情況更改
while(height >2200) {
height /=2;
width /=2;
scale *=0.5f;
}
matrix.postScale(scale, scale);
int neHeight = height;
int neWidth = width;
if(rotation ==90|| rotation ==270) {
neHeight = width;
neWidth = height;
}
Bitmap canvasBitmap = Bitmap.createBitmap(neWidth, neHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas =newCanvas(canvasBitmap);
float transX = height;
float transY =0;
if(rotation ==90) {
transX = height;
transY =0;
}else if(rotation ==180) {
transX = width;
transY = height;
}else if(rotation ==270) {
transX =0;
transY = width;
}else{
transX =0;
transY =0;
}
matrix.postTranslate(transX, transY);
canvas.drawBitmap(bitmap, matrix,newPaint());
if(bitmap !=null) {
bitmap.recycle();
bitmap =null;
System.gc();
}
return canvasBitmap;
}
8、獲取拍攝角度
public int getExifOrientation(String path) {
String img_path = path;
System.out.println("圖片位置"+ img_path);
int degree =0;
ExifInterface exif =null;
try{
exif =new ExifInterface(img_path);
}catch(IOException e) {
e.printStackTrace();
}
if(exif !=null) {
intorientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if(orientation != -1) {
// We only recognize a subset of orientation tag values.
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree =90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree =180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree =270;
break;
}
}
}
return degree;
}
基本處理操作就這么多渣慕,僅供參考嘶炭。