You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 CAMERA permission in your manifest and at runtime (required for camera-based detection)
  • Double-check that your BarcodeDetector is set to detect all relevant formats (using Barcode.ALL_FORMATS covers 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 a SparseArray<Barcode> of all results

That should get you all the barcodes from your images instead of just a random single one!

内容的提问来源于stack exchange,提问作者gabikefiv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:31:31