问题
I'm trying to record video by checking each frame of camera preview to bitmap with quality ARGB_8888. As it required 4 channel, Created IplImage with channel 4 too. Now the output have two major problems :
1) Bitmap that created from IplImage have grayscale. even if I have converted it from BGR2RGBA. 2) 4 channel IplImage gave me bitmap (divided in 4 parts) with same screen.
Let me put my code over here.
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (yuvIplimage != null && recording) {
videoTimestamp = 1000 * (System.currentTimeMillis() - startTime);
// Where imagewidth = 640 and imageheight = 480 (As per camera preview size)
// Create the yuvIplimage
IplImage yuvimage = IplImage.create(imageWidth, imageHeight * 3 / 2, IPL_DEPTH_8U, 2);
yuvimage.getByteBuffer().put(data);
IplImage rgbimage = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_8U, 3);
opencv_imgproc.cvCvtColor(yuvimage, rgbimage, opencv_imgproc.CV_YUV2BGR_NV21);
Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight,Bitmap.Config.RGB_565);
bitmap.copyPixelsFromBuffer(rgbimage.getByteBuffer());
//Save file to SDCARD------------
File file = new File(Environment.getExternalStorageDirectory(),
"rgbbitmap.png");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// mybitmap.recycle();
} catch (Exception e) { // TODO
}
try {
// Get the correct time
recorder.setTimestamp(videoTimestamp);
// Record the image into FFmpegFrameRecorder
recorder.record(yuvimage);
} catch (FFmpegFrameRecorder.Exception e) {
Log.v(LOG_TAG, e.getMessage());
e.printStackTrace();
}
}
}
As well as, find the below bitmap image as I'm getting as output with 4 parts of same frame.
![](https://www.eimg.top/images/2020/03/22/d805c4c3e70894f32c0298d45cad7e77.png)
What's wrong with my code or what's missing by me? Let me know your best suggestions.
Thanks,
回答1:
IplImage yuvImage = IplImage.create(width, height * 3 / 2, IPL_DEPTH_8U, 1);
yuvImage.getByteBuffer().put(data);
IplImage bgrImage = IplImage.create(width, height, IPL_DEPTH_8U, 3);
cvCvtColor(yuvImage, bgrImage, CV_YUV2BGR_NV21);
cvSaveImage("/mnt/sdcard/result.jpg", bgrImage);
来源:https://stackoverflow.com/questions/23149867/4-channel-iplimage-javacv-to-android-bitmap