CameraX Landscape Orientation VideoRecord

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

MXMS

unread,
Feb 8, 2023, 9:26:15 PM2/8/23
to Android CameraX Discussion Group
Hi guys, my first conversation in this group. I'm pretty happy with the CameraX library and I'm hoping I'm just missing something small.

Here's my current code:

```
private suspend fun bindCaptureUsecase() {
val cameraProvider = ProcessCameraProvider.getInstance(requireContext()).await()
val cameraSelector = getCameraSelector(0)

val preview = Preview.Builder()
.setTargetResolution(Size(1280,720))
.setTargetRotation(Surface.ROTATION_0)
.build().apply {
setSurfaceProvider(captureViewBinding.previewView.surfaceProvider)
}

val recorder = Recorder.Builder()
.build()

videoCapture = VideoCapture.withOutput(recorder)

try {
cameraProvider.unbindAll()
val camera = cameraProvider.bindToLifecycle(viewLifecycleOwner,cameraSelector,videoCapture,preview)

if (camera.cameraInfo.hasFlashUnit()) {
Log.d(TAG, "This device DOES have torch (flash) unit.")
try {
// This doesnt even seem to work...
camera.cameraControl.enableTorch(true)
Log.d(TAG, "Torch turned on successfully (forced on by default).")
} catch(exc: CameraControl.OperationCanceledException) {
Log.e(TAG, "Turning torch on ERROR: ", exc)
}
} else {
Log.e(TAG, "This device doesn't have torch (flash) unit.")
}

} catch (exc: Exception) {
// we are on main thread, let's reset the controls on the UI.
Log.e(TAG, "Use case binding failed", exc)
resetUIandState("bindToLifecycle failed: $exc")
}
enableUI(true)
}
```

Currently video is recording and the quality is high and most importantly the torch is automatically turned on when the recording begins (that specific function was required for my app). 

My app is in a fixed landscape orientation. I have placed the landscape orientation in the AndroidManifest of both my plugin and the root project.

The problem is the video output is in portrait rotation, despite apparently having the correct resolution.

Screenshot 2023-02-09 at 10.24.12.png

Hopefully this is enough information & someone can assist.

Thanks.
- Grant

Charcoal Chen

unread,
Feb 9, 2023, 5:33:50 AM2/9/23
to Android CameraX Discussion Group, brits...@gmail.com
Thanks for reporting this.
I tried to run CameraX coretestapp on a Samsung Note10+ device and lock the test app in landscape mode, but I can't reproduce the issue.
Could you help to clarify some information?

1. Can you share which devices you ran the app on and then encounter the issue? Does it happen on some specific devices or all devices?
2. What is the app used to watch the captured video? If it is not the Google Photos app, could you try whether the video can be displayed correctly in Google Photos? 


MXMS

unread,
Feb 9, 2023, 8:22:10 AM2/9/23
to Android CameraX Discussion Group, charco...@google.com, MXMS
I actually managed to get it working with this:

```
videoCapture = VideoCapture.withOutput(recorder)
videoCapture.targetRotation = Surface.ROTATION_90
```

I guess the setTargetResolution API isn't available but the actual variable property is? Not sure.
But hey at least the problem is fixed for me, maybe this thread will help others with landscape related issues.

Charcoal Chen

unread,
Feb 12, 2023, 10:23:38 PM2/12/23
to Android CameraX Discussion Group, brits...@gmail.com, Charcoal Chen
Thanks. Per your feedback, the issue should be caused by the default target rotation setting. Just as what you say that clarifying this issue can helps others. I want to clarify some more questions with you to find the root cause.

 In CameraX, we try to get the display rotation as the default target rotation when calling bindToLifecycle() to bind the use case. This approach works well in our current video related sample apps. In CameraXVideo sample app, you can easily download and build it to check the result. I did the following changes and the recorded video is in correct landscape orientation:
1. Mark out the lock portrait code here
2. Add android:screenOrientation="landscape" to lock activity in landscape mode here

I'm not sure what's your plugin design. Is it possible that when your plugin design binds the VideoCapture to the lifecycle, the display rotation value is still Surface_ROTATION_0, and then it causes the problem? Could you help to clarify what's the value returned by VideoCapture#getTargetRotation() (this is also a hidden API)? If the returned value is really Surface_ROTATION_0, could you double check the display rotation value is the same at the mean time? If possible, could you provide some information about why the display rotation value doesn't match the locked landscape orientation according to your plugin design (just provide the rough idea but not about your confidential design)?

About the setTargetRotation() API, it is under discussing to make it as a public API. So that apps can control the target rotation in the similar orientation-locked design.

Leo Huang

unread,
Feb 13, 2023, 12:36:53 AM2/13/23
to Charcoal Chen, Android CameraX Discussion Group, brits...@gmail.com
if you lock app orientation, you will have to listen to the device orientation change and update it to VideoCapture.
See the sample on https://groups.google.com/a/android.com/g/camerax-developers/c/gVLW8aC1Drs/m/HVHEuBRZAQAJ?utm_medium=email&utm_source=footer

'Charcoal Chen' via Android CameraX Discussion Group <camerax-d...@android.com> 於 2023年2月13日 週一 上午11:23寫道:
--
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/697ad1e9-74dc-46bd-b875-ec63c7a386fdn%40android.com.
Reply all
Reply to author
Forward
0 new messages