我从opencv4android(面部检测)复制了示例代码。我通过了NDK部分。并在手机上运行应用程序。但SurfaceView仅显示相机预览。似乎OnCameraFrame中的任何操作都不会影响SurfaceView上的Canvas。所以我追溯到他们的SDK。问题出在deliverAndDrawFrame函数中。以前有人遇到过同样的问题吗?
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);
}
错误:
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)
答案 0 :(得分:2)
在尝试锁定画布之前,您是否尝试检查表面是否有效? 如果所有这些代码都在一个循环中,请在锁定画布之前将其放入:
if(!getHolder().getSurface().isValid()){
continue;
}
如果它不在循环中,请尝试将所有代码(从锁定画布及其下方)放入此if语句中:
if(getHolder().getSurface().isValid()){
//code goes here
}
答案 1 :(得分:1)
我遇到了相同的问题,要解决它,请相信或不注释,只需注释掉这部分即可! 当您调用mCamera.startPreview();
时,它将尝试重绘正在绘制的内容。