资料来源:Reading QR Codes Using the Mobile Vision API
最近的Google Play services SDK版本使得安卓开发者能创建识别二维码的app。
这个文章,将说明怎么来使用它。
必要环境:
最新版本的android studio
带相册的安卓设备
1.安装Google Play Services SDK
在项目的配件文件build.gradle中加入下列这句:
compile 'com.google.android.gms:play-services:7.8.0'
当你点击了Sync Now同步后,可能会报如下错误:
点击 Install repository and sync project 安装sdk
2.修改mainfest
在AndroidManifest.xml中加入下列代码,当你运行app时,它将会自动加入二维码库
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
权限也是必须的:
<uses-permission android:name="android.permission.CAMERA" />
3.从图片中识别二维码
先在assets目录中加入一张带二维码的图片,这里命名为 myqrcode.jpg。
1)将图片读取后生成Bitmap对象
因为api接口需要将Bitmap做为参数,所以得先把图片转成Bitmap。
参考下列代码:
Bitmap myQRCode = BitmapFactory.decodeStream(getAssets().open("myqrcode.jpg"));
2)创建识别对象
通过创建BarcodeDetector的实例对象来识别二维码(or 条形码)。
获取BarcodeDetector实例代码:
BarcodeDetector barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
注意这里默认是识别支持的条形码的。修改setBarcodeFormats 来指明需要识别的目标类型。
3)识别
通过早前的Bitmap 对象来创建Frame对象
Frame myFrame = new Frame.Builder()
.setBitmap(myQRCode)
.build();
通过调用BarcodeDetector的detect方法来识别图片并生成一个带所有二维码信息的SparseArray
SparseArray<Barcode> barcodes = barcodeDetector.detect(myFrame);
每个item都是一个条码对象。其中的displayValue 字段是识别后的信息,可以打印看看。
if(barcodes.size() != 0) {
// 打印扫描结果
Log.d("My QR Code's Data",
barcodes.valueAt(0).displayValue
);
}
4.通过相机扫描识别
用相机来扫描识别二维码也是非常简单的。
下列写一个测试例子:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp">
<SurfaceView
android:layout_width="640px"
android:layout_height="480px"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/camera_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/code_info"
android:layout_toRightOf="@+id/camera_view"
android:textSize="20sp"
android:layout_marginLeft="16dp"
android:text="Nothing to read."
android:layout_alignParentTop="true"
/>
</RelativeLayout>
activity主要代码:
cameraView = (SurfaceView)findViewById(R.id.camera_view);
barcodeInfo = (TextView)findViewById(R.id.code_info);
barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
@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() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
// 因为receiveDetections在非UI线程中执行
barcodeInfo.post(new Runnable() {
public void run() {
barcodeInfo.setText(
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
运行app后,可以看到app能很快的识别出二维码。
结论:
这篇文章主要讲述怎么通过mobile vision api来识别带二维码的图片或用相机扫描识别二维码。
当然也可以识别其它商品条形码类似UPC-A and EAN-13。
如要学习更多的mobile vision API,请阅读官方文档:documentation