googlesamples / io2015-codelabs

codelabs for Google I/O 2015
https://codelabs.developers.google.com/
Apache License 2.0
516 stars 165 forks source link

Serious Bug in Barcode detection Api **Scan the attached image to see it ** #119

Open monajafi opened 7 years ago

monajafi commented 7 years ago

Sometimes while camera preview is open, Barcode Api detects non barcode items as barcode and returns random barcode number( detects barcode by itself without seeing any barcode image). If you want to test this behaviour keep the camera 10 to 15 cm above the left side of MacBook Pro keyboard it detects keyboard as barcode continuously(Scan following image of keyboard to see the bug ,regardless of orientation it detects image as barcode). 20170403_091601

This does not happen in other SDK like Scandit, i-nigma or Manatee works just Vision Api detects wrongly. increasing preview resolution increases detection error. here is my createcamerasource method in barcodecapture activity class : @SuppressLint("InlinedApi") private void createCameraSource(boolean autoFocus) { Context context = getApplicationContext(); BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).setBarcodeFormats(Barcode.EAN_13 | Barcode.EAN_8 | Barcode.UPC_A).build(); this.mScanFeedback = new ScanFeedback(this); mScanFeedback.setBeepEnabled(true); barcodeDetector.setProcessor(new Detector.Processor() { @Override public void receiveDetections(Detector.Detections detections) {

            SparseArray<Barcode> barcodes = detections.getDetectedItems();
            if (barcodes != null && (barcodes.size() > 0)) {
                String barcode =barcodes.valueAt(0).displayValue;
                if (!barcode.isEmpty()) {
                    showBarcode(barcode);
                }
            }
        }

        @Override
        public void release() {

        }
    });

    if (!barcodeDetector.isOperational()) {
        Toast.makeText(context, R.string.barcodeOperationalErr, Toast.LENGTH_SHORT).show();
        IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
        boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;

        if (hasLowStorage) {
            Toast.makeText(this, R.string.lowStorageErr, Toast.LENGTH_LONG).show();
        }
    }

    CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector).setRequestedPreviewSize(2560, 1440)
            .setFacing(CameraSource.CAMERA_FACING_BACK);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        builder = builder.setFocusMode(
                autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null);
    }
    mCameraSource = builder
            .setFlashMode(Camera.Parameters.FLASH_MODE_OFF)
            .build();
    flashBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                mCameraSource.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

            } else {
                mCameraSource.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            }
        }
    });
}