Skip to content Skip to sidebar Skip to footer

Renderscript Scriptintrinsicyuvtorgb And Bitmap Allocation

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

Solution 1:

I don't know what triggers your error, and how this could be related to garbage collector. At any rate, 1280x720 bitmaps can exhaust your memory quickly.

It is counter-productive to recreate all renderscript allocations for each frame. The whole point is to make all preparations once, and reuse.

Attached find the utility function I use:

public Bitmap convertYuvImageToBitmap(Context context, YuvImage yuvImage) {

    int w = yuvImage.getWidth();
    int h = yuvImage.getHeight();

    if (rs == null) {
        // once
        rs = RenderScript.create(context);
        yuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }

    if (yuvAllocation == null || yuvAllocation.getBytesSize() < yuvImage.getYuvData().length) {
        yuvType = new Type.Builder(rs, Element.U8(rs)).setX(yuvImage.getYuvData().length);
        yuvAllocation = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
        Log.w(TAG, "allocate in " + yuvAllocation.getBytesSize() + " " + w + "x" + h);
    }

    if (rgbaAllocation == null || 
            rgbaAllocation.getBytesSize() < rgbaAllocation.getElement().getBytesSize()*w*h) {
        rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(w).setY(h);
        rgbaAllocation = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
        Log.w(TAG, "allocate out " + rgbaAllocation.getBytesSize() + " " + w + "x" + h);
    }

    yuvAllocation.copyFrom(yuvImage.getYuvData());

    yuvToRgb.setInput(yuvAllocation);
    yuvToRgb.forEach(rgbaAllocation);

    if (bmpout == null) {
        bmpout = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    }
    rgbaAllocation.copyTo(bmpout);
    return bmpout;
}

Post a Comment for "Renderscript Scriptintrinsicyuvtorgb And Bitmap Allocation"