ExifInterface不显示凸轮捕获图像的旋转

时间:2017-10-12 21:14:09

标签: android rotation exif android-bitmap

我从我的摄像头捕获图像并创建位图并将路径传递给ExifInterface以确定旋转,因为凸轮捕获的图像始终旋转90度。但是下面的代码总是显示对应于0旋转类型的值ORIENTATION_UNDEFINED

这是确定轮换的任何其他方式,或者我在这里做错了什么?

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photoFile = null;

    try {
        photoFile = Utils.createImageFile(getContext());
        String authorities = context.getPackageName() + ".fileprovider";
        uriForFile = FileProvider.getUriForFile(context, authorities, photoFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "handleCaptureImage: " + uriForFile);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
    startActivityForResult(intent, CAPTURE_IMAGE);


if (requestCode == CAPTURE_IMAGE) {

     Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uriForFile);
     saveBitmap(bitmap);
}


 public static File saveBitmap(Bitmap bitmap) {
    File file = null;

    String imageFileName = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
    String path = App.getAppContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + imageFileName;


    if (bitmap != null) {
        file = new File(path);
        try {
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(path);                     

                //this bitmap is rotated when I observe in debug mode.
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

                ExifInterface exifInterface = new ExifInterface(path);

                //still always returns 0 I expect 6 which is for 90 degrees
                Log.d(TAG, "saveBitmap: "+exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return file;
}

1 个答案:

答案 0 :(得分:0)

  

我从我的摄像头捕获图像

我猜这是来自ACTION_IMAGE_CAPTURE,因为您的代码引用了requestCode

在这种情况下,你有什么:

  • 从文件中读取图像
  • 将图像再次保存到另一个文件,沿途消灭EXIF标题

你不需要任何这些。

您知道图片的位置,因为您(可能)在EXTRA_OUTPUT请求中通过ACTION_IMAGE_CAPTURE提供了一个位置。因此,使用ExifInterface从该位置读取。摆脱位图的东西,特别是把位图写回来。 JPEG文件可能包含EXIF标头。 Bitmap没有,Bitmap创建的JPEG文件没有(除非您自己添加)。