问题描述
我试图捕捉摄像头preVIEW图片及它的一些图纸。问题是,我从相机preVIEW大约只有3-4 fps的绘画,有一半的帧处理时间接收和解码NV21形象,转换为位图。我有一个code做这个任务,这是我的另一个问题堆栈中。它似乎没有要快,但我不知道,如何优化它。大约需要100-150毫秒。三星注3,图像尺寸1920×1080。我怎样才能使它工作得更快?
I am trying to capture images from camera preview and do some drawing on it. The problem is, I have only about 3-4 fps of drawing, and half of the frame processing time is receiving and decoding NV21 image from camera preview and converting to bitmap. I have a code to do this task, which I found on another stack question. It does not seem to be fast, but I do not know, how to optimize it. It takes about 100-150 ms. on Samsung Note 3, image size 1920x1080. How can I make it work faster?
code:
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.set previewFormat设置为RGB_565,但不能指定相机PARAMS相机,我读过也是NV21是唯一可用的格式。我不知道这件事,是否有可能找到这些格式更改的解决方案。
The final format of image has to be bitmap, so I could then do processing on it. I've tried to set Camera.Parameters.setPreviewFormat to RGB_565, but could not assign camera params to camera, I've read also that NV21 is the only available format. I am not sure about that, whether it is possible to find solution in these format changes.
感谢您提前。
推荐答案
感谢您,亚历克斯·科恩,帮我做这做皈依更快。我实现了你的建议的方法(RenderScript内部函数)。这code,与RenderScript内在制成,转换YUV图像为位图一下〜5倍的速度。 previous code拿了100-150毫秒。三星注3,这需要15-30左右。 如果有人需要做同样的任务,有code:
Thank you, Alex Cohn, for helping me to do make this convertion faster. I implemented your suggested methods (RenderScript intrinsics). This code, made with RenderScript intrinsics, converts YUV image to bitmap about ~5 times faster. Previous code took 100-150 ms. on Samsung Note 3, this takes 15-30 or so. If someone needs to do the same task, there is the code:
这些将被使用:
private RenderScript rs;
private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
private Type.Builder yuvType, rgbaType;
private Allocation in, out;
在上创建函数初始化我..:
In on create function I initialize..:
rs = RenderScript.create(this);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
和整体上previewFrame看起来像这样(我在这里接收和转换图像):
And the whole onPreviewFrame looks like this (here I receive and convert the image):
if (yuvType == null)
{
yuvType = new Type.Builder(rs, Element.U8(rs)).setX(dataLength);
in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(prevSizeW).setY(prevSizeH);
out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
}
in.copyFrom(data);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
Bitmap bmpout = Bitmap.createBitmap(prevSizeW, prevSizeH, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout);
这篇关于YUV(NV21)图像皈依位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!