问题
I copied sample code from opencv4android(face detection). I passed the NDK part. And run the app on phone. However SurfaceView only shows camera preview. It seems whatever done in OnCameraFrame is not affecting the Canvas on the SurfaceView. So I trace into their SDK. the problem is in deliverAndDrawFrame function. Does anybody had the same problem before?
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
Log.d(TAG, "mStretch value: " + mScale);
if (mScale != 0) {
canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2),
(int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2),
(int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()),
(int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null);
} else {
canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2,
(canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(),
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null);
}
if (mFpsMeter != null) {
mFpsMeter.measure();
mFpsMeter.draw(canvas, 20, 30);
}
getHolder().unlockCanvasAndPost(canvas);
}
Error:
10-09 21:57:47.485 28018-28088/com.example.tim.r3dmobileclient E/SurfaceHolder﹕ Exception locking surface
java.lang.IllegalArgumentException
at android.view.Surface.nativeLockCanvas(Native Method)
at android.view.Surface.lockCanvas(Surface.java:253)
at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:848)
at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:816)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:412)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:348)
at java.lang.Thread.run(Thread.java:841)
回答1:
Have you tried to check if the surface is valid, before trying to lock the canvas? If all this code is in a loop try putting this before locking the canvas:
if(!getHolder().getSurface().isValid()){
continue;
}
If it is not inside a loop, try putting all the code (from locking the canvas and below) inside this if statement:
if(getHolder().getSurface().isValid()){
//code goes here
}
回答2:
I had the same problem, to solve it -believe or not- just comment out this part! It is trying to redraw what is being drawn when you call mCamera.startPreview();
来源:https://stackoverflow.com/questions/33040448/lockcanvas-keeps-throwing-exceptions