Android camera preview with color filter… here is a solution to the problem.
Android camera preview with color filter
I have a CameraPreview
class. It extends SurfaceView
and implements SurfaceHolder.Callback and PreviewCallback.
I want to change the image that the camera sends
to the SurfaceView
. It 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:
Abnormal lock surface
java.lang.IllegalArgumentException
In android.view.Surface.nativeLockCanvas( native method)
In android.view.Surface.lockCanvas (Surface.java:243).
Google says the error happens because Canvas is not locked, but I always keep it locked.
How do I draw an image on SurfaceView
?
I
created a function that applies a color filter to an existing image, and 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
};