Preview Android camera with color filter

I have a class CameraPreview. It expands SurfaceViewand implements SurfaceHolder.Callbackand PreviewCallback. I would like to change the image that the camera sends to SurfaceView. This should be done in the following part of the code:

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    Log.d(TAG, "onPreviewFrame");
    Canvas canvas = mHolder.lockCanvas();
    synchronized (mHolder) {
        Paint p = new Paint();
        p.setColor(Color.RED);
        canvas.drawRect(0, 0, 100, 200, p);
        mHolder.unlockCanvasAndPost(canvas);
    }   
}

But I get the following exception:

Exception locking surface java.lang.IllegalArgumentException at android.view.Surface.nativeLockCanvas(Native Method) at android.view.Surface.lockCanvas(Surface.java:243)

Google says the error is due to an unlocked canvas, but I always leave it locked. How can I draw an image on SurfaceView?

I am creating a function that applies a color filter with an existing image, I hope this code is correct.

@Override
protected void overDraw(Canvas canvas) {        
    Paint p = new Paint();
    ColorMatrixColorFilter fl = new ColorMatrixColorFilter(new ColorMatrix(filter));
    p.setColorFilter(fl);
    canvas.drawPaint(p);
    Log.d(TAG, "overDraw");
}

float[] filter = new float[]
{ 
    //r, g, b, a, k
    0.33,0.33,0.33,0,0, //r
    0.33,0.33,0.33,0,0, //g
    0.33,0.33,0.33,0,0, //b
    0,0,0,1,0,  //a
};
+4
source share
1 answer

, SurfaceView. onPreviewFrame() . , . , TextureView OpenGL , . , onPreviewFrame() .

0

All Articles