Orientation

30 views
Skip to first unread message

Giuseppe Sorce

unread,
Apr 4, 2024, 11:09:18 AMApr 4
to Android CameraX Discussion Group
I got screen orientation

fun checkRotation(context: Context,  onChangeRotation: (rotation : Int) -> Unit) {

   val  mOrientationEventListener = object : OrientationEventListener(context) {
        override fun onOrientationChanged(orientation: Int) {
            val rotation: Int
            if (orientation >= 45 && orientation < 135) {
                rotation = android.view.Surface.ROTATION_270
            } else if (orientation >= 135 && orientation < 225) {
                rotation = android.view.Surface.ROTATION_180
            } else if (orientation >= 225 && orientation < 315) {
                rotation = android.view.Surface.ROTATION_90
            } else {
                rotation = android.view.Surface.ROTATION_0
            }
            onChangeRotation(rotation)
        }
    }

    mOrientationEventListener.enable()
}
When app is in portrait i have values of 0 and in landscape 1 and 3.

Ok when i use camera i set orientation :

    private fun getImageAnalysis(): ImageAnalysis {
        var resolutionSelector = cameraInfoService.getResolutionSelector(
            orientationScreen,
            cameraInfoService.getISCamera4k(cameraInfo, cameraId)
        )
        imageAnalysis = ImageAnalysis.Builder()
            . setTargetRotation(rotationTarget)
            .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
            .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST).also {
                Camera2Interop.Extender(it)
                    .setSessionCaptureCallback(object : CameraCaptureSession.CaptureCallback() {
                        override fun onCaptureCompleted(
                            session: CameraCaptureSession,
                            request: CaptureRequest,
                            result: TotalCaptureResult
                        ) {
                            super.onCaptureCompleted(session, request, result)

                            mCapturedIso = result.get(CaptureResult.SENSOR_SENSITIVITY) ?: -1
                        }
                    })
            }
            .build().also {
                it.setAnalyzer(
                    ContextCompat.getMainExecutor(cameraInfoService.getContext()),
                    rgbaAnalyzer
                )
            }
        return imageAnalysis
    }

but when i log    imageAnalysis.resolutionInfo.rotationDegrees i find 90 for portrait and i's wiorng

 when (resolutionInfo?.rotationDegrees) {
            90, 270 -> {
                isPortrait = false
                width = resolutionInfo.resolution.height
                height = resolutionInfo.resolution.width
            }
            0, 180 -> {
                isPortrait = true
                width = resolutionInfo.resolution.width
                height = resolutionInfo.resolution.height
            }
        }

Xi Zhang (张熹)

unread,
Apr 4, 2024, 1:32:44 PMApr 4
to Giuseppe Sorce, Android CameraX Discussion Group
Your rotation code looks correct. FYI this is CameraX's code for getting Surface rotation from motion sensor: https://github.com/androidx/androidx/blob/0dafc4c4f7a37964ba3b6a5f4273cbcabfbbaf91/camera/camera-view/src/main/java/androidx/camera/view/RotationProvider.java#L159, which is equivalent to yours.

The rotation being 90 degrees for portrait mode is also expected. The camera sensor is installed with a 90 degrees rotation, and so is the output image. If the device is in portrait mode, you need to rotate the image by 90 degrees and swap the output's width and height. BTW a more reliable way to get the info on the image is ImageProxy#getImageInfo.

Please let us know if you have more questions.


--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/da2319e0-f575-4088-b162-4050142e758en%40android.com.
Reply all
Reply to author
Forward
0 new messages