javaandroidscreen-resolutionandroid-orientationandroid-camerax

Sensor orientation doesn't fit device orientation


I have an application which until now worked only in landscape mode. Here is the code:

AndroidManifest.xml

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:screenOrientation="landscape"
            tools:ignore="LockedOrientationActivity">

CameraHelper.java

private void bindCamera(@NonNull ProcessCameraProvider cameraProvider) {
    Log.d(TAG, "Binding camera");
    CameraSelector cameraSelector = new CameraSelector.Builder()
            .requireLensFacing(CameraSelector.LENS_FACING_BACK)
            .build();

    // Image analysis settings
    ImageAnalysis.Builder imageAnalysisBuilder = new ImageAnalysis.Builder();

    ImageAnalysis imageAnalysis = imageAnalysisBuilder
            .setImageQueueDepth(1)
            .setTargetResolution(new Size(1280, 720))
            .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
            .build();

    imageAnalysis.setAnalyzer(analyzerExecutor, imageAnalyzer);

    var captureBuilder = new ImageCapture.Builder();
    captureBuilder.setTargetRotation(Surface.ROTATION_90).
            setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY).
            setTargetResolution(new Size(4032, 3024)).
            setJpegQuality(Constants.JPEG_QUALITY).
            setFlashMode(ImageCapture.FLASH_MODE_OFF);

    var ext = new Camera2Interop.Extender<>(captureBuilder);
    ext.setCaptureRequestOption(CaptureRequest.CONTROL_CAPTURE_INTENT,
            CaptureRequest.CONTROL_CAPTURE_INTENT_MOTION_TRACKING);
    imageCapture = captureBuilder.build();

    try {
        cameraProvider.unbindAll();
        if (lifecycleOwner.getLifecycle().getCurrentState() != DESTROYED) {
            this.camera = cameraProvider.bindToLifecycle(lifecycleOwner,
                    cameraSelector,
                    imageCapture,
                    imageAnalysis);
        }
    } catch (Exception e) {
        Log.e(TAG, "Camera binding failed", e);
        MainActivity.instance.snackbar.showError("Camera binding failed", e);
    }
}

Now, i would like the application to work only in portrait mode. Steps i did:

I tried treading from here and if i understand correctly i need to rotate camera sensor.

Please advise me what changes i need to do in my code to get the portrait mode 720x1280 orientation.

Thanks!


Solution

  • I managed to solve my problem, apparently when i tried to change Size to Size(720,1280) - this created camera with 1280x720 resolution, but it's really an image of size width=720 and height=1280, so it was confusing, i just need than to flip the image i got.