Rotation not woking in image analysis

47 views
Skip to first unread message
Assigned to charco...@google.com by wuj...@google.com

Pengcheng Yin (nowavewater)

unread,
Mar 13, 2023, 3:10:59 AM3/13/23
to Android CameraX Discussion Group
My device is fixed, default front camera orientation looks like this:

Untitled.png
No matter what rotation I set to target rotation of image analysis, the saved picture always looks like this:
Untitled.png 
The preview looks right though, is there any way to get the input image right for the image analysis, the camerax version is 1.0.2

val lifecycleOwner = LocalLifecycleOwner.current
                    val context = LocalContext.current
                    val cameraProviderFuture = remember { ProcessCameraProvider.getInstance(context) }
                    val fileDataSource: FileDataSource = remember { FileDataSource(context) }
                    var savedImage: Boolean = remember { false }
                    AndroidView(
                        factory = { ctx ->
                            val previewView = PreviewView(ctx)
                            val executor = ContextCompat.getMainExecutor(ctx)
                            cameraProviderFuture.addListener({
                                val cameraProvider = cameraProviderFuture.get()
                                val preview = Preview.Builder()
                                    .setTargetRotation(Surface.ROTATION_270)
                                    .build().also {
                                    it.setSurfaceProvider(previewView.surfaceProvider)
                                }
                                val imageAnalysis = ImageAnalysis.Builder()
                                    .setTargetRotation(Surface.ROTATION_270)
                                    .build()
                                imageAnalysis
                                    .setAnalyzer(executor) { imageProxy ->
                                    if (!savedImage) {
                                        val mediaImage = imageProxy.image
                                        if (mediaImage != null) {
                                            val bitmap = Bitmap.createBitmap(mediaImage.width, mediaImage.height, Bitmap.Config.ARGB_8888)
                                            YuvToRgbConverter(context).yuvToRgb(mediaImage, bitmap)
                                            val fileName = "face_photo_" + Calendar.getInstance().timeInMillis + ".jpg"
                                            val file = fileDataSource.getFileByName("face_photo", fileName)
                                            val sink: BufferedSink = file.sink().buffer()
                                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, sink.outputStream())
                                            sink.flush()
                                            sink.close()
                                            savedImage = true
                                        }
                                    }
                                }
                                val cameraSelector = CameraSelector.Builder()
                                    .requireLensFacing(CameraSelector.LENS_FACING_FRONT)
                                    .build()
                                cameraProvider.unbindAll()
                                val camera = cameraProvider.bindToLifecycle(
                                    lifecycleOwner,
                                    cameraSelector,
                                    preview,
                                    imageAnalysis
                                )
                            }, executor)
                            previewView
                        },
                        modifier = Modifier.fillMaxSize(),
                    )

Charcoal Chen

unread,
Mar 14, 2023, 7:36:21 AM3/14/23
to Android CameraX Discussion Group, nowav...@gmail.com
Hi,

For the Preview part, if you want to rotate the Preview's image, please refer to the example in the javadoc of PreviewView#getViewPort(int) to know how to do it.

For the ImageAnalysis part, you need to retrieve the rotation degrees values from the received ImageProxy's ImageInfo, and then, save the rotation degrees as the Exif rotation tag value into the JPEG file. In your code, the Exif rotation value is not saved. Please refer to the javadoc of ImageInfo#getRotationDegrees() for the details.
Reply all
Reply to author
Forward
0 new messages