How to stop scanning and store data from Google

2019-05-17 21:14发布

问题:

I'm using Google's Vision API BarcodeScanner on my project. I would like to interrupt scanning once a code has been scanned and store the content in another activity. How can i do that ? There are so many classes and 'interconnections' :x

Thanks !

回答1:

If you are using CameraSourcePreview as in the sample code, you can call its "release()" method to shut down the camera and the associated barcode detector. If you are using the CameraSource directly without the preview, then you can call "release()" directly on that instance.

See this other question which discusses passing the result back:

How to capture barcode values using the new Barcode API in Google Play Services?



回答2:

So, as far as stopping the scanning goes, I got it working by calling .release() on my BarcodeDetector instance. I followed this example and as you can see they set a Detector.Processor<Barcode> for the BarcodeDetector. The processor has a receiveDetections() method, so what I did was just calling barcodeDetector.release() with barcodeDetector being the instance that detected the barcode. It works fine for me, I also tested the scanning and starting another activity after detection, and only one activity is added, so it really just detects one barcode and then stops.

To save the content in another activity, you can start another activity in the same receiveDetections() method using an Intent, and then use the putExtra() method to get the data you need to the other activity, although I don't really know what you want to save and thus putExtra() might not suffice for you.

EDIT: concerning the example scroll down to "4. Reading a QR Code Using the Camera" there you will find what I am talking about.