<分区>
我正在尝试从相机预览中捕获图像并在其上进行一些绘图。问题是,我只有大约 3-4 fps 的绘图,一半的帧处理时间是从相机预览接收和解码 NV21 图像并转换为位图。我有一个代码来完成这个任务,我在另一个堆栈问题上找到了它。好像不快,不知道怎么优化。在三星 Note 3 上大约需要 100-150 毫秒,图像大小为 1920x1080。我怎样才能让它工作得更快?
代码:
public Bitmap curFrameImage(byte[] data, Camera camera)
{
Camera.Parameters parameters = camera.getParameters();
int imageFormat = parameters.getPreviewFormat();
if (imageFormat == ImageFormat.NV21)
{
YuvImage img = new YuvImage(data, ImageFormat.NV21, prevSizeW, prevSizeH, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
img.compressToJpeg(new android.graphics.Rect(0, 0, img.getWidth(), img.getHeight()), 50, out);
byte[] imageBytes = out.toByteArray();
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}
else
{
Log.i(TAG, "Preview image not NV21");
return null;
}
}
图像的最终格式必须是位图,这样我才能对其进行处理。我尝试将 Camera.Parameters.setPreviewFormat 设置为 RGB_565,但无法将相机参数分配给相机,我还读到 NV21 是唯一可用的格式。我不确定是否可以在这些格式更改中找到解决方案。
提前谢谢你。