0

I capture image from my cam and create bitmap and pass the path to ExifInterface to determine the rotation, since the cam captured image is rotated 90 degrees all the time. But this below code always shows value 0 which corresponds to ORIENTATION_UNDEFINED rotation type.

It is any other way to determine rotation or I am doing something wrong here ?

 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;
}
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31

1 Answers1

0

I capture image from my cam

I am going to guess that this is from ACTION_IMAGE_CAPTURE, given that your code references a requestCode.

In that case, what you have:

  • Reads in the image from a file
  • Saves the image again to another file, wiping out the EXIF headers along the way

You don't need any of that.

You know where the image is, as you (presumably) supplied a location via EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE request. So, use ExifInterface to read from that location. Get rid of the bitmap stuff, in particular writing the bitmap back out. A JPEG file may have EXIF headers. A Bitmap does not, and a JPEG file created from a Bitmap does not (unless you add them yourself).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • `else if (requestCode == CAPTURE_IMAGE) { ExifInterface exifInterface = new ExifInterface(uriForFile.toString()); Log.d(TAG, "onActivityResult: "+exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));` I tried this in the first instance it self but it gives exception as it cant find the path I give.. after that I started with bitmap flow – Aalap Patel Oct 13 '17 at 13:47
  • Since with string path exif doesnt give orientation I also tried using inputstream from bitmap and passing to create Exif instance but same result 0 always for orientation where as bitmap is rotated.. – Aalap Patel Oct 13 '17 at 13:56
  • @AalapPatel: "I tried this in the first instance it self but it gives exception as it cant find the path I give" -- that is because a `Uri` is not a file. You might consider editing your question and posting the code that is triggering what you have presently in your question. For example, if my guess is correct, and you are using `ACTION_IMAGE_CAPTURE`, show your code that sets up that `Intent` and calls `startActivityForResult()`. – CommonsWare Oct 13 '17 at 14:01
  • "that is because a Uri is not a file", thats why I tried to create bitmap and from bitmap to file, and thus using file path in `ExifInterface` to determine the rotation, but it gives 0.. I know from uri to bitmap to file is a long way but I could not find a way to create file from uri.. – Aalap Patel Oct 13 '17 at 14:11
  • @AalapPatel: You do not need to "create file from uri". You already have the `File`. It is called `photoFile`. – CommonsWare Oct 13 '17 at 14:13
  • yes.. indeed.. silly mistake, it was right there and I was looking for it everywhere.. it solved, gives result 6 which is what I want.. many thanks.. – Aalap Patel Oct 13 '17 at 14:18