Skip to content
Advertisement

How to stop Image rotation using BitmapFactory once camera takes photograph?

When I have captured a photograph with the camera on my Pixel 3XL, it rotates 90 degrees when displaying in the following page (in this instance EditorActivity.class)

I have tried to fix this by simply adding a flipIMage method but it does not appear to do anything…

 if(resultCode == RESULT_OK){
    
                        bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+ "/photoTemp.png");
                        flipIMage(bitmap);
                        Intent intent = new Intent(this, EditorActivity.class);
                        startActivity(intent);
                    }

flipIMage is;

public static Bitmap flipIMage(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        int rotation = fixOrientation(bitmap);
        matrix.postRotate(rotation);
        matrix.preScale(-1, 1);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

If it helps, my cameraClicked method is here;

public void cameraClicked(View view) {


        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File tempFile = new File(Environment.getExternalStorageDirectory().getPath()+ "/photoTemp.png");
        try {
            tempFile.createNewFile();
            Uri uri = FileProvider.getUriForFile(
                    this,
                    this.getApplicationContext()
                            .getPackageName() + ".provider", tempFile);
            //install.setDataAndType(uri, mimeType);
            takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(takePictureIntent, 2);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

fixOrientation method;

private static int fixOrientation(Bitmap bitmap) {
        if (bitmap.getWidth() > bitmap.getHeight()) {
            return 90;
        }
        return 0;
    }

EditorActivity;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        

            //
        
        setContentView(R.layout.activity_editor);
        
        Bitmap bitmap = MainActivity.bitmap;
        
        //
        
        
        
        
        
        btnDoneOrEdit = (Button) findViewById(R.id.btnDoneOrEdit);
        btnChangeMask = (Button) findViewById(R.id.btnChangeMask);
        
        editorImage = (EditorImage)findViewById(R.id.editorImage);
        editorImage.setBitmapImage(bitmap);
        editorImage.setTopPanel((LinearLayout) findViewById(R.id.topPanel));
        editorImage.setBottomPanel((LinearLayout) findViewById(R.id.bottomPanel));
        gridview_zombie = (GridView)findViewById(R.id.gridView_zombie);
        linearcontent = (LinearLayout)findViewById(R.id.linearlayout_content);

Advertisement

Answer

When you reload the image on the editor.class you can try checking the EXIF data on the image and rotating the image before displaying.

To get the path from the Bitmap use:

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = 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); 
}

     public static Bitmap rotateImage(Bitmap bitmap, String path) throws IOException {
        int rotate = 0;
        ExifInterface exif;
        exif = new ExifInterface(path);
        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;
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, true);
    }

You can then use the returned Bitmap and display that.

So your call stack would be

  Uri uri = getimageUri(this, MainActivity.Bitmap);
  String path = getRealPathFromUri(uri);
  Bitmap rotatedBitmap = rotateImage(MainActivity.Bitmap, path);
  editorImage.setBitmapImage(bitmap);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement