WebsiteBeaver / android-document-scanner

66 stars 48 forks source link

OpenCV assertion issue causing multiple crashes #29

Closed ruchikagoel7 closed 1 year ago

ruchikagoel7 commented 1 year ago
Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.ayumitra/com.websitebeaver.documentscanner.DocumentScannerActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2143666989, result=-1, data=null} to activity {com.ayumitra/com.websitebeaver.documentscanner.DocumentScannerActivity}: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.1.0) /build/master_pack-android/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
]
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5094)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5127)
       at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
       at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2441)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:233)
       at android.os.Looper.loop(Looper.java:334)
       at android.app.ActivityThread.main(ActivityThread.java:8396)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)

Caused by java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2143666989, result=-1, data=null} to activity {com.ayumitra/com.websitebeaver.documentscanner.DocumentScannerActivity}: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.1.0) /build/master_pack-android/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
]
       at android.app.ActivityThread.deliverResults(ActivityThread.java:5651)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5081)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5127)
       at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
       at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2441)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:233)
       at android.os.Looper.loop(Looper.java:334)
       at android.app.ActivityThread.main(ActivityThread.java:8396)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)

Caused by org.opencv.core.CvException: cv::Exception: OpenCV(4.1.0) /build/master_pack-android/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

       at org.opencv.imgproc.Imgproc.cvtColor_1(Imgproc.java)
       at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:2309)
       at com.websitebeaver.documentscanner.utils.ImageUtil.getImageFromFilePath(ImageUtil.kt:36)
       at com.websitebeaver.documentscanner.DocumentScannerActivity$cameraUtil$1.invoke(DocumentScannerActivity.kt:88)
       at com.websitebeaver.documentscanner.DocumentScannerActivity$cameraUtil$1.invoke(DocumentScannerActivity.kt:75)
       at com.websitebeaver.documentscanner.utils.CameraUtil.startForResult$lambda-0(CameraUtil.kt:41)
       at com.websitebeaver.documentscanner.utils.CameraUtil.lambda$QtmUm3JcXlX2aa1YkJnrPoMUzS8()
       at com.websitebeaver.documentscanner.utils.-$$Lambda$CameraUtil$QtmUm3JcXlX2aa1YkJnrPoMUzS8.onActivityResult(:4)
       at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:418)
       at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:375)
       at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:793)
       at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
       at android.app.Activity.dispatchActivityResult(Activity.java:8718)
       at android.app.ActivityThread.deliverResults(ActivityThread.java:5644)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5081)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5127)
       at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
       at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2441)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:233)
       at android.os.Looper.loop(Looper.java:334)
       at android.app.ActivityThread.main(ActivityThread.java:8396)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)

Getting this crash for multiple users Android build tools: 4.1.0 targetsdk 33

alexglazkov9 commented 1 year ago

I also see the same issue, mostly on Samsung devices.

alexglazkov9 commented 1 year ago

@dmarcs from what I can see, the error happens here on line 36: https://github.com/WebsiteBeaver/android-document-scanner/blob/7c540fac6ffde5ba5f87129f76a9a0221e36d8dd/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt#L31-L37

It seems that Imgcodecs.imread(..) returns null on line 33, because filepath is a "missing file, improper permissions, unsupported or invalid format".

Could be an invalid format used by samsung/motorola devices? Maybe worth adding a null check to avoid crashes?

dmarcs commented 1 year ago

@alexglazkov9 can you please log the filepath, so we can see if it's an invalid format? Also can you add a check to see if the file exists, so we know if this is caused by a missing file?

shahawi commented 1 year ago

May be a fix pull request #30

shahawi commented 1 year ago

Apologies, Seems that this pull request is flawed maybe someone can improve upon

dmarcs commented 1 year ago

Fixed in https://github.com/WebsiteBeaver/android-document-scanner/pull/33 by making sure image isn't empty before calling cvtColor. If it is try reading image without OpenCV.