【发布时间】:2015-05-16 20:45:43
【问题描述】:
我正在尝试通过将相机预览的每一帧检查为具有 ARGB_8888 质量的位图来录制视频。由于它需要 4 个通道,因此也使用通道 4 创建了 IplImage。现在输出有两个主要问题:
1) 从 IplImage 创建的位图有灰度。即使我已经从 BGR2RGBA 转换它。 2)4通道IplImage给了我相同屏幕的位图(分为4部分)。
让我把我的代码放在这里。
@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();
}
}
}
此外,找到下面的位图图像,因为我得到了相同帧的 4 个部分的输出。
我的代码有什么问题或我缺少什么?让我知道您的最佳建议。
谢谢,
【问题讨论】:
-
相机预览数据通常是 YUV 格式,因此我们需要将其转换为 RGB,类似于我在此处的答案中显示的内容:stackoverflow.com/a/12870556/523744
-
@SamuelAudet:嗨,塞缪尔,感谢您的回复。但它不起作用。对于您的解决方案,我必须使用配置 RGB_565 的位图对象,因为我们已经使用通道 3 创建了 IPLImage。N 它正在提供输出图像,因为我已经更新了。
-
@SamuelAudet:请检查我更新的代码和图像。
-
@SamuelAudet:我必须将位图强制使用到 RGB_565,我不能使用 ARGB_8888。但我认为我们可以只使用 ARGB_8888 来获取位图的质量。
-
cvCvtColor()也支持CV_BGR2BGR565之类的转换,所以试试吧。
标签: android opencv bitmap javacv iplimage