BarcodeUtils 是一个条码解析、生成、扫描库,基于 zxing 封装,适合快速在项目中集成条码相关功能
解码方法都集中在 DecodeUtils 中,包括 decodeFile()、decodeYUV()、decodeBitmap() 三种方法,如下:
Result result = DecodeUtils.decodeFile(new File("/mnt/sdcard/0/qrcode.png"));
Log.i("条码内容:", result.getText());生成主要使用的是 BarcodeCreator,如下:
String qrcodeContent = "http://baidu.com"; // 条码内容
BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE; // 条码类型为二维码
int imageWidth = 500; // 条码宽为 500
int imageHeight = 500; // 条码高为 500
Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, null);
File outFile = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + getPackageName() + File.separator + System.currentTimeMillis() + ".jpeg");
BarcodeCreator barcodeCreator = new BarcodeCreator(qrcodeContent, barcodeFormat, imageWidth, imageHeight);
barcodeCreator.setCharset("UTF-8"); // 设置编码方式
barcodeCreator.setOutFile(outFile); // 设置将生成的条码保存到文件
barcodeCreator.setOutCompressFormat(CompressFormat.JPEG); // 设置输出压缩格式
barcodeCreator.setOutCompressQuality(100); // 设置输出压缩质量
Bitmap barcodeBitmap = barcodeCreator.create(logoBitmap); // 生成条码,并且在条码中间绘制当前应用的 logo在 onCreate() 方法中创建 BarcodeScanner,如下:
// 扫描回调,后面再讲
MyBarcodeScanCallback scanCallback = new MyBarcodeScanCallback();
// 创建一个支持所有格式,编码方式为 UTF-8 并且默认扫描区域为全屏的条码扫描器
BarcodeScanner barcodeScanner = new BarcodeScanner(getBaseContext(), scanCallback);如果你想自定义扫描格式等信息,可使用其它构造函数
在你打开 Camera 以后调用 barcodeScanner.setCamera() 方法设置 Camera,如下:
Camera camera = Camera.open();
barcodeScanner.setCamera(camera);默认的扫描区域是全屏的,如果你想自定义扫描区域的话就调用 barcodeScanner.setScanAreaRectInPreview(Rect) 方法设置扫描区域,如下:
barcodeScanner.setScanAreaRectInPreview(new Rect(100, 100, 400, 250));在执行完 camera.startPreview() 后调用 barcodeScanner.start() 启动扫描,如下:
camera.startPreview();
barcodeScanner.start();private class MyBarcodeScanCallback implements BarcodeScanner.BarcodeScanCallback {
@Override
public void onFoundPossibleResultPoint(ResultPoint resultPoint) {
// 你可以在这里将可疑点绘制到你的界面上
// 具体如何绘制你可以参考 zxing 的 ViewfinderView.java 的 addPossibleResultPoint() 方法或者参考本库中的 ScanAreaView.java 的 addPossibleResultPoint() 方法
// 你还可以直接使用本库自带的 ScanAreaView.java 来作为扫描区,具体使用方式你可以参考本项目中的 BarcodeScanActivity.java
}
@Override
public boolean onDecodeCallback(final Result result, final byte[] barcodeBitmapByteArray, final float scaleFactor) {
if (result != null) {
// 找到条码
return false; //停止扫描
} else {
// 没有找到他条码
return true; //继续扫描
}
}
}在需要停止解码的时候调用 barcodeScanner.stop() 即可停止扫描,如下:
@Override
public void onStop(){
super.onStop();
barcodeScanner.stop();
}在需要释放的时候调用 barcodeScanner.release() 方法即可释放,一般情况下建议重写 Activity 的 onDestroy() 方法,在 onDestroy() 方法内部释放 BarcodeScanner
注意:当已经释放了之后再去调用 start() 方法则会抛出 IllegalStateException 异常
@Override
protected void onDestroy() {
if (barcodeScanner != null) {
barcodeScanner.release();
}
super.onDestroy();
}完整使用示例请参考 sample 源码
Copyright (C) 2017 Peng fei Pan <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.