0

I'm trying to get pictures using front camera (selfie) and back camera and save it to an imageview. When I tried to take a picture using the front camera (selfie) the image is getting flipped upside down (vertical) and when using the back camera, the image is getting flipped horizontally.

I'm trying to detect the camera usage using "camInfo.facing ==(Camera.CameraInfo.CAMERA_FACING_FRONT" and "camInfo.facing ==(Camera.CameraInfo.CAMERA_FACING_BACK" but only "camInfo.facing ==(Camera.CameraInfo.CAMERA_FACING_BACK" is working. I hope I can give some rotation to get it to work.

Dont know how to proceed. Help much appreciable.

compileSdkVersion 28
    defaultConfig {
        applicationId "com.austurn.keikonew.keiko"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"



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

        try {
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    Uri uri = photoURI;
                    Bitmap myImg = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
                    Matrix matrix = new Matrix();
                    int width = myImg.getWidth();
                    int height=myImg.getHeight();
                    Camera cam = null;
                    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();

                        Camera.CameraInfo camInfo = new Camera.CameraInfo();

                        if (camInfo.facing ==(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
                            matrix.postRotate(90);
                            Toast.makeText(this.getActivity(), "Front camera", Toast.LENGTH_LONG).show();
                             }

                        if (camInfo.facing ==(Camera.CameraInfo.CAMERA_FACING_BACK)) {
                            Toast.makeText(this.getActivity(), "back camera", Toast.LENGTH_LONG).show();
                            float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
                            Matrix matrixMirrorY = new Matrix();
                            matrixMirrorY.setValues(mirrorY);
                            matrix.postConcat(matrixMirrorY);
                            matrix.postRotate(270);
                        }

                    Bitmap bitPicFinal = Bitmap.createBitmap(myImg, 0, 0, width, height,matrix, true);
                    myImg.recycle();
                    int desWidth;
                    int desHeight;
                    desWidth = bitPicFinal.getWidth();
                    desHeight = desWidth;
                    Bitmap croppedBitmap = Bitmap.createBitmap(bitPicFinal, 0,bitPicFinal.getHeight() / 2 - bitPicFinal.getWidth() / 2,desWidth, desHeight);
                    croppedBitmap = Bitmap.createScaledBitmap(croppedBitmap, 528, 528, true);
                    camview.setImageBitmap(croppedBitmap);
                }
        }catch(Exception e){
            Toast.makeText("Something went wrong", Toast.LENGTH_LONG).show();
        }
    }
}
jkdev
  • 11,360
  • 15
  • 54
  • 77
AjKu
  • 47
  • 12

2 Answers2

0

You are creating a new instance of Camera.CameraInfo that does not actually refer to the camera that was used to capture photo. You should use exif metadata contained in the captured file, try the solution described here:

Why does an image captured using camera intent gets rotated on some devices on Android?

igi
  • 481
  • 5
  • 6
  • Thank you, but im using only one type of orientation (portrait). I would like to detect which camera has been used whether front camera or back camera. does the exif metadata have that information. I tried the exif function, but the orientation value is "0" which is portrait mode. I'm not flipping the camera to have landscape mode. – AjKu Jul 06 '18 at 14:24
  • 1
    I don't think you can reliably detect front camera using EXIF headers. You will get inconsistent results on different devices. I would recommend implementing your own photo capturing activity, this will give you full control over taking the photo and you will know which camera was used. – igi Jul 07 '18 at 21:13
0

The Camera.CameraInfo() default constructor says nothing about the actual device you use. One should fill this object using Camera#getCameraInfo() static method. Then, you would know whether the camera device at index 1 is facing back or front, and also have the hardware orientation of this device, which can help to adjust the images taken by this camera through Camera API.

This orientation info is irrelevant for images received via ACTION_IMAGE_CAPTURE intent, because the Camera app may choose to rotate or not to rotate the Jpeg before storing it to the MediaStore.

On most devices, the EXIF of stored image will have some indication of front or back-facing camera, but there is no officially documented way to determine this.

Note that the EXIF orientation "0" does not mean "portrait", it means that the Jpeg is stored in a natural way, and shoudl be displayed without adjusting orientation. If you get an image with orientation "0" and width < height, then it's portrait; if you get an image with orientation "0" and width > height, it's landscape.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307