【问题标题】:Barcode scanner is reading only QR codes using ML Kit条形码扫描仪使用 ML Kit 仅读取 QR 码
【发布时间】:2019-12-21 12:27:57
【问题描述】:

我正在尝试使用 ML Kit 在 android studio 中构建条形码扫描仪。我有一个工作正常的代码,它只检测二维码。我想让条形码扫描仪读取所有类型的条形码,主要是食品条形码。我正在尝试获取条形码的原始值,以便在数据库中进行搜索并将相关信息显示到屏幕上。

以下是我用来检测二维码的代码。

public class MainActivity extends AppCompatActivity {
    CameraView camera_view;
    boolean isDetected = false;
    private Button btn_start_again;

    FirebaseVisionBarcodeDetectorOptions options;
    FirebaseVisionBarcodeDetector detector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Dexter.withActivity(this)
                .withPermissions(new String [] {Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO})
                .withListener(new MultiplePermissionsListener() {

                    @Override
                    public void onPermissionsChecked(MultiplePermissionsReport report) {

                        setUpCamera();

                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {

                        Toast.makeText(getApplicationContext(),"You must accept the permissions",Toast.LENGTH_LONG).show();

                    }
                }).check();

    }

    private void setUpCamera() {

        btn_start_again =(Button)findViewById(R.id.btn_again);
        btn_start_again.setEnabled(false);

        btn_start_again.setOnClickListener((View v) -> isDetected = !isDetected);

        camera_view = (CameraView) findViewById(R.id.cameraview);
        camera_view.setLifecycleOwner(this);

        camera_view.addFrameProcessor(new FrameProcessor() {
            @Override
            public void process(@NonNull Frame frame) {

                processImage(getVisionImageFromFrame(frame));


            }
        });

        FirebaseVisionBarcodeDetectorOptions options =
                new FirebaseVisionBarcodeDetectorOptions.Builder()
                        .setBarcodeFormats(
                                FirebaseVisionBarcode.FORMAT_QR_CODE,
                                FirebaseVisionBarcode.FORMAT_AZTEC)
                        .build();
        detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);

    }

    private void processImage(FirebaseVisionImage image) {

        if(!isDetected){

            detector.detectInImage(image)
                    .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
                        @Override
                        public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {

                            processResult(firebaseVisionBarcodes);

                        }
                    })

                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                            Toast.makeText(getApplicationContext(),"e.getMessage()",Toast.LENGTH_LONG).show();

                        }
                    });


        }

    }

    private void processResult(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {

        if(firebaseVisionBarcodes.size() > 0 ){

            isDetected = true;
            btn_start_again.setEnabled(isDetected);

            for (FirebaseVisionBarcode item:firebaseVisionBarcodes){

                Rect bounds = item.getBoundingBox();
                Point[] corners = item.getCornerPoints();

                String rawValue = item.getRawValue();

                int value_type = item.getValueType();
                switch (value_type){
                    case FirebaseVisionBarcode.TYPE_TEXT:
                    {
                        createDialog(item.getRawValue());
                    }
                    break;
                    case FirebaseVisionBarcode.TYPE_URL:
                    {

                        Intent intent =  new Intent(Intent.ACTION_VIEW, Uri.parse(item.getRawValue()));
                        startActivity(intent);

                    }
                    case FirebaseVisionBarcode.TYPE_CONTACT_INFO:
                    {

                        String info = new StringBuilder("Name:")
                                /*.append(item.getContactInfo().getName().getFormattedName())
                                .append("\n")*/
                                .append("Address")
                                .append(item.getContactInfo().getAddresses().get(0).getAddressLines())
                                .append("\n")
                                .append("Email:")
                                .append(item.getContactInfo().getEmails().get(0).getAddress())
                                .toString();
                        createDialog(info);

                    }
                    break;

                    default:
                    {

                        createDialog(rawValue);

                    }
                    break;


                }

            }

        }

    }

    private void createDialog(String text) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(text);
        builder.setPositiveButton("OK", ((DialogInterface dialog, int i) -> dialog.dismiss()));

        AlertDialog dialog = builder.create();
        dialog.show();

    }

    private FirebaseVisionImage getVisionImageFromFrame(Frame frame) {

        byte[] data = frame.getData();
        FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()
                .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
                .setHeight(frame.getSize().getHeight())
                .setWidth(frame.getSize().getWidth())
                .build();
                //.setRotation(frame.getRotation())

        return FirebaseVisionImage.fromByteArray(data,metadata);
    }
}

【问题讨论】:

  • 您的应用只读取二维码,因为您尚未添加条形码类型
  • .setBarcodeFormats( FirebaseVisionBarcode.FORMAT_QR_CODE, FirebaseVisionBarcode.FORMAT_AZTEC) 在此处添加更多类型
  • 我已经添加了几种格式和类型,仍然没有结果。

标签: java android barcode-scanner firebase-mlkit


【解决方案1】:

你还有硬件问题。

Android 设备的摄像头通常仅在正确对焦时才读取条形码,并且仅在后置摄像头上发生,因为前置摄像头通常不具有自动对焦功能。

相反,即使没有很好地聚焦,二维码也会被读取。

此外,普通相机读取条码的速度很慢,如果您想要一台能够快速读取所有条码的设备,请寻找具有内置条码读取器的设备,它们被称为 PDA,尽管它们比智能手机贵得多.

【讨论】:

  • 请问有什么解决方案,因为这是我使用智能手机读取条形码的要求之一吗?
  • 条码格式有很多种。您可以在 Play 商店中安装许多应用程序中的一些来读取条形码,这可能会告诉您条形码的格式。然后您必须将相应的 FirebaseVisionBarcode.FORMAT* 添加到您的代码中
猜你喜欢
  • 2012-08-08
  • 2022-06-24
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
相关资源
最近更新 更多