简体   繁体   中英

the Barcode Scanner from googles mlkit doesn't recognize any barcodes

So i followed googles tutorial for their barcode scanner ( this one ) and the qr scanning works like a charm. The only problem is that i don't need qr codes but rather bar codes. But they don't work. It doesn't detect anything. I tried multiple online bar codes and ones from around the house but none got recognised as a barcode.

this is the code in my activity that handles the image and scanner:

public void btnClick(View v) {

    Intent imageTakeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if(imageTakeIntent.resolveActivity(getPackageManager())!=null){
        startActivityForResult(imageTakeIntent, 101);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==101 && resultCode==RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageView.setImageBitmap(imageBitmap);
        image = InputImage.fromBitmap(imageBitmap, 0);
        barCodeScanning();
    }
}

public void barCodeScanning(){
    BarcodeScannerOptions options =
            new BarcodeScannerOptions.Builder()
                    .setBarcodeFormats(
                            Barcode.FORMAT_CODE_128,
                            Barcode.FORMAT_CODE_93,
                            Barcode.FORMAT_CODE_93,
                            Barcode.FORMAT_CODABAR,
                            Barcode.FORMAT_EAN_13,
                            Barcode.FORMAT_EAN_8,
                            Barcode.FORMAT_ITF,
                            Barcode.FORMAT_UPC_A,
                            Barcode.FORMAT_UPC_E,
                            Barcode.FORMAT_PDF417,
                            Barcode.FORMAT_DATA_MATRIX,
                            Barcode.FORMAT_QR_CODE,
                            Barcode.FORMAT_AZTEC)
                    .build();
    BarcodeScanner scanner = BarcodeScanning.getClient();
    Task<List<Barcode>> result = scanner.process(image)
            .addOnSuccessListener(new OnSuccessListener<List<Barcode>>() {
                @Override
                public void onSuccess(List<Barcode> barcodes) {
                    System.out.println(barcodes.size());
                    for (Barcode barcode: barcodes) {
                        Rect bounds = barcode.getBoundingBox();
                        Point[] corners = barcode.getCornerPoints();

                        String rawValue = barcode.getRawValue();
                        System.out.println(rawValue);

                        int valueType = barcode.getValueType();
                        System.out.println(valueType);
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    e.printStackTrace();
                }
            });
}

The console looks fine to me except the line E/libc: Access denied finding property "ro.hardware.chipname" which is marked red but didn't turn up any helpful results on google. This is the complete console output:

02/26 18:29:10: Launching 'app' on HMD Global Nokia 5.3.
Install successfully finished in 2 s 230 ms.
$ adb shell am start -n "com.project.kuecheninventar/com.project.kuecheninventar.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 28314 on device 'hmd_global-nokia_5_3-N0AA003687KA2700363'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
W/kuecheninventa: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
I/AdrenoGLES: QUALCOMM build                   : cfd6c90, Id60e6598a1
Build Date                       : 01/27/20
OpenGL ES Shader Compiler Version: EV031.27.05.03
Local Branch                     : mybranchea40f85b-21cb-f1fb-b0c0-5cb90179c3e0
Remote Branch                    : quic/gfx-adreno.lnx.1.0.r91-rel
Remote Branch                    : NONE
Reconstruct Branch               : NOTHING
Build Config                     : S P 8.0.12 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
W/Gralloc3: mapper 3.x is not supported
D/TransportRuntime.JobInfoScheduler: Scheduling upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) with jobId=-1883559869 in 86400000ms(Backend next call timestamp 0). Attempt 1
I/DynamiteModule: Considering local module com.google.mlkit.dynamite.barcode:10000 and remote module com.google.mlkit.dynamite.barcode:0
Selected local version of com.google.mlkit.dynamite.barcode
I/tflite: Initialized TensorFlow Lite runtime.
I/native: barcode_detector_client.cc:239 Not using NNAPI
E/libc: Access denied finding property "ro.hardware.chipname"
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
I/System.out: 0
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore: Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...

The line System.out.println(barcodes.size()); just prints 0 which means the barcode list is empty. Did i do something wrong or why does the qr code regognize but no bar code?

I forgot about this issue because i solved through another way. So here is my solution with zxing: In the app built.gradle use implementation 'com.journeyapps:zxing-android-embedded:4.1.0'

public class BarCodeScanner extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        scan();
    }

    private void scan() {
        IntentIntegrator i = new IntentIntegrator(this);

        i.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);

        i.setCaptureActivity(CaptureActivitaet.class);
        i.setOrientationLocked(false);
        i.setBeepEnabled(false);
        i.setPrompt("Scanning");
        i.initiateScan();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        IntentResult res = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (res != null) {
            if (res.getContents() != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(res.getContents());
                builder.setTitle(getString(R.string.scanComplete));
                builder.setPositiveButton(R.string.ok, (dialogInterface, i) -> {
                    setResult(RESULT_OK, new Intent().putExtra("code", res.getContents()));
                    finish();
                }).setNegativeButton(R.string.scanAgain, (dialogInterface, i) -> scan());
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

The capture activity is just an empty activity public class CaptureActivitaet extends CaptureActivity {} although it has to be configured in the Manifest file like this:

    <activity
        android:name="com.project.Grocerly.UI.CaptureActivitaet"
        android:screenOrientation="fullSensor"
        android:stateNotNeeded="true"
        android:windowSoftInputMode="stateAlwaysHidden" />

This opens a fullscreen camera activity which scans codes accordingly to the types set via i.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES); in the scan() methode. The extra capture activity can probably be skipped if you use setCaptureActivity(this.class) and configure it correctly in the manifest file but im not sure about that.

Because this was a project for university this code isn't very pretty but it works and that was my main concern at the time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM