0

I am making an application in which I have to take a picture every one second in a service without launching the camera preview.

I wrote the following code in service and able to take picture.

if (cameraId < 0) {
    Log.d(TAG, "No camera found");
} 
else 
{
   camera = Camera.open(cameraId);
   camera.startPreview();
}
camera.takePicture(null, null, bitmapHandler);

In the bitmapHandler class I get the capture image data in

@Override
public void onPictureTaken(byte[] data, Camera camera) {
.
.
.
}
  1. when I take the picture in portrait mode, the resultant image is rotated by 90 degree clockwise.
  2. when I take the picture in landscape mode, the resultant image is proper.
  3. when I take the picture in reverse-landscape mode, the resultant image is rotated by 180 degree
  4. when I take the picture in reverse-portrait mode, the resultant image is rotated by 270 degree What i did to get proper image so that I can feed that image to my neural network.

What I tried : I am rotating the bitmap by 90 when image was taken in portrait mode, but still not able to find in which mode the image was captured.

Does the Camera API have any way to know or control the rotation angle ?

JoeG
  • 7,191
  • 10
  • 60
  • 105
  • Device orientation is not part of Camera API, but you can find it from the device – Alex Cohn Oct 15 '18 at 15:03
  • See *[How to detect when the device switch from portrait to landscape mode?](https://stackoverflow.com/questions/18321680/how-to-detect-when-the-device-switch-from-portrait-to-landscape-mode)* and also *[Rotating phone quickly 180 degrees, camera preview turns upside down](https://stackoverflow.com/a/19599599/192373)* – Alex Cohn Oct 15 '18 at 20:36
  • 1
    Thanks @AlexCohn , I am able to get rotation change info in my service using OrientationEventListener – Sandeep Kumar Yadav Oct 16 '18 at 09:22

1 Answers1

0

I was using the function fixImageOrientation, parameters are image itself and path to the image file (as second parameter, I was passing the path where camera activity stores temporary image file)

    public static Bitmap rotateImage(Bitmap bmp, float angle)
    {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        System.out.println("Rotating image " + Float.toString(angle) + " degrees, rotateImage()");
        return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }

    public static Bitmap fixImageOrientation(Bitmap bmp, String path)
    {
        ExifInterface ei;
        boolean changed = true;
        try {
            ei = new ExifInterface(path);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

            if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
                bmp = rotateImage(bmp, 90);
            else if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
                bmp = rotateImage(bmp, 180);
            else if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
                bmp = rotateImage(bmp, 270);
            else
                changed = false;

        } catch (IOException e){
            System.out.println("IOException, image probably has no exif data, fixImageOrientation()");
            e.printStackTrace();

        }

        if (changed)
        {
            System.out.println("Image orientation fixed, fixImageOrientation()");

        }
        else
        {
            System.out.println("Image orientation did not change, fixImageOrientation()");
        }

        return bmp;
    }
unlut
  • 3,525
  • 2
  • 14
  • 23
  • it is working in the case when I saved the captured image and then trying to Exif data. But in my case, I need not to save the image, just want to feed the same image to a neural network model. – Sandeep Kumar Yadav Oct 16 '18 at 11:45
  • How do you use camera API to take your image. – unlut Oct 16 '18 at 20:24