I use the following versions of CameraX dependencies:
implementation "androidx.camera:camera-camera2:1.0.0-beta04"
implementation "androidx.camera:camera-view:1.0.0-alpha11"
implementation "androidx.camera:camera-extensions:1.0.0-alpha11"
implementation "androidx.camera:camera-lifecycle:1.0.0-beta04"
And try to encode output video from the camera into H264 using android MediaCodec
and decode it back and show the result video on a SurfaceView, everything works fine except that the video on a SurfaceView is conterclockwise rotated by 90 degrees in portrait mode and has no rotation in landscape mode.
My code of capturing video from the camera looks like this:
private lateinit var yData: ByteArray //
private lateinit var uData: ByteArray // store YUV data from camera
private lateinit var vData: ByteArray //
imageAnalysis = ImageAnalysis.Builder().setBackpressureStrategy ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST).build()
imageAnalysis.setAnalyzer(executor, ImageAnalysis.Analyzer { imageProxy ->
if (imageProxy.image == null || imageProxy.planes.size < 3) return@Analyzer
for (i in 0 until 3) {
val plane = imageProxy.image!!.planes[i]
val bytes = ByteArray(plane.buffer.remaining())
plane.buffer.get(bytes)
when(i) {
0 -> yData = bytes
1 -> uData = bytes
2 -> vData = bytes
}
}
imageProxy.close()
// send yData, uData, vData to encoder ...
})
To solve the problem I tried to use: imageAnalysis.targetRotation = Surface.ROTATION_0
and other values but it doesn't work at all - the decoded video still rotated.
Also I tried to rotate YUV data that I get from the camera using this answer and this time I succeeded in rotating my video, BUT the colors of the video suddenly gets dark and I have no idea why.. but if I don't rotate my video the colors are perfect.
So my question is: how can I correctly rotate my video without colors distortion? The way when I need to rotate my SurfaceView isn't fit for me. And I repeat - the problem exactly with decoded video that I first get from camera and encode to H264 and not with preview from camera, the preview from camera works fine.
Here my source video without use of any ways that I mentioned above.
Finally I found a solution - I made an android library, based on Google's libyuv library for easy and fast scaling, rotating and mirroring frames from android Camera2 or CameraX api.