【发布时间】:2015-12-24 12:57:26
【问题描述】:
我想制作自己的 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
我知道 System.gc() 是一种不好的做法,但是在这里如何分配内存呢? bitmap.recycle();位图=空;没有解决问题。
【问题讨论】:
标签: bitmap garbage-collection android-camera android-renderscript