Skip to content

Making JavaCamera2View be consistent with JavaCameraView - #13840

Merged
opencv-pushbot merged 1 commit into
opencv:3.4from
jaewoosong:jaewoosong-patch-1
Feb 26, 2019
Merged

opencv-pushbot merged 1 commit into
opencv:3.4from
jaewoosong:jaewoosong-patch-1

Conversation

@jaewoosong

@jaewoosong jaewoosong commented Feb 15, 2019 •

Copy link
Copy Markdown
Contributor

resolves #11325

Bug fix of Interrupted while setCameraPreviewSize and width and height must be > 0 which is related to JavaCamera2View

This pullrequest changes

org.opencv.android.JavaCamera2View crashes on certain phones such as Samsung Galaxy S7 and Alcatel A5A. (On contrary, JavaCameraView works fine.) It is because CameraBridgeViewBase.CAMERA_ID_BACK and CameraBridgeViewBase.CAMERA_ID_FRONT do not match correct values. This patch introduces two new final int variables to correctly detect cameras.

Bug which happens with old code

java.lang.RuntimeException: Interrupted while setCameraPreviewSize.
        at org.opencv.android.JavaCamera2View.connectCamera(JavaCamera2View.java:333)
        at org.opencv.android.CameraBridgeViewBase.onEnterStartedState(CameraBridgeViewBase.java:360)
        at org.opencv.android.CameraBridgeViewBase.processEnterState(CameraBridgeViewBase.java:321)
        at org.opencv.android.CameraBridgeViewBase.checkCurrentState(CameraBridgeViewBase.java:313)
        at org.opencv.android.CameraBridgeViewBase.surfaceChanged(CameraBridgeViewBase.java:198)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:725)
        at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:180)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2553)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
        at android.view.Choreographer.doCallbacks(Choreographer.java:709)
        at android.view.Choreographer.doFrame(Choreographer.java:644)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6247)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
     Caused by: java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:877)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:823)
        at org.opencv.android.CameraBridgeViewBase.AllocateCache(CameraBridgeViewBase.java:457)
        at org.opencv.android.JavaCamera2View.connectCamera(JavaCamera2View.java:322)
        at org.opencv.android.CameraBridgeViewBase.onEnterStartedState(CameraBridgeViewBase.java:360) 
        at org.opencv.android.CameraBridgeViewBase.processEnterState(CameraBridgeViewBase.java:321) 
        at org.opencv.android.CameraBridgeViewBase.checkCurrentState(CameraBridgeViewBase.java:313) 
        at org.opencv.android.CameraBridgeViewBase.surfaceChanged(CameraBridgeViewBase.java:198) 
        at android.view.SurfaceView.updateWindow(SurfaceView.java:725) 
        at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:180) 
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944) 
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2553) 
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462) 
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965) 
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907) 
        at android.view.Choreographer.doCallbacks(Choreographer.java:709) 
        at android.view.Choreographer.doFrame(Choreographer.java:644) 
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893) 
        at android.os.Handler.handleCallback(Handler.java:836) 
        at android.os.Handler.dispatchMessage(Handler.java:103) 
        at android.os.Looper.loop(Looper.java:203) 
        at android.app.ActivityThread.main(ActivityThread.java:6247) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 

@jaewoosong jaewoosong changed the title Bug fix: JavaCamera2View Bug fix: JavaCamera2View cannot find cameras Feb 15, 2019
@alalek

alalek commented Feb 15, 2019

Copy link
Copy Markdown
Member

CameraBridgeViewBase.CAMERA_ID_BACK and CameraBridgeViewBase.CAMERA_ID_FRONT do not match correct values.

There values are OpenCV-specific (not mapped into any Android values).
How they do not match?


How many cameras are available? (check this via debugger breakpoint)
Are camera rights properly requested?

@jaewoosong

Copy link
Copy Markdown
Contributor Author

Hi! There were two cameras on testing phones. One on the front and the other on the back. I also confirmed that the two cameras are recognized correctly by using the debugger. The permission was also correctly granted. If I change JavaCamera2View to JavaCameraView, other things kept exactly same, then everything works correctly. But if I change to JavaCamera2View then the error occurs.

@alalek

alalek commented Feb 19, 2019

Copy link
Copy Markdown
Member

What value of "mCameraIndex" is showed by debugger?

@jaewoosong

Copy link
Copy Markdown
Contributor Author

mCameraIndex is 0 in debugger.

@jaewoosong

Copy link
Copy Markdown
Contributor Author

I think I found the cause of the problem.

android.hardware.Camera takes integer inputs between 0 and getNumberOfCameras()-1 (API link). Let's consider the following example code.

private CameraBridgeViewBase mOpenCvCameraView;
mOpenCvCameraView.setCameraIndex(0);

If I use JavaCameraView, I will eventually reach initializeCamera() and arrive at line 128: mCamera = Camera.open(localCameraIndex); Then android.hardware.camera will successfully open the camera.

On the other hand, if I use JavaCamera2View the code will arrive at initializeCamera() at line 86 but then will fail to find a camera.

So it seems the difference between Camera and Camera2 APIs causes the problem. With my pull request, the users will be able to make a smooth transition between JavaCameraView and JavaCamera2View easily.

@alalek

alalek commented Feb 20, 2019

Copy link
Copy Markdown
Member

mOpenCvCameraView.setCameraIndex(0);

There are special OpenCV-specific values for this parameter:
https://github.com/opencv/opencv/blob/4.0.1/modules/java/generator/android/java/org/opencv/android/CameraBridgeViewBase.java#L53-L55

Looks like, current implementation of JavaCamera2View supports these values only, without supporting of zero-based device-specific indexes.

@jaewoosong

Copy link
Copy Markdown
Contributor Author

Looks like, current implementation of JavaCamera2View supports these values only, without supporting of zero-based device-specific indexes.

Yes. JavaCameraView and android.hardware.camera both support zero-based device-specific indexes but JavaCamera2View does not. So JavaCameraView and JavaCamera2View behave differently. I thought it would be good if they behave in same way.

@jaewoosong

Copy link
Copy Markdown
Contributor Author

I made a new commit. Compared to the previous commit, this new commit makes logic of JavaCameraView and JavaCamera2View consistent. (on contrast, previous commit was just a bypass)

There are three important final variables:

  • CAMERA_ID_ANY = -1;
  • CAMERA_ID_BACK = 99;
  • CAMERA_ID_FRONT = 98;

The logic of JavaCameraView's initializeCamera() (link) is four steps:

  1. Condition block of if (mCameraIndex == CAMERA_ID_ANY)
  2. Condition block of if (mCameraIndex == CAMERA_ID_BACK)
  3. Condition block of if (mCameraIndex == CAMERA_ID_FRONT)
  4. If all the above does not work, then try Camera.open(localCameraIndex) with localCameraIndex given by the user.

On contrary, the logic of JavaCamera2View's initializeCamera() (link) is missing the last step:

  1. Condition block of if (mCameraIndex == CameraBridgeViewBase.CAMERA_ID_ANY)
  2. Condition block of if ((mCameraIndex == CameraBridgeViewBase.CAMERA_ID_BACK && characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) || (mCameraIndex == CameraBridgeViewBase.CAMERA_ID_FRONT && characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) )

So in this new commit I added a new step to JavaCamera2View:

else { // this corresponds to the last step of JavaCameraView's logic
    Log.i(LOGTAG, "Trying to open camera with new open(" + mCameraIndex + ")");
    mCameraID = Integer.toString(mCameraIndex);
    manager.openCamera(mCameraID, mStateCallback, mBackgroundHandler);
}

This new commit makes the logic of JavaCameraView and JavaCamera2View consistent. It supports more diverse inputs while doing no harm to the existing code.

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change makes sense.

Please squash commits into one (for example, git rebase -i HEAD~3)
(git history with changes is not needed)

@jaewoosong jaewoosong changed the title Bug fix: JavaCamera2View cannot find cameras Making JavaCamera2View be consistent with JavaCameraView Feb 22, 2019
@jaewoosong

Copy link
Copy Markdown
Contributor Author

Added an error-checking block and squashed the commits into one!

@jaewoosong

Copy link
Copy Markdown
Contributor Author

The new code (which corresponds to the resolved conversation) is committed and all previous commits are squashed into one. Thanks for the hard work of OpenCV maintainers!

@alalek

alalek commented Feb 25, 2019

Copy link
Copy Markdown
Member

Thank you!

This patch should go into 3.4 branch first. We will merge changes from 3.4 into master regularly (weekly/bi-weekly).

So, please:

  • change "base" branch of this PR: master => 3.4 (use "Edit" button near PR title)
  • rebase your commits from master onto 3.4 branch. For example:
    git rebase -i --onto upstream/3.4 upstream/master
    (check list of your commits, save and quit (Esc + "wq" + Enter)
    where upstream is configured by following this GitHub guide and fetched (git fetch upstream).
  • push rebased commits into source branch of your fork (with --force option)

Note: no needs to re-open PR, apply changes "inplace".

@jaewoosong
jaewoosong changed the base branch from master to 3.4 February 26, 2019 05:53
@jaewoosong

Copy link
Copy Markdown
Contributor Author

Hi! I rebased the commit according to your explanation.

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@opencv-pushbot
opencv-pushbot merged commit 225bdc2 into opencv:3.4 Feb 26, 2019
@alalek alalek mentioned this pull request Feb 26, 2019
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants