猿问

从Android中的视频图像中获取帧

我已经实现了一个简单的应用程序,在屏幕上显示相机图片。我现在要做的是抓取一个帧并将其作为位图处理。从我能找到的东西到这一点,这不是一件容易的事。


我已经尝试使用onPreviewFrame方法,您将当前帧作为字节数组,并尝试使用BitmapFactory类解码它,但它返回null。帧的格式是无头YUV,可以转换为位图,但在手机上需要太长时间。另外我已经读过onPreviewFrame方法在运行时有约束,如果它需要太长时间,应用程序可能会崩溃。


那么这样做的正确方法是什么?


12345678_0001
浏览 1407回答 3
3回答

慕码人8056858

在API 17+中,您可以使用'ScriptIntrinsicYuvToRGB'RenderScript从NV21转换为RGBA888。这使您可以轻松处理预览帧而无需手动编码/解码帧:@Override public void onPreviewFrame(byte[] data, Camera camera) {    Bitmap bitmap = Bitmap.createBitmap(r.width(), r.height(), Bitmap.Config.ARGB_8888);    Allocation bmData = renderScriptNV21ToRGBA888(        mContext,        r.width(),        r.height(),        data);    bmData.copyTo(bitmap);}public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {  RenderScript rs = RenderScript.create(context);  ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));  Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);  Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);  Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);  Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);  in.copyFrom(nv21);  yuvToRgbIntrinsic.setInput(in);  yuvToRgbIntrinsic.forEach(out);  return out;}

哆啦的时光机

我实际上尝试了前面给出的代码,发现Colorvalues不准确。我通过预览和直接返回JPEG数组的camera.takePicture检查了它。而且颜色非常不同。经过一点点搜索后,我发现了另一个将PreviewImage从YCrCb转换为RGB的例子:static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {&nbsp; &nbsp; final int frameSize = width * height;&nbsp; &nbsp; for (int j = 0, yp = 0; j < height; j++) {&nbsp; &nbsp; &nbsp; &nbsp; int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < width; i++, yp++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int y = (0xff & ((int) yuv420sp[yp])) - 16;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (y < 0) y = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((i & 1) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = (0xff & yuv420sp[uvp++]) - 128;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; u = (0xff & yuv420sp[uvp++]) - 128;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int y1192 = 1192 * y;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int r = (y1192 + 1634 * v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int g = (y1192 - 833 * v - 400 * u);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int b = (y1192 + 2066 * u);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (r < 0) r = 0; else if (r > 262143) r = 262143;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (g < 0) g = 0; else if (g > 262143) g = 262143;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (b < 0) b = 0; else if (b > 262143) b = 262143;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}由this和takePicture()给出的颜色值完全匹配。我想我应该在这里张贴。 这是我从中获取此代码的地方。 希望这可以帮助。
随时随地看视频慕课网APP

相关分类

Java
Android
我要回答