相机意图自动旋转到90度

时间:2013-10-22 07:26:07

标签: android

在我的下面的代码中,我正在尝试使用原生相机拍照并上传到服务器但是当我把它作为肖像并在画廊中查看它作为景观意味着,它旋转到90度。请帮忙: -

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {

            handleCameraPhoto();
}
private void handleCameraPhoto() {
    Intent mediaScanIntent = new Intent(
            "android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);

    mediaScanIntent.setData(contentUri);
    getActivity().sendBroadcast(mediaScanIntent);
}

如何在保存到SD卡之前旋转图像?

2 个答案:

答案 0 :(得分:22)

在listview中显示图像时我也遇到了这种问题。但是使用EXIF数据我能够找到合适的方向来设置图像。

这是用于显示的位图对象:

  Matrix matrix = new Matrix();
  matrix.postRotate(getImageOrientation(url));
  Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
  bitmap.getHeight(), matrix, true);

这是在上面代码的第2行中使用的方法来旋转图像方向。

 public static int getImageOrientation(String imagePath){
     int rotate = 0;
     try {

         File imageFile = new File(imagePath);
         ExifInterface exif = new ExifInterface(
                 imageFile.getAbsolutePath());
         int orientation = exif.getAttributeInt(
                 ExifInterface.TAG_ORIENTATION,
                 ExifInterface.ORIENTATION_NORMAL);

         switch (orientation) {
         case ExifInterface.ORIENTATION_ROTATE_270:
             rotate = 270;
             break;
         case ExifInterface.ORIENTATION_ROTATE_180:
             rotate = 180;
             break;
         case ExifInterface.ORIENTATION_ROTATE_90:
             rotate = 90;
             break;
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
    return rotate;
 }

这可能不是您问题的准确答案,它对我有用,并希望它对您有用。

答案 1 :(得分:2)

Matrix matrix=new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);



This method does not require creating a new bitmap each time.. Hope this works.