5

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)
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Tim Hsu
  • 402
  • 5
  • 19

2 Answers2

2

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
}
  • the code i presented above is in their sdk. I've checked that surfaceCreated method run before lockConvas – Tim Hsu Oct 10 '15 at 06:52
  • I added the isValid. and it pass. problem still remains – Tim Hsu Oct 10 '15 at 06:54
  • I wonder we can't lock the surface that is set in camera preview stage – Tim Hsu Oct 10 '15 at 07:03
  • @TimHsu did you ever find a solution to this? Encountering the same problem –  Oct 12 '16 at 16:43
  • @user3186023 yap but i almost forget what i did. The only thing i remember is that i modified some piece code in SDK(maybe around the function i list above). It was actually an ugly fix. – Tim Hsu Oct 14 '16 at 07:29
  • I managed to fix this problem. However I'm having problems with using MediaRecorder with Opencv to record the screen, because JavaCameraView already has an instance of the camera open. Do you have any suggestions for this? –  Oct 14 '16 at 07:30
  • 2
    Can someone share the fix? – Federico Ponzi Jun 12 '18 at 14:21
1

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();

Sameh Yassin
  • 424
  • 3
  • 9
  • I solved the problem but I forget about the details, very sorry. Only thing that I sill remember is that I modified the SDK code. – Tim Hsu Jan 20 '19 at 06:07