无法使用Firebase ML Kit读取超过30个字符的条形码。

huangapple go评论72阅读模式
英文:

Not able to read more 30 chars barcode using Firebase ML Kit

问题

我正在尝试使用 Firebase ML Kit 独立 API 来读取条形码128和 QR 码。我能够读取条形码和 QR 码。但是,如果我的条形码长度超过30-35个字符,条形码扫描器就无法检测到条形码。我能够检测 QR 码,但无法检测条形码。

以下是我的代码:

// 使用以下依赖项以在 Google Play Services 中使用动态下载的模型
implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:16.1.2'
implementation 'com.google.mlkit:barcode-scanning:16.0.3'

ScannerActivity

executor = Executors.newSingleThreadExecutor();
PreviewView previewView = findViewById(R.id.cameraPreviewView);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
    try {
        ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
        Preview preview = new Preview.Builder().build();
        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();

        preview.setSurfaceProvider(
                previewView.createSurfaceProvider());

        ImageCapture imageCapture = new ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
                .build();

        ImageAnalysis imageAnalysis =
                new ImageAnalysis.Builder()
                        .build();

        imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new ImageAnalysis.Analyzer() {

            private BarcodeScanner scanner = buildBarCodeScanner();

            @Override
            public void analyze(ImageProxy imageProxy) {
                @SuppressLint("UnsafeExperimentalUsageError") Image mediaImage = imageProxy.getImage();
                if (mediaImage != null) {
                    InputImage image =
                            InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());
                    Task<List<Barcode>> result = scanner.process(image);

                    result.addOnSuccessListener(new OnSuccessListener<List<Barcode>>() {
                        @Override
                        public void onSuccess(List<Barcode> barcodes) {
                            processBarCodes(barcodes);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            // 任务失败,出现异常
                            Log.i("CameraXApp3", "扫描任务失败。错误:" + e);

                        }
                    }).addOnCompleteListener(new OnCompleteListener<List<Barcode>>() {
                        @Override
                        public void onComplete(@NonNull Task<List<Barcode>> task) {
                            mediaImage.close();
                            imageProxy.close();
                        }
                    });
                }
            }

            private BarcodeScanner buildBarCodeScanner() {
                BarcodeScannerOptions options =
                        new BarcodeScannerOptions.Builder()
                                .setBarcodeFormats(
                                        Barcode.FORMAT_QR_CODE,
                                        Barcode.FORMAT_CODE_128)
                                .build();
                return BarcodeScanning.getClient(options);
            }

            private void processBarCodes(List<Barcode> barcodes) {
                for (Barcode barcode : barcodes) {
                    confirmCounter++;
                    if (confirmCounter >= CONFIRM_VALUE) {
                        confirmCounter = 0;
                        final Rect boundingBox = barcode.getBoundingBox();
                        final Point[] cornerPoints = barcode.getCornerPoints();
                        String rawValue = barcode.getRawValue();
                        int valueType = barcode.getValueType();
                        if (valueType == Barcode.TYPE_TEXT) {
                            toast(MainActivity.this, "Received Message:" + rawValue);
                        }
                    }
                }
            }

            public void toast(final Context context, final String text) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(() -> Toast.makeText(context, text, Toast.LENGTH_LONG).show());
            }
        });

        Camera camera = cameraProvider.bindToLifecycle(
                ((LifecycleOwner) this),
                cameraSelector,
                preview,
                imageCapture,
                imageAnalysis);

    } catch (InterruptedException | ExecutionException e) {

    }
}, ContextCompat.getMainExecutor(this));

我还尝试通过点击来增加聚焦但没有成功

previewView.setOnTouchListener((v, event) -> {
    v.performClick();
    if (event.getAction() == MotionEvent.ACTION_UP) {
        final MeteringPointFactory factory = previewView.getMeteringPointFactory();
        final MeteringPoint point = factory.createPoint(event.getX(), event.getY());
        final FocusMeteringAction action = new FocusMeteringAction.Builder(point).build();
        camera.getCameraControl().startFocusAndMetering(action);
        return false;
    }
    return true;
});
英文:

I am trying to read barcode128 and QR code using firebase ML kit standalone API. I am able to read both the barcode and QR code. But if my barcode length exceeds more than 30-35 chars then the barcode scanner not able to detect the barcode. I am able to detect QR code but not a barcode.

Here is my code :

// Use this dependency to use the dynamically downloaded model in Google Play Services
implementation &#39;com.google.android.gms:play-services-mlkit-barcode-scanning:16.1.2&#39;
implementation &#39;com.google.mlkit:barcode-scanning:16.0.3&#39;  

ScannerActivity :

    executor = Executors.newSingleThreadExecutor();
PreviewView previewView = findViewById(R.id.cameraPreviewView);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -&gt; {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
Preview preview = new Preview.Builder().build();
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
preview.setSurfaceProvider(
previewView.createSurfaceProvider());
ImageCapture imageCapture = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.build();
ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.build();
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new ImageAnalysis.Analyzer() {
private BarcodeScanner scanner = buildBarCodeScanner();
@Override
public void analyze(ImageProxy imageProxy) {
@SuppressLint(&quot;UnsafeExperimentalUsageError&quot;) Image mediaImage = imageProxy.getImage();
if (mediaImage != null) {
InputImage image =
InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());
Task&lt;List&lt;Barcode&gt;&gt; result = scanner.process(image);
result.addOnSuccessListener(new OnSuccessListener&lt;List&lt;Barcode&gt;&gt;() {
@Override
public void onSuccess(List&lt;Barcode&gt; barcodes) {
processBarCodes(barcodes);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
Log.i(&quot;CameraXApp3&quot;, &quot;scanner task failed. Error:&quot; + e);
}
}).addOnCompleteListener(new OnCompleteListener&lt;List&lt;Barcode&gt;&gt;() {
@Override
public void onComplete(@NonNull Task&lt;List&lt;Barcode&gt;&gt; task) {
mediaImage.close();
imageProxy.close();
}
});
}
}
private BarcodeScanner buildBarCodeScanner() {
BarcodeScannerOptions options =
new BarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_CODE_128)
.build();
return BarcodeScanning.getClient(options);
}
private void processBarCodes(List&lt;Barcode&gt; barcodes) {
for (Barcode barcode : barcodes) {
confirmCounter++;
if (confirmCounter &gt;= CONFIRM_VALUE) {
confirmCounter = 0;
final Rect boundingBox = barcode.getBoundingBox();
final Point[] cornerPoints = barcode.getCornerPoints();
String rawValue = barcode.getRawValue();
int valueType = barcode.getValueType();
if (valueType == Barcode.TYPE_TEXT) {
toast(MainActivity.this, &quot;Received Message:&quot; + rawValue);
}
}
}
}
public void toast(final Context context, final String text) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -&gt; Toast.makeText(context, text, Toast.LENGTH_LONG).show());
}
});
Camera camera = cameraProvider.bindToLifecycle(
((LifecycleOwner) this),
cameraSelector,
preview,
imageCapture,
imageAnalysis);
} catch (InterruptedException | ExecutionException e) {
}
}, ContextCompat.getMainExecutor(this));

I am also trying to add more focus by clicking but no luck :

 previewView.setOnTouchListener((v, event) -&gt; {
v.performClick();
if (event.getAction() == MotionEvent.ACTION_UP) {
final MeteringPointFactory factory = previewView.getMeteringPointFactory();
final MeteringPoint point = factory.createPoint(event.getX(), event.getY());
final FocusMeteringAction action = new FocusMeteringAction.Builder(point).build();
camera.getCameraControl().startFocusAndMetering(action);
return false;
}
return true;
});

答案1

得分: 2

ML Kit条形码要求最细的条至少为3个像素宽。Code-128码中每个字符宽度为11个条码,因此以像素计算为33个像素宽,对于35个这样的字符,宽度要求为1155个像素。因此,图像中的条形码宽度至少需要1155个像素。这个粗略计算不考虑<start> + <end> + <checksum>字符。

您可能需要增加图像分辨率来适应这种格式。

英文:

ML Kit barcode requires thinnest bar to be minimum 3 pixels wide. Code-128 each character is 11 bar wide, so in terms of pixels it is 33 pixels wide and for 35 such character it requires 1155 pixels in terms of width. So barcode needs to be at least 1155 pixels wide in the image. This rough calculation ignores <start> + <end> + <checksum> characters.

You may need to increase the image resolution for this format.

huangapple
  • 本文由 发表于 2020年9月16日 20:22:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63919991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定