扫描仪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)
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));