扫描仪scanner接口_QR Code Scanner –适用于Android的条形码扫描仪

本文介绍了如何使用Google Mobile Vision API在Android应用中实现二维码(QR Code)和条形码扫描功能。通过配置Android Studio,设置必要的权限,以及创建 BarcodeDetector,实现实时和图像扫描。示例代码展示了从相机预览和图像中检测二维码的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

扫描仪scanner接口

QR Code scanner or Barcode scanner for android features are present in many apps to read some useful data. In this tutorial, we’ll be discussing and implementing the Barcode API present in the Google Mobile Vision API. To know the implementation of Face Detection using the Vision API refer here.

许多应用程序中均存在用于Android功能的QR码扫描仪或条形码扫描仪,以读取一些有用的数据。 在本教程中,我们将讨论和实现Google Mobile Vision API中存在的Barcode API。 要了解使用Vision API进行人脸检测的实现,请参阅此处

适用于Android的条形码扫描仪 (Barcode Scanner for Android)

QR Code Scanner for Android, Bar code scanner for android tutorial

With the introduction of Google Vision API, implementing Barcodes in an application has got a lot easier for developers.


随着Google Vision API的引入,对于开发人员而言,在应用程序中实现条形码变得非常容易。

Following are the major formats that the Vision API supports.

以下是Vision API支持的主要格式。

  • 1D barcodes: EAN-13, EAN-8, UPC-A, UPC-E, Code-39, Code-93, Code-128, ITF, Codabar

    一维条形码 :EAN-13,EAN-8,UPC-A,UPC-E,Code-39,Code-93,Code-128,ITF,Codabar
  • 2D barcodes: QR Code, Data Matrix, PDF-417, AZTEC

    2D条码 :QR码,数据矩阵,PDF-417,AZTEC

Barcodes can scan things ranging from URL, Contact info to Geolocation, WIFI, Driver license IDs.

条形码可以扫描各种内容,包括URL,联系信息,地理位置,WIFI,驾驶执照ID。

QR Code is the more popular format and is commonly seen in many applications.

QR码是更流行的格式,在许多应用程序中都很常见。

Below, we’ll be developing an application that scans the QR Code value from a bitmap image as well as detects QR Code through a camera and perform the relevant actions.

下面,我们将开发一个应用程序,该程序从位图图像中扫描QR Code值,并通过摄像头检测QR Code并执行相关操作。

适用于Android项目结构的QR Code扫描仪 (QR Code Scanner for Android project structure)

为条形码库配置Android Studio (Configuring Android Studio for Barcode Library)

Add the following inside the build.gradle file.

build.gradle文件中添加以下内容。

implementation 'com.google.android.gms:play-services-vision:11.8.0'

Add the following inside the AndroidManifest.xml file application tag to enable barcode detection in your application.

AndroidManifest.xml文件应用程序标记内添加以下内容,以在应用程序中启用条形码检测。

<meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="barcode" />

来自图像的QR码扫描仪 (QR Code Scanner from Image)

The code for the activity_main.xml layout file is given below.

下面给出了activity_main.xml布局文件的代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


    <Button
        android:id="@+id/btnTakePicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/take_barcode_picture" />

    <Button
        android:id="@+id/btnScanBarcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnTakePicture"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:text="@string/scan_barcode" />
</RelativeLayout>

The code for the MainActivity.java is given below.

MainActivity.java的代码如下。

package com.journaldev.barcodevisionapi;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnTakePicture, btnScanBarcode;

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

    private void initViews() {
        btnTakePicture = findViewById(R.id.btnTakePicture);
        btnScanBarcode = findViewById(R.id.btnScanBarcode);
        btnTakePicture.setOnClickListener(this);
        btnScanBarcode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnTakePicture:
                startActivity(new Intent(MainActivity.this, PictureBarcodeActivity.class));
                break;
            case R.id.btnScanBarcode:
                startActivity(new Intent(MainActivity.this, ScannedBarcodeActivity.class));
                break;
        }

    }
}

The MainActivity.java contains two buttons. The first launches an Activity that scans for a QR Code in the bitmap image captured from the camera and returns the data present in the QR Code(if any). The second scans for the QRCode and detects them in real time.

MainActivity.java包含两个按钮。 第一个启动活动,该活动在从相机捕获的位图图像中扫描QR码,并返回QR码中存在的数据(如果有)。 第二个扫描QRCode并实时检测到它们。

Before we move onto the business logic of the application, we need to add the following permissions to the AndroidManifest.xml file.

在进入应用程序的业务逻辑之前,我们需要向AndroidManifest.xml文件添加以下权限。

<uses-feature
        android:name="android.hardware.camera"
        android:required="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

To share and access the files created by other applications we need to add the following provider tag inside our application tag in the AndroidManifest.xml file.

要共享和访问其他应用程序创建的文件,我们需要在AndroidManifest.xml文件的应用provider标签内添加以下provider程序标签。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

This is required for retrieving the image captured by the camera in our QR code scanner for android application.

在Android应用程序的QR码扫描仪中检索相机捕获的图像时,这是必需的。

Let’s start with the first one namely the PictureBarcodeActivity.java.

让我们从第一个开始,即PictureBarcodeActivity.java

The code for the xml layout activity_barcode_picture.xml is given below.

xml布局activity_barcode_picture.xml的代码如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/journaldev_logo" />

    <TextView
        android:id="@+id/txtResultsHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:text="Results"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtResultsBody"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtResultsHeader"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:gravity="center" />

    <Button
        android:id="@+id/btnOpenCamera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:text="@string/open_camera" />
</RelativeLayout>

The code for the PictureCodeActivity.java class is given below.

下面给出了PictureCodeActivity.java类的代码。

package com.journaldev.barcodevisionapi;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.File;
import java.io.FileNotFoundException;

public class PictureBarcodeActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnOpenCamera;
    TextView txtResultBody;

    private BarcodeDetector detector;
    private Uri imageUri;
    private static final int REQUEST_CAMERA_PERMISSION = 200;
    private static final int CAMERA_REQUEST = 101;
    private static final String TAG = "API123";
    private static final String SAVED_INSTANCE_URI = "uri";
    private static final String SAVED_INSTANCE_RESULT = "result";

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

        initViews();

        if (savedInstanceState != null) {
            if (imageUri != null) {
                imageUri = Uri.parse(savedInstanceState.getString(SAVED_INSTANCE_URI));
                txtResultBody.setText(savedInstanceState.getString(SAVED_INSTANCE_RESULT));
            }
        }

        detector = new BarcodeDetector.Builder(getApplicationContext())
                .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE)
                .build();

        if (!detector.isOperational()) {
            txtResultBody.setText("Detector initialisation failed");
            return;
        }
    }

    private void initViews() {
        txtResultBody = findViewById(R.id.txtResultsBody);
        btnOpenCamera = findViewById(R.id.btnTakePicture);
        txtResultBody = findViewById(R.id.txtResultsBody);
        btnOpenCamera.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnTakePicture:
                ActivityCompat.requestPermissions(PictureBarcodeActivity.this, new
                        String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                break;
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CAMERA_PERMISSION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    takeBarcodePicture();
                } else {
                    Toast.makeText(getApplicationContext(), "Permission Denied!", Toast.LENGTH_SHORT).show();
                }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            launchMediaScanIntent();
            try {


                Bitmap bitmap = decodeBitmapUri(this, imageUri);
                if (detector.isOperational() && bitmap != null) {
                    Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                    SparseArray<Barcode> barcodes = detector.detect(frame);
                    for (int index = 0; index < barcodes.size(); index++) {
                        Barcode code = barcodes.valueAt(index);
                        txtResultBody.setText(txtResultBody.getText() + "\n" + code.displayValue + "\n");

                        int type = barcodes.valueAt(index).valueFormat;
                        switch (type) {
                            case Barcode.CONTACT_INFO:
                                Log.i(TAG, code.contactInfo.title);
                                break;
                            case Barcode.EMAIL:
                                Log.i(TAG, code.displayValue);
                                break;
                            case Barcode.ISBN:
                                Log.i(TAG, code.rawValue);
                                break;
                            case Barcode.PHONE:
                                Log.i(TAG, code.phone.number);
                                break;
                            case Barcode.PRODUCT:
                                Log.i(TAG, code.rawValue);
                                break;
                            case Barcode.SMS:
                                Log.i(TAG, code.sms.message);
                                break;
                            case Barcode.TEXT:
                                Log.i(TAG, code.displayValue);
                                break;
                            case Barcode.URL:
                                Log.i(TAG, "url: " + code.displayValue);
                                break;
                            case Barcode.WIFI:
                                Log.i(TAG, code.wifi.ssid);
                                break;
                            case Barcode.GEO:
                                Log.i(TAG, code.geoPoint.lat + ":" + code.geoPoint.lng);
                                break;
                            case Barcode.CALENDAR_EVENT:
                                Log.i(TAG, code.calendarEvent.description);
                                break;
                            case Barcode.DRIVER_LICENSE:
                                Log.i(TAG, code.driverLicense.licenseNumber);
                                break;
                            default:
                                Log.i(TAG, code.rawValue);
                                break;
                        }
                    }
                    if (barcodes.size() == 0) {
                        txtResultBody.setText("No barcode could be detected. Please try again.");
                    }
                } else {
                    txtResultBody.setText("Detector initialisation failed");
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Failed to load Image", Toast.LENGTH_SHORT)
                        .show();
                Log.e(TAG, e.toString());
            }
        }
    }

    private void takeBarcodePicture() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File photo = new File(Environment.getExternalStorageDirectory(), "pic.jpg");
        imageUri = FileProvider.getUriForFile(PictureBarcodeActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, CAMERA_REQUEST);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (imageUri != null) {
            outState.putString(SAVED_INSTANCE_URI, imageUri.toString());
            outState.putString(SAVED_INSTANCE_RESULT, txtResultBody.getText().toString());
        }
        super.onSaveInstanceState(outState);
    }

    private void launchMediaScanIntent() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        mediaScanIntent.setData(imageUri);
        this.sendBroadcast(mediaScanIntent);
    }

    private Bitmap decodeBitmapUri(Context ctx, Uri uri) throws FileNotFoundException {
        int targetW = 600;
        int targetH = 600;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;

        return BitmapFactory.decodeStream(ctx.getContentResolver()
                .openInputStream(uri), null, bmOptions);
    }
}

Few inferences drawn from the above code are given below.

从上面的代码得出的推论很少,下面给出。

  • The following code creates a Barcode Detector.
    detector = new BarcodeDetector.Builder(getApplicationContext())
               .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE)
               .build();

    以下代码创建一个条形码检测器。
  • The types of formats to be scanned are set inside the method setBarcodeFormats().

    可以在setBarcodeFormats()方法内部设置要扫描的格式类型。
  • takeBarcodePicture() function is where the camera is launched. To retrieve the image we use the launchMediaScanIntent() that calls a broadcast Intent to search for the image using the image URI.

    takeBarcodePicture()函数是启动相机的位置。 要检索图像,我们使用launchMediaScanIntent()调用广播的Intent,以使用图像URI搜索图像。
  • A Frame.Builder is used to create a frame of the Bitmap image. Over the frame, the Barcode detector scans for the possible QR Codes.
    The following line in the above code creates a Frame out of the Bitmap.
    Frame frame = new Frame.Builder().setBitmap(bitmap).build();

    Frame.Builder用于创建位图图像的框架。 在框架上,条形码检测器扫描可能的QR码。
    上面的代码中的以下行从位图创建帧。
  • We’ve created a SparseArray that’ll contain all possible QR Codes present in the image by invoking the detect() method over the Barcode Detector.
    SparseArray<Barcode> barcodes = detector.detect(frame);

    To get the format of the QR Code, the valueFormat field is called over the Barcode instance as shown below.

    我们已经创建了一个SparseArray,它将通过在条形码检测器上调用detect()方法来包含图像中存在的所有可能的QR码。
    SparseArray<Barcode> barcodes = detector.detect(frame);

    要获取QR码的格式,请在Barcode实例上调用valueFormat字段,如下所示。

  • To get the displayed value and the raw values, the following are invoked.
    barcodes.valueAt(index).displayValue
    barcodes.valueAt(index).rawValue

    要获取显示值和原始值,将调用以下内容。
  • The relevant value returned is displayed in the TextView. For then one Barcodes in a Bitmap, their values are appended to the current TextView.

    返回的相关值显示在TextView中。 对于位图中的一个条形码,其值将附加到当前的TextView中。

The ScannedBarcodeActivity.java class scans for the Barcode through the camera. We’ve generated two of our own custom QR Codes from here.
The code for the layout of the activity_scan_barcode.xml is given below.

ScannedBarcodeActivity.java类通过摄像机扫描条形码。 我们从这里生成了两个自己的自定义QR码。
下面给出了activity_scan_barcode.xml的布局代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_horizontal_margin">

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btnAction"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/txtBarcodeValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:text="No Barcode Detected"
        android:textColor="@android:color/white"
        android:textSize="20sp" />


    <Button
        android:id="@+id/btnAction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="ADD CONTENT IN THE MAIL" />

</RelativeLayout>

相机的Android条码扫描仪 (Barcode Scanner for Android from Camera)

The code for the ScannedBarcodeActivity.java is given below.

下面给出了ScannedBarcodeActivity.java的代码。

package com.journaldev.barcodevisionapi;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.IOException;

public class ScannedBarcodeActivity extends AppCompatActivity {

    SurfaceView surfaceView;
    TextView txtBarcodeValue;
    private BarcodeDetector barcodeDetector;
    private CameraSource cameraSource;
    private static final int REQUEST_CAMERA_PERMISSION = 201;
    Button btnAction;
    String intentData = "";
    boolean isEmail = false;


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

        initViews();
    }

    private void initViews() {
        txtBarcodeValue = findViewById(R.id.txtBarcodeValue);
        surfaceView = findViewById(R.id.surfaceView);
        btnAction = findViewById(R.id.btnAction);


        btnAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (intentData.length() > 0) {
                    if (isEmail)
                        startActivity(new Intent(ScannedBarcodeActivity.this, EmailActivity.class).putExtra("email_address", intentData));
                    else {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(intentData)));
                    }
                }


            }
        });
    }

    private void initialiseDetectorsAndSources() {

        Toast.makeText(getApplicationContext(), "Barcode scanner started", Toast.LENGTH_SHORT).show();

        barcodeDetector = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();

        cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(1920, 1080)
                .setAutoFocusEnabled(true) //you should add this feature
                .build();

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (ActivityCompat.checkSelfPermission(ScannedBarcodeActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        cameraSource.start(surfaceView.getHolder());
                    } else {
                        ActivityCompat.requestPermissions(ScannedBarcodeActivity.this, new
                                String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                cameraSource.stop();
            }
        });


        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
                Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();
                if (barcodes.size() != 0) {


                    txtBarcodeValue.post(new Runnable() {

                        @Override
                        public void run() {

                            if (barcodes.valueAt(0).email != null) {
                                txtBarcodeValue.removeCallbacks(null);
                                intentData = barcodes.valueAt(0).email.address;
                                txtBarcodeValue.setText(intentData);
                                isEmail = true;
                                btnAction.setText("ADD CONTENT TO THE MAIL");
                            } else {
                                isEmail = false;
                                btnAction.setText("LAUNCH URL");
                                intentData = barcodes.valueAt(0).displayValue;
                                txtBarcodeValue.setText(intentData);

                            }
                        }
                    });

                }
            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        cameraSource.release();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initialiseDetectorsAndSources();
    }
}

Few inferences drawn from the above code are given below.

从上面的代码得出的推论很少,下面给出。

  • SurfaceView is good to display camera preview images as it renders the GUI rapidly.
    The interface SurfaceHolder.Callback is used to receive information about changes that occur in the surface (in this case, the camera preview). SurfaceHolder.Callback implements three methods:
    • SurfaceChanged : This method is called when the size or the format of the surface changes.
    • SurfaceCreated : When, in the first instance, the surface is created, this method is called.
    • SurfaceDestroyed : This is called when the surface is destroyed.

    SurfaceView可以很好地显示相机预览图像,因为它可以快速呈现GUI。
    SurfaceHolder.Callback接口用于接收有关曲面中发生的更改的信息(在本例中为摄影机预览)。 SurfaceHolder.Callback实现三种方法:
    • SurfaceChanged :更改表面的大小或格式时,将调用此方法。
    • SurfaceCreated :首先创建表面时,将调用此方法。
    • SurfaceDestroyed :破坏表面时调用。
  • CameraSource manages the camera in conjunction with an underlying detector.
    Here SurfaceView is the underlying detector.
    CameraSource.start() opens the camera and starts sending preview frames to the SurfaceView.
    CameraSource is created in the following way:
    cameraSource = new CameraSource.Builder(this, barcodeDetector)
                    .setRequestedPreviewSize(1920, 1080)
                    .setAutoFocusEnabled(true) //you should add this feature
                    .build();

    CameraSource与基础探测器一起管理摄像机。
    在这里,SurfaceView是基础检测器。
    CameraSource.start()打开相机,并开始将预览帧发送到SurfaceView。
    CameraSource是通过以下方式创建的:
  • We’ve assigned a processor on the Barcode Detector using setProcessor().
    The interface contains the callback to the method receiveDetections() which receives the QR Code from the camera preview and adds them in the SparseArray.

    我们已经使用setProcessor()在条形码检测器上分配了一个处理器。
    该接口包含对方法ReceiveDetections()的回调,该方法从摄像机预览接收QR码并将其添加到SparseArray中。
  • The value of the QR Code is displayed in the TextView using a Runnable since the Barcodes are detected in a background thread.

    由于在后台线程中检测到条形码,因此使用Runnable在TextView中显示QR Code的值。
  • In this example, we’ve created two barcodes using the generator. One contains the URL. The second contains an email address.
    On clicking the button, based on the QR code value detected, we’ll either launch the URL or send an email to the relevant email address detected from the QR Code.

    在此示例中,我们使用生成器创建了两个条形码。 一个包含URL。 第二个包含一个电子邮件地址。
    单击按钮后,基于检测到的QR码值,我们将启动URL或将电子邮件发送到从QR码检测到的相关电子邮件地址。

The output of the PictureBarcodeActivity.java is given below.

PictureBarcodeActivity.java的输出如下。

The output of the ScannedBarcodeActivity.java activity in action is given below.

运行中的ScannedBarcodeActivity.java活动的输出如下。

That’s all for QR code scanner project for android using Mobile Vision API. We’ve added the sample QR Codes to the source code. You can download the final Android QR Code Scanner/Barcode Scanner Project from the link below and play around with different QR Codes.

这就是使用Mobile Vision API的Android QR码扫描仪项目的全部内容。 我们已经将示例QR代码添加到了源代码中。 您可以从下面的链接下载最终的Android QR Code扫描仪/条形码扫描仪项目,并使用不同的QR Code。

翻译自: https://www.journaldev.com/18198/qr-code-barcode-scanner-android

扫描仪scanner接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值