我想制作自己的 SurfaceView 并向那里发送我从 onPreviewFrame(byte[] data, Camera camera) 方法获得的帧。为此,我需要将帧从 Yuv 转换为 ARGB,并将它们绘制到 Canvas 到我的 SurfaceView。
这是我的 onPreviewFrame:
public void onPreviewFrame(byte[] data, Camera camera) {
if (camera != null) {
camera.addCallbackBuffer(data);
}
// using RenderScript
Bitmap bitmap = RenderScriptFilter.convertYuvToRgbIntrinsic(rs, data, PREVIEW_WIDTH, PREVIEW_HEIGHT);
if (mFilterSurfaceView != null) {
SurfaceHolder holder = mFilterSurfaceView.getHolder();
Canvas canvas = holder.lockCanvas();
// draw bitmap to the canvas
canvas.drawBitmap(bitmap, 0, 0, mPaint);
holder.unlockCanvasAndPost(canvas);
mFrames++;
System.gc();
}
}
};
这是 convertYuvToRgbIntrinsic 方法:
public static Bitmap convertYuvToRgbIntrinsic(RenderScript rs, byte[] data, int w, int h) {
int imageWidth = w ;
int imageHeight = h ;
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));
// Create the input allocation memory for Renderscript to work with
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs))
.setX(imageWidth)
.setY(imageHeight)
.setYuvFormat(android.graphics.ImageFormat.NV21);
Allocation aIn = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
// Set the YUV frame data into the input allocation
aIn.copyFrom(data);
// Create the output allocation
Type.Builder rgbType = new Type.Builder(rs, Element.RGBA_8888(rs))
.setX(imageWidth)
.setY(imageHeight);
Allocation aOut = Allocation.createTyped(rs, rgbType.create(), Allocation.USAGE_SCRIPT);
yuvToRgbIntrinsic.setInput(aIn);
// Run the script for every pixel on the input allocation and put the result in aOut
yuvToRgbIntrinsic.forEach(aOut);
// Create an output bitmap and copy the result into that bitmap
Bitmap outBitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
aOut.copyTo(outBitmap);
return outBitmap ;
}
我认为问题出现是因为 System.gc() 方法。因为当我尝试使用 640x480 时一切正常,但使用 1280x720 时出现一些错误:
12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp E/RenderScript:rsAssert 失败:cmd->cmdID < (sizeof(gPlaybackFuncs) / sizeof(void *)),在 frameworks/rs/rsThreadIO.cpp在 156 12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp E/RenderScript:playCoreCommands 错误 con 0x78fb79a0,cmd 2072915616 12-30 13:06:59.063 18034-18178/youten. : 0x2fa27a84 (code=1) 处的致命信号 11 (SIGSEGV),线程 18178 (o.y2ndkyuv420sp)
我知道 System.gc() 是一种不好的做法,但是在这里如何分配内存呢?bitmap.recycle(); 位图=空;没有解决问题。