How to obtain the orientation of the image taken with the Camera 2 API

Asked 2 years ago, Updated 2 years ago, 149 views

I am using the Camera 2 API.
I want to get the orientation of the image after taking the picture and correct it to the correct position, but I can't find a good way to do it.
Currently, we are correcting the orientation as follows.

public ImageStore(Image image, File file, Context con, int cameraDirect){
    mImage=image;
    mFile = file;
    This.context=con;
    This.camDirection=cameraDirect;
}

@ Override
public void run() {

    Bitmap bmp = null;
    ByteBuffer buffer=mImage.getPlanes()[0].getBuffer();
    byte [ ] bytes = new byte [buffer.remaining()];
    buffer.get(bytes);

    try{
        bmp = BitmapFactory.decodeByteArray (bytes, 0, bytes.length, null);
    }catch(Exceptione){
        e.printStackTrace();
    }

    Matrix m;

    if(bmp.getWidth()>bmp.getHeight()){
        if(camDirection==FRONT_CAMERA){
            m = new Matrix(); 
            m.setRotate(270);
            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
        } else{
            m = new Matrix(); 
            m.setRotate(90);
            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
        }
    }
}

According to the above method, it may not work depending on the model, so if possible, I would like to be able to obtain the current orientation.
I wanted to use ExifInterface, but I couldn't get it because it wasn't saved in a file.

If you are familiar with this, please take care of me.

add
I tried to force myself to solve it, but I solved myself.
I didn't want to leave the photos I took in the gallery, but I wanted the Exif information, so I temporarily saved it in the gallery, retrieved it again from the gallery, and obtained the Exif information.
It's a little unreasonable to delete the image from the gallery once you get the Exif information, but I chose this specification.
I look forward to working with you again if there is a good way.

android camera

2022-09-30 21:18

1 Answers

When the shooting is done (TakePicture?), how about getting the direction of the device and using it as a basis?

//Example code to write below the Activity class.
introtation=this.getWindowManager().getDefaultDisplay().getRotation();
switch(rotation){
    caseSurface.ROTATION_0:return0;
    caseSurface.ROTATION_90:return90;
    caseSurface.ROTATION_180:return180;
    caseSurface.ROTATION_270:return270;
}
return 0;


2022-09-30 21:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.