0

As I am working on Android app with minimum API level 16. I have to build my custom Camera Builder for live frames and process them for OCR.I have set a surface view and add a preview callback so that I can get live frames on onPreviewFrame(byte[] bytes, Camera camera) Method.

By default the screen is in landscape mode and in most of the devices I am getting the correct orientation. But in some devices I am getting image 180 degrees rotated in this method and was not able to process.

Display display = ((WindowManager) Objects.requireNonNull(context.getSystemService(WINDOW_SERVICE))).getDefaultDisplay();
return display.getRotation();

The above method return the rotation 90 degrees in both devices. But on one, the image has correct orientation and on other the image is 180 degrees rotated. Here is the camera class:

public abstract class Camera {

    private static android.hardware.Camera mCamera;


/**
 * A safe way to get an instance of the Camera object.
 */


private static android.hardware.Camera getCameraInstance(int cameraFacing) {
    android.hardware.Camera c = null;
    try {
        c = android.hardware.Camera.open(cameraFacing); // attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}


public static void closeCamera() {
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }

}

public static void autoFocus() {
    if (mCamera != null)
        try {
            mCamera.autoFocus(null);
        } catch (Exception ignored) {

        }
}


/**
 * focusMode: android.hardware.Camera.Parameters
 * cameraFacing: android.hardware.Camera.CameraInfo.FACING_FRONT or FACING_BACK
 */
public static class CameraBuilder {
    private static final String TAG = CameraBuilder.class.getSimpleName();
    private String focusMode = android.hardware.Camera.Parameters.FOCUS_MODE_AUTO;
    private int cameraFacing = android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
    private int displayOrientation = 0;

    public CameraBuilder() {

    }

    /**
     * @param focusMode default: android.hardware.Camera.Parameters.FOCUS_MODE_AUTO
     * @return CameraBuilder
     */
    public CameraBuilder setFocusMode(String focusMode) {
        this.focusMode = focusMode;
        return this;
    }

    /**
     * @param cameraFacing default : android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK
     * @return CameraBuilder
     */
    public CameraBuilder setCameraFacing(int cameraFacing) {
        this.cameraFacing = cameraFacing;
        return this;
    }


    public CameraBuilder setDisplayOrientation(int orientation) {
        this.displayOrientation = orientation;
        return this;
    }

    public android.hardware.Camera build() {
        try {
            mCamera = getCameraInstance(cameraFacing);
            android.hardware.Camera.Parameters params = mCamera.getParameters();
            params.setFocusMode(focusMode);
            mCamera.setParameters(params);

        } catch (Exception e) {
            // Camera is not available (in use or does not exist)
            Log.e(TAG, "Camera Exception: " + e.getMessage());
        }
        mCamera.setDisplayOrientation(displayOrientation);
        return mCamera;
    }

}

}

Here is the initialization of my camera.

camera = new CameraBuilder()
           .setCameraFacing(Camera.CameraInfo.CAMERA_FACING_BACK)
           .setFocusMode(android.hardware.Camera.Parameters.FOCUS_MODE_AUTO)
           .setDisplayOrientation(0)
           .build();
  • can you try camera initialization without ".setDisplayOrientation(0)" and check, if your result changes to 90° then let me know. I will share some alternate solution – Abdul Wahab Aug 26 '18 at 11:18
  • 1
    It's probably Nexus 5x or Nexus 6. Please see *[Nexus 5x reverse landscape sensor fix in a android camera preview app](https://stackoverflow.com/a/35431231/192373)*. – Alex Cohn Aug 26 '18 at 15:30

0 Answers0