Search code examples
androidandroid-cameranexus6

Android Camera Api - Nexus 6 - Camera showing showing Inverse


The requirement is to show camera in portrait mode .

The camera view is displaying properly in all other devices like - Nexus 4 , Nexus 5 , Samsung S3 , Samsung S4 etc .

With NEXUS 6 the camera is displayed as upside down .

Here is how i am setting the camera parameters -

private void setCameraParameters() {
    try {
        Parameters parameters = mCamera.getParameters();
        Camera.CameraInfo camInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(mCameraId, camInfo);
        int cameraRotationOffset = camInfo.orientation;
        System.out.println("Offset :" + cameraRotationOffset);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            parameters.set("orientation", "portrait");
            mCamera.setDisplayOrientation(90);

            Camera.Size size = getBestCameraSize(90,getWidth(),getHeight(),parameters);
            if(size==null) {
                size = getOptimalPreviewSize(90, getWidth(), getHeight(), parameters);
            }
            parameters.setPreviewSize(size.width,size.height);
            mCamera.setParameters(parameters);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

After my experiments i understood that setting display orientation to 270 degrees works fine with Nexus 6 .

Query - How to find out what all devices need the display orientation as 90 and what all devices need the display orintation as 270 ? And how to detect it ?


Solution

  • Use this code to set orientation of your camera as portrait. Its a generic one and should work for all devices:

    public static void setCameraDisplayOrientation(Activity activity,
             int cameraId, android.hardware.Camera camera) {
         android.hardware.Camera.CameraInfo info =
                 new android.hardware.Camera.CameraInfo();
         android.hardware.Camera.getCameraInfo(cameraId, info);
         int rotation = activity.getWindowManager().getDefaultDisplay()
                 .getRotation();
         int degrees = 0;
         switch (rotation) {
             case Surface.ROTATION_0: degrees = 0; break;
             case Surface.ROTATION_90: degrees = 90; break;
             case Surface.ROTATION_180: degrees = 180; break;
             case Surface.ROTATION_270: degrees = 270; break;
         }
    
         int result;
         if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
             result = (info.orientation + degrees) % 360;
             result = (360 - result) % 360;  // compensate the mirror
         } else {  // back-facing
             result = (info.orientation - degrees + 360) % 360;
         }
         camera.setDisplayOrientation(result);
     }
    

    Source: http://developer.android.com/reference/android/hardware/Camera.html