Google Vision仅返回单个条码,如何获取检测到的所有条码字符串?
Hey there! I’ve spent plenty of time working with Google Vision’s barcode detection, so I know exactly how to fix this issue for you. The problem here is that the default setup (or the example code you’re using) is only configured to handle a single barcode detection at a time—but Google Vision absolutely supports detecting multiple barcodes in one image. Here’s how to adjust your code to capture all of them:
First, let’s replace that single-barcode onBarcodeDetected callback with a processor that can handle a full list of detected barcodes. The key is to use Detector.Processor<List<Barcode>>, which gives you access to every barcode the service finds in the frame.
Here’s the adjusted code for your BarcodeCaptureActivity:
// Initialize your BarcodeDetector with all supported formats BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this) .setBarcodeFormats(Barcode.ALL_FORMATS) .build(); // Set a custom processor to handle multiple barcodes barcodeDetector.setProcessor(new Detector.Processor<List<Barcode>>() { @Override public void release() { // Clean up resources when done (e.g., stop the camera source) if (cameraSource != null) { cameraSource.release(); } } @Override public void receiveDetections(Detector.Detections<List<Barcode>> detections) { SparseArray<Barcode> detectedBarcodes = detections.getDetectedItems(); // Check if we found any barcodes if (detectedBarcodes.size() > 0) { Log.d(TAG, "...::: DETECTED " + detectedBarcodes.size() + " BARCODES :::..."); // Loop through every barcode in the result set for (int i = 0; i < detectedBarcodes.size(); i++) { Barcode barcode = detectedBarcodes.valueAt(i); Log.d(TAG, "Barcode " + (i+1) + ": " + barcode.rawValue); // Add your custom logic here—store the rawValue in a list, update UI, etc. } } } }); // Make sure your CameraSource uses this detector (if you're using one) CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector) .setAutoFocusEnabled(true) .setRequestedPreviewSize(1280, 720) .build();
Why this works:
The original onBarcodeDetected method you were using is likely part of a simplified example that only grabs the first detected barcode. By implementing Detector.Processor, you get direct access to the full SparseArray of all barcodes found in each frame. You can then iterate through this array to extract every barcode’s rawValue (or other details like format, bounding box, etc.).
Quick checks to avoid issues:
- Ensure you’ve requested the
CAMERApermission in your manifest and at runtime (required for camera-based detection) - Double-check that your
BarcodeDetectoris set to detect all relevant formats (usingBarcode.ALL_FORMATScovers everything, but you can narrow it down if needed) - If you’re processing static images instead of camera frames, the same logic applies—use
barcodeDetector.detect(image)which returns aSparseArray<Barcode>of all results
That should get you all the barcodes from your images instead of just a random single one!
内容的提问来源于stack exchange,提问作者gabikefiv

