JimmyPun610 / BarcodeScanner.Mobile

Barcode Scanner using GoogleVision API for Xamarin Form, Maui
MIT License
348 stars 101 forks source link

Camera crashing in Android 11 #77

Closed abhinashpati closed 2 years ago

abhinashpati commented 3 years ago

Stack Trace:

Java.Lang.RuntimeException: set display orientation failed
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0008e] in <1959115d56f8444789986cf39185638c>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0001f] in <1959115d56f8444789986cf39185638c>:0 
  at Android.Hardware.Camera.SetDisplayOrientation (System.Int32 degrees) [0x00018] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Android.Hardware.Camera.cs:5039 
  at GoogleVisionBarCodeScanner.CameraPreview.SetOrientation () [0x0003d] in <929ca9ff04df47ad86680b7a3455a351>:0 
  at GoogleVisionBarCodeScanner.CameraPreview.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00043] in <929ca9ff04df47ad86680b7a3455a351>:0 
  at Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this, System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00008] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Android.Views.ViewGroup.cs:3873 
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.43(intptr,intptr,bool,int,int,int,int)
  --- End of managed Java.Lang.RuntimeException stack trace ---
java.lang.RuntimeException: set display orientation failed
    at android.hardware.Camera.setDisplayOrientation(Native Method)
    at crc6406d57bc67a4dd4fc.CameraPreview.n_onLayout(Native Method)
    at crc6406d57bc67a4dd4fc.CameraPreview.onLayout(CameraPreview.java:62)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at crc643f46942d9dd1fff9.ViewRenderer_2.n_onLayout(Native Method)
    at crc643f46942d9dd1fff9.ViewRenderer_2.onLayout(ViewRenderer_2.java:47)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
    at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout(Native Method)
    at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout(Platform_DefaultRenderer.java:72)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
    at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout(Native Method)
    at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout(Platform_DefaultRenderer.java:72)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
    at crc643f46942d9dd1fff9.PageRenderer.n_onLayout(Native Method)
    at crc643f46942d9dd1fff9.PageRenderer.onLayout(PageRenderer.java:72)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
    at crc64720bb2db43a66fe9.Platform_ModalContainer.n_onLayout(Native Method)
    at crc64720bb2db43a66fe9.Platform_ModalContainer.onLayout(Platform_ModalContainer.java:53)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at crc643f46942d9dd1fff9.PlatformRenderer.n_onLayout(Native Method)
    at crc643f46942d9dd1fff9.PlatformRenderer.onLayout(PlatformRenderer.java:63)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:1225)
    at android.view.View.layout(View.java:24475)
    at android.view.ViewGroup.layout(ViewGroup.java:7383)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4260)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3695)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2618)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9971)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010)
    at android.view.Choreographer.doCallbacks(Choreographer.java:809)
    at android.view.Choreographer.doFrame(Choreographer.java:744)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8587)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

I have an app that uses Prism that has a TabbedPage. From the tabpage I'm opening a page with the BarcodeScanner and then from there I'm opening another page and from there I'm accessing MediaPicker.CaptureAsync to open Camera app for capturing a picture. When I'm returning to the page after clicking the picture, I'm getting this crash

petrdrabek commented 3 years ago

Same issue here - only on pre-version 6.0.0.2. Why is there no code pushed to repository for version 6.0.0.2? Only nuget package available.

Issue ocures also on sample android app if you use nuget package pre-6.0.0.2 from nuget.org.

abhinashpati commented 3 years ago

Well I'm using 5.0.0.8 and i'm getting this crash. So i can confidently say it's an existing issue of the library and not specific to 6.0.02 pre.

JimmyPun610 commented 3 years ago

Please provide a sample project for debugging, I didn't use Prism.

abhinashpati commented 3 years ago

Here's a sample. I have replicated my project structure in this sample project as well. A Think to note that the issue is not easy to reproduce. It doesn't happens always. It happens out of nowhere. You might have to go through the flow multiple times in order to reproduce it.

https://github.com/abhinashpati/BarcodeSample

@petrdrabek If you have any better and easy way to reproduce this issue, please let @JimmyPun610 know. And as @petrdrabek mentioned that this issue also occurs in the sample, may be you can try to reproduce it with that.

petrdrabek commented 3 years ago

@abhinashpati I'm experiencing diferent issue than you, sorry. It occures only with direct ML Kit using (v6+) I will desribe it with detail explanation and sample app soon.

JimmyPun610 commented 3 years ago

Here's a sample. I have replicated my project structure in this sample project as well. A Think to note that the issue is not easy to reproduce. It doesn't happens always. It happens out of nowhere. You might have to go through the flow multiple times in order to reproduce it.

https://github.com/abhinashpati/BarcodeSample

@petrdrabek If you have any better and easy way to reproduce this issue, please let @JimmyPun610 know. And as @petrdrabek mentioned that this issue also occurs in the sample, may be you can try to reproduce it with that.

Hi,

I have just tested several times with my Pixel 3 and it looks nothing happen. But for investigating the issue, it should not be related to Prism but Android Camera. The error should be come from GoogleVisionBarCodeScanner.CameraPreview.SetOrientation

    public void SetOrientation()
        {

            Android.Hardware.Camera camera = Methods.GetCamera(_cameraSource);
            camera?.StopPreview();
            switch (_windowManager.DefaultDisplay.Rotation)
            {
                case SurfaceOrientation.Rotation0:
                    camera?.SetDisplayOrientation(90);
                    break;
                case SurfaceOrientation.Rotation90:
                    camera?.SetDisplayOrientation(0);
                    break;
                case SurfaceOrientation.Rotation180:
                    camera?.SetDisplayOrientation(270);
                    break;
                case SurfaceOrientation.Rotation270:
                    camera?.SetDisplayOrientation(180);
                    break;
            }
            camera?.StartPreview();
        }

I would suggest you move to version 6+ which is switched to CameraX if it is possible. I have also uploaded the version 5.0.9 which added camera.StopPreview() and camera.StartPreview() and hopefully this could be fixed.

abhinashpati commented 3 years ago

Unfortunately, V5.0.9 Doesn't fix it. I'll try to v6 and to see if CameraX fixes this bug.

abhinashpati commented 3 years ago

I'm now getting another crash in V6.1. This is also happening randomly when I'm scanning barcode one after another.

2021-09-21 15:45:05.007 20095-20095/? A/DEBUG: Abort message: '[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Handle must be valid.
    Parameter name: instance
      at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method) [0x00009] in <1959115d56f8444789986cf39185638c>:0 
      at Android.Runtime.JNIEnv.CallVoidMethod (System.IntPtr jobject, System.IntPtr jmethod) [0x0000e] in <cdf69449759145adbc699e96b2eb3764>:0 
      at AndroidX.Camera.Core.IImageProxyInvoker.Close () [0x00033] in <3513b64a9eca4ee5946ec0af08160ea2>:0 
      at GoogleVisionBarCodeScanner.Renderer.CameraViewRenderer+BarcodeAnalyzer.Analyze (AndroidX.Camera.Core.IImageProxy proxy) [0x0025b] in <e5a7d9dfaad84930875e6bed57b41ce1>:0 
      at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_1 (System.Object state) [0x00000] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) [0x00007] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00021] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <98fdeeb5cad34f67b78b105df850970d>:0 
JimmyPun610 commented 3 years ago

FATAL UNHANDLED EXCEPTION: System.ArgumentException: Handle must be valid.

Hi,

If the navigation flow of your app is the same as the sample you provided, which is

  1. Click start scanning in landing page
  2. Go to barcode scanning page
  3. go to next page
  4. click use camera to open the camera to take photo.

I suggest you pop the barcode scanning page (which include the camera view) before pushing the next page, which should be

  1. Click start scanning
  2. open scanning page and scan
  3. get the scanning result, pop the scanning page first, and then push the camera page
petrdrabek commented 3 years ago

FATAL UNHANDLED EXCEPTION: System.ArgumentException: Handle must be valid.

@abhinashpati That is exactly the error I was facing too. I already find solution. @JimmyPun610 I will do push request soon.

JimmyPun610 commented 3 years ago

Hi @petrdrabek

Thank you for helping. Just pull the request and publish the new version 6.1.0.1-pre.

Hi @abhinashpati

Let see if the problem still exist with the 6.1.0.1-pre.

abhinashpati commented 3 years ago

I can confirm the Crash FATAL UNHANDLED EXCEPTION: System.ArgumentException: Handle must be valid is fixed but not the black screen issue :(

naokiyoshidassi commented 2 years ago

I'm also facing the black screen issue now. The camera turns extremely dark and don't recongnize anything on certain device.

Looks like it always happens on Android 11.

JimmyPun610 commented 2 years ago

For black screen issue, it may related to RequestFPS paramenter. Do not set it and the device will take default. maybe related to #101