On Android , I use MediaCodec to create mp4 video file from captured images.
I am getting the OpenCV Mat data using
Image i = reader.acquireLatestImage();
ByteBuffer byteBuffer = i.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
Mat mImageGrab = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); <= bytes from Image object
I have checked and the bitmap images are correctly showing(with some colors wrong translation)
this is the code that tests the Bitmaps:
Mat nv21 = new Mat();
Imgproc.cvtColor(mImageGrab, nv21, Imgproc.COLOR_RGB2YUV_I420);<= NV21 is mat was created
Bitmap bmp = Bitmap.createBitmap(mImageGrab.cols(), mImageGrab.rows(), Bitmap.Config.ARGB_8888);
Mat bmpMat = new Mat();
Imgproc.cvtColor(nv21,bmpMat, Imgproc.COLOR_YUV420sp2RGB);<= transfer NV21 to Bitmap
Utils.matToBitmap(bmpMat, bmp);<= great bitmap result
However the MediaCodec mp4 expects byte array of NV21 image and not bitmap. How can I transform the Mat to NV21? or the bitmap to NV21? I want to perform some image processing to the images before sending those images to the MediaCodec.
I can also use the "encodeYUV420SP" function that was mentioned here Link to encode NV21 but it is too slow for video.
other option - render scripts - support for those supposes to be deprecated - so - I prefer not to. But I am ready to try : bitmap to NV21 render script that is androidx compatible.()
from Android : how to convert back openCV Mat to byte array NV21?
No comments:
Post a Comment