I want to make my own SurfaceView and send there a frames which I've obtained from onPreviewFrame(byte[] data, Camera camera) method. To do it, I need to conver frames from Yuv to ARGB and to draw them to a Canvas to my SurfaceView.
Here is my 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();
}
}
};
Here is convertYuvToRgbIntrinsic method:
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 ;
}
I think the problem appears because of System.gc() method. Because when I try with 640x480 all okay, but with 1280x720 I obtain some errors:
12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp E/RenderScript: rsAssert failed: cmd->cmdID < (sizeof(gPlaybackFuncs) / sizeof(void *)), in frameworks/rs/rsThreadIO.cpp at 156 12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp E/RenderScript: playCoreCommands error con 0x78fb79a0, cmd 2072915616 12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp A/libc: Fatal signal 11 (SIGSEGV) at 0x2fa27a84 (code=1), thread 18178 (o.y2ndkyuv420sp)
I understood that System.gc() is a bad practice, but what to do here to allocate a memory? bitmap.recycle(); bitmap = null; didn't fixes the problem.