0

I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured.

I want rotate image to straight if they are rotated.

I tried to do it but its not working.

   private void onCaptureImageResult(Intent data) {
    try {

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".png");

    FileOutputStream fo;

        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();


    profileImage = destination;

    Bitmap rotatedBitmap = modifyOrientation(thumbnail, profileImage.getAbsolutePath());

    ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
    byte[] byteArray1 = stream1.toByteArray();

    File tempFile1 = File.createTempFile("temp", null, getCacheDir());
    FileOutputStream fos1 = new FileOutputStream(tempFile1);
    fos1.write(byteArray1);


    if (rotatedBitmap != null) {
        profileImageView.setImageBitmap(rotatedBitmap);
        profileImage = tempFile1;
    } else {
        profileImageView.setImageBitmap(thumbnail);
        profileImage = destination;
    }

}
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
    byte[] byteArray = stream.toByteArray();

    try {

        File tempFile = File.createTempFile("temp",null, getCacheDir());
        FileOutputStream fos = new FileOutputStream(tempFile);
        fos.write(byteArray);

        profileImage = tempFile;

        Bitmap rotatedBitmap = modifyOrientation(bm,profileImage.getAbsolutePath());

        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
        byte[] byteArray1 = stream1.toByteArray();

        File tempFile1 = File.createTempFile("temp",null, getCacheDir());
        FileOutputStream fos1 = new FileOutputStream(tempFile1);
        fos1.write(byteArray1);



        if(rotatedBitmap != null) {
            profileImageView.setImageBitmap(rotatedBitmap);
            profileImage = tempFile1;
        }
        else {
            profileImageView.setImageBitmap(bm);
            profileImage = tempFile;
        }
    }

    catch (IOException e)
    {

    }

}

EDIT:

I tried to use camera intent now and get path from intent still its not working.

private void onCaptureImageResult(Intent data) {
    try {

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".png");

    FileOutputStream fo;

        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
        Bitmap rotatedBitmap = null;

   // profileImage = destination;

        Uri tempUri = getImageUri(getApplicationContext(),thumbnail);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
        File finalFile = new File(getRealPathFromURI(tempUri));

        ExifInterface ei = new ExifInterface(finalFile.getAbsolutePath());
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotateImage(thumbnail, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotateImage(thumbnail, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotateImage(thumbnail, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                break;
        }

    if (rotatedBitmap != null) {

        profileImageView.setImageBitmap(rotatedBitmap);

        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
        byte[] byteArray1 = stream1.toByteArray();

        File tempFile1 = File.createTempFile("temp", null, getCacheDir());
        FileOutputStream fos1 = new FileOutputStream(tempFile1);
        fos1.write(byteArray1);

        profileImage = tempFile1;
    } else {
        profileImageView.setImageBitmap(thumbnail);
        profileImage = destination;
    }

}
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
   }


public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}



   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

What's wrong now?

Can anyone help please? What's going wrong? Thank you..

Sid
  • 2,792
  • 9
  • 55
  • 111
  • Possible duplicate of [why image captured using camera intent gets rotated on some devices in android](http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android) – earthw0rmjim Oct 20 '16 at 13:26
  • You should include method `modifyOrientation` in this question since i think that method is the problem – HendraWD Oct 20 '16 at 13:33
  • yes I forgot to add it.. Please check the edited question.. @Hendra Wijaya Djiono – Sid Oct 20 '16 at 13:43
  • Which orientation do you get? Which case of the switch statement is executed? Or is default choosen? You could give better info about whats happens. – greenapps Oct 20 '16 at 13:46
  • as I debug I found it dose not go inside the switch cases.and orientation I get as 0.@greenapps – Sid Oct 20 '16 at 13:48
  • Again: it is impossible that the swich statement is not executed. So it goes inside. Tell what is choosen! – greenapps Oct 20 '16 at 13:50
  • `profileImage.getAbsolutePath()`. Please show the used intent. Show how you construct `profileImage`. Did you check if it still has the same path after taking the picture.? Toast it to see. – greenapps Oct 20 '16 at 13:54

2 Answers2

0
profileImage = destination;

You took the thumbnail which is a Bitmap and wrote it to file.

Then after that you used that file to extract an exifinterface.

But bitmaps do not contain exif information. And hence your file does not either.

So your orientation is always null;

If you had used profileImage to launch the camera intent then leave it as is.

So remove above statement.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Which image path need to pass to EXIF?? Not getting it. :-( @greenapps – Sid Oct 20 '16 at 17:29
  • Please.. you do not have to address the poster of an answer with@name every time as it is obvious that you talk to the poster. – greenapps Oct 20 '16 at 17:32
  • I asked you if you had used `profileImage` variable with your intent. You did not answer that question. You better post code for the used intent in your post. At the beginning. You have to use the image path of the image taken by the camera. Not a path you saved the thumbnail bitmap to as you do now. – greenapps Oct 20 '16 at 17:34
  • I tried to get uri and its path now can you please check edited question? – Sid Oct 20 '16 at 17:50
  • I do not see the code for an intent to start the camera app. Nor code for onActivityResult. Please remove all gallery intent code. You make it confusing. Narrow down to one problem only. – greenapps Oct 20 '16 at 17:55
  • its all camera result. onCaptureImageResult method is called in onActivityResult of camera intent. I have added the method. Please check. – Sid Oct 20 '16 at 18:00
  • You still did not post code for the intent that starts the camera app. And also you did not remove the irrelevant gallery code. – greenapps Oct 20 '16 at 18:42
  • You still did not post the wanted code. I still do not know how you start the camera app. – greenapps Oct 21 '16 at 09:31
  • In general: why dont you tell more? You are not reacting on the things i say. Do you undertand now why your code could not get the exif? If you do not tell what you know and not deliver code asked this will prolongue for another week. – greenapps Oct 21 '16 at 09:35
  • private void cameraIntent() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CAMERA); } This way I start the camera intent – Sid Oct 21 '16 at 09:40
  • You should have posted that as code of course. Not as a comment. And you should have started with it right away. Ok. Your problem starts with this intent. Now tell what the problem is starting the camera app in this way. What do you get in onActivityResult? And what is it that you should want to get in onActivityResult but do not get? (This is your chance to show you understand your problem). – greenapps Oct 21 '16 at 09:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126308/discussion-between-sid-and-greenapps). – Sid Oct 21 '16 at 09:50
  • No chat. Give all info here. – greenapps Oct 21 '16 at 09:53
0
public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
 int rotate = 0;
try {
    context.getContentResolver().notifyChange(imageUri, null);
    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;
    }

    Log.i("RotateImage", "Exif orientation: " + orientation);
    Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
    e.printStackTrace();
}
return rotate;
}

And put this code in Activity result method and get value to rotate image...

String selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();

        int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath);

Rotate Bitmap using this

 public static Bitmap rotate(Bitmap bitmap, float degrees) {
   Matrix matrix = new Matrix();
   matrix.postRotate(degrees);
   return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  }
Ganesh Pokale
  • 1,538
  • 1
  • 14
  • 28