dlazaro66 / QRCodeReaderView

Modification of ZXING Barcode Scanner project for easy Android QR-Code detection and AR purposes
1.9k stars 491 forks source link

Inflating the view before the camera permission is given and then starting the scanner after the permission is granted crashes #156

Open Dima-369 opened 6 years ago

Dima-369 commented 6 years ago

The sample app avoids this by only inflating the layout when the camera permission is given.

We are migrating this library into our project and this would allow us to quickly port everything without rewriting all the view logic.


You can emulate this quickly by moving the line below from initQRCodeReaderView() to onCreate() while keeping the content variable as a class member variable:

View content = getLayoutInflater().inflate(R.layout.content_decoder, mainLayout, true);

The sample app will then crash (after quickly displaying a single camera frame) with:

java.lang.RuntimeException: An error occurred while executing doInBackground()                                                                              
   at android.os.AsyncTask$3.done(AsyncTask.java:309)                                                                              
   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)                                                                              
   at java.util.concurrent.FutureTask.setException(FutureTask.java:223)                                                                              
   at java.util.concurrent.FutureTask.run(FutureTask.java:242)                                                                              
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)                                                                              
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)                                                                              
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)                                                                              
   at java.lang.Thread.run(Thread.java:818)                                                                              
Caused by: java.lang.IllegalArgumentException: Both dimensions must be greater than 0                                                                              
   at com.google.zxing.common.BitMatrix.<init>(BitMatrix.java:50)                                                                              
   at com.google.zxing.common.GlobalHistogramBinarizer.getBlackMatrix(GlobalHistogramBinarizer.java:91)                                                                              
   at com.google.zxing.common.HybridBinarizer.getBlackMatrix(HybridBinarizer.java:86)                                                                              
   at com.google.zxing.BinaryBitmap.getBlackMatrix(BinaryBitmap.java:85)                                                                              
   at com.google.zxing.qrcode.QRCodeReader.decode(QRCodeReader.java:77)                                                                              
   at com.dlazaro66.qrcodereaderview.QRCodeReaderView$DecodeFrameTask.doInBackground(QRCodeReaderView.java:355)                                                                              
   at com.dlazaro66.qrcodereaderview.QRCodeReaderView$DecodeFrameTask.doInBackground(QRCodeReaderView.java:329)                                                                              
   at android.os.AsyncTask$2.call(AsyncTask.java:295)                                                                              
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                              
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)                                                                               
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)                                                                               
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)                                                                               
   at java.lang.Thread.run(Thread.java:818)
vietmobilefolk commented 6 years ago

@Gira-X : Could you try use ViewStub as placeholder for QRCodeReaderView, then only inflate when permission granted?

Dima-369 commented 6 years ago

Yes, that works indeed!

Here is the modified sample repo which uses a ViewStub: https://github.com/Gira-X/QRCodeReaderView


But in the end we decided to use a Google Vision API view because it scans a bit faster: https://github.com/wax911/Vision-Barcode-Scanner