解决相机拍照不清晰,拍照旋转问题!

private Uri uri;

//拍照

Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); 

ContentValues values = new ContentValues();  
       values.put(MediaStore.Images.Media.DISPLAY_NAME, String.valueOf(System.currentTimeMillis())+".jpg");  
       values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");  
       uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
       getImageByCamera.putExtra(MediaStore.Images.Media.ORIENTATION, 0);  
       getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);

startActivityForResult(getImageByCamera,REQUEST_CAMERA);

//获取拍照的图片

if (requestCode == REQUEST_CAMERA) { //相机获取照片
String path = Tool.getPathFromUri(context, uri);
int orientation = Tool.readPictureDegree(path);//获取旋转角度
AppLog.d("dingdongkai", "orientation==="+orientation);
if(Math.abs(orientation) > 0){
Bitmap bit = getBitmapFromUri(uri);
   bitmap =  Tool.rotaingImageView(orientation, bit);//旋转图片  
}  
startPhotoZoom(Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null)));

//根据uri获取图片路径

public static String getPathFromUri(Context mContext,Uri contentUri){
AppLog.d("dingdongkai", "contentUri==="+contentUri.toString());
   String[] proj = { MediaStore.Images.Media.DATA };
   CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
   Cursor cursor = loader.loadInBackground();
   int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
   return cursor.getString(column_index);
}

 

//获取图片的旋转角度
public static int readPictureDegree(String path) {  
int degree  = 0;  
try {
ExifInterface exifInterface = new ExifInterface(path);  
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
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;
}
} catch (IOException e) {  
e.printStackTrace();  
}  
return degree;  
}  

 

/**
 * 裁剪图片方法实现
 * 
 * @param uri
 */
public void startPhotoZoom(Uri uri) {
// 设置裁剪属性
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 320);
intent.putExtra("outputY", 320);
intent.putExtra("return-data", true);
startActivityForResult(intent, 6);
}

你可能感兴趣的:(问题)