二维码又称QR Code,QR全称Quick Response,是一种比一维码更高级的条码格式,能存储汉字、数字和图片等信息。
App开发中,我们常常会遇到二维码扫描功能和二维码生成功能的需求。目前,常用的方式是集成zxing这个开源项目的扫码功能,( 开源项目地址)。下面介绍集成方法。
接入步骤:
1.引入jar包
app build.gradle中加入依赖
dependencies {
...
implementation 'com.google.zxing:core:3.3.0'
}
2.如何生成二维码
直接上代码
/**
*
* @param content 字符串内容
* @param width 二维码宽度
* @param height 二维码高度
* @param character_set 编码方式(一般使用UTF-8)
* @param error_correction_level 容错率 L:7% M:15% Q:25% H:35%
* @param margin 空白边距(二维码与边框的空白区域)
* @param color_black 黑色色块
* @param color_white 白色色块
* @return BitMap
*/
public static Bitmap createQRCodeBitmap(String content, int width,int height,
String character_set,String error_correction_level, String margin,int color_black, int color_white) {
// 字符串内容判空
if (TextUtils.isEmpty(content)) {
return null;
}
// 宽和高>=0
if (width < 0 || height < 0) {
return null;
}
try {
/** 1.设置二维码相关配置 */
Hashtable hints = new Hashtable<>();
// 字符转码格式设置
if (!TextUtils.isEmpty(character_set)) {
hints.put(EncodeHintType.CHARACTER_SET, character_set);
}
// 容错率设置
if (!TextUtils.isEmpty(error_correction_level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, error_correction_level);
}
// 空白边距设置
if (!TextUtils.isEmpty(margin)) {
hints.put(EncodeHintType.MARGIN, margin);
}
/** 2.将配置参数传入到QRCodeWriter的encode方法生成BitMatrix(位矩阵)对象 */
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
/** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
//bitMatrix.get(x,y)方法返回true是黑色色块,false是白色色块
if (bitMatrix.get(x, y)) {
pixels[y * width + x] = color_black;//黑色色块像素设置
} else {
pixels[y * width + x] = color_white;// 白色色块像素设置
}
}
}
/** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,并返回Bitmap对象 */
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
主要步骤:
设置二维码相关配置,包括传入的二维码长宽、容错率和空白边距大小。
将配置参数传入到QRCodeWriter的encode方法并生成BitMatrix(位矩阵)对象。
位矩阵对象中bitMatrix.get(x, y)方法可判断是黑色色块还是白色色块,根据不同色块给数组元素赋我们传入的颜色值。
根据像素数组每个像素点的颜色值创建Bitmap对象并返回,即二维码。
主要参数介绍:
character_set
字符集/字符转码格式,通常使用UTF-8,格式不对可能导致乱码。传null时,默认使用 “ISO-8859-1”
error_correction_level
容错率,也就是纠错水平,二维码破损一部分也能扫码就归功于容错率,容错率可分为L、 M、 Q、 H四个等级,其分别占比为:L:7% M:15% Q:25% H:35%。传null时,默认使用 “L”,当然容错率越高,二维码能存储的内容也随之变小。
margin
二维码和边框的空白区域宽度
color_black、color_white
黑色色块和白素色块,我们常见的二维码一般是黑白两色的,也就是这两个色块。
3.如何识别图像中的二维码
识别图像中的二维码代码:
/**
* 扫描二维码图片的方法
* @param path
* @return
*/
public Result scanningImage(String path)
if(TextUtils.isEmpty(path)){
return null;
}
Hashtable hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); //设置二维码内容的编码
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小
scanBitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
return reader.decode(bitmap1, hints);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return null;
}
4.如何识别摄像头扫描的二维码
实现原理,在摄像头的PreviewCallback的回调函数中,可以直接获取摄像头捕获二进制YUV数据,将数据转换为BinaryBitmap对象。
将YUV数据转换为BinaryBitmap对象的方法:
首先引入类 PlanarYUVLuminanceSource, 代码如下:
import android.graphics.Bitmap;
import com.google.zxing.LuminanceSource;
/**
* This object extends LuminanceSource around an array of YUV data returned from the camera driver,
* with the option to crop to a rectangle within the full data. This can be used to exclude
* superfluous pixels around the perimeter and speed up decoding.
*
* It works for any pixel format where the Y channel is planar and appears first, including
* YCbCr_420_SP and YCbCr_422_SP.
*
* @author [email protected] (Daniel Switkin)
*/
public final class PlanarYUVLuminanceSource extends LuminanceSource {
private final byte[] yuvData;
private final int dataWidth;
private final int dataHeight;
private final int left;
private final int top;
public PlanarYUVLuminanceSource(byte[] yuvData, int dataWidth, int dataHeight, int left, int top,int width, int height) {
super(width, height);
if (left + width > dataWidth || top + height > dataHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}
this.yuvData = yuvData;
this.dataWidth = dataWidth;
this.dataHeight = dataHeight;
this.left = left;
this.top = top;
}
@Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
int offset = (y + top) * dataWidth + left;
System.arraycopy(yuvData, offset, row, 0, width);
return row;
}
@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
// If the caller asks for the entire underlying image, save the copy and give them the
// original data. The docs specifically warn that result.length must be ignored.
if (width == dataWidth && height == dataHeight) {
return yuvData;
}
int area = width * height;
byte[] matrix = new byte[area];
int inputOffset = top * dataWidth + left;
// If the width matches the full width of the underlying data, perform a single copy.
if (width == dataWidth) {
System.arraycopy(yuvData, inputOffset, matrix, 0, area);
return matrix;
}
// Otherwise copy one cropped row at a time.
byte[] yuv = yuvData;
for (int y = 0; y < height; y++) {
int outputOffset = y * width;
System.arraycopy(yuv, inputOffset, matrix, outputOffset, width);
inputOffset += dataWidth;
}
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
public int getDataWidth() {
return dataWidth;
}
public int getDataHeight() {
return dataHeight;
}
public Bitmap renderCroppedGreyscaleBitmap() {
int width = getWidth();
int height = getHeight();
int[] pixels = new int[width * height];
byte[] yuv = yuvData;
int inputOffset = top * dataWidth + left;
for (int y = 0; y < height; y++) {
int outputOffset = y * width;
for (int x = 0; x < width; x++) {
int grey = yuv[inputOffset + x] & 0xff;
pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
}
inputOffset += dataWidth;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}
转换数据代码如下:
BinaryBitmap createBinaryBitmap(byte[] data, int width, int height) {
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
}
int tmp = width; // Here we are swapping, that's the difference to #11
width = height;
height = tmp;
PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
return bitmap;
}
识别代码如下:
MultiFormatReader multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(hints);
Result rawResult;
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
Log.d(TAG, "Found barcode:\n" + rawResult.toString());
} catch (ReaderException re) {
// continue
} finally {
multiFormatReader.reset();
}
5.快速集成二维码扫描功能方法
您还可以直接集成前人写好的Demo中的代码,引入二维码扫描和生成的全部功能,最常用的Demo为QrCodeScan-master,下面介绍集成方法。
QrCodeScan-master下载地址
(1) 下载demo,拷贝demo中的com.google.zxing下的5个包和com.utils包到自己的项目中。
(2) 拷贝项目资源中的activity_scanner.xml和toolbar_scanner.xml。
(3) 拷贝项目资源中目录中的raw文件夹到本项目中,raw文件夹下的beep.ogg是扫描成功时的提示音。
(4) 拷贝或合并资源中的attrs.xml、colors.xml和ids.xml这三个文件。
(5) app build.gradle中加入依赖
dependencies {
...
implementation 'com.google.zxing:core:3.3.0'
}
(6) 修改R文件引入路径,为本项目的R文件引用地址,需要修改的文件有以下4个文件
com.google.zxing.activity.CaptureActivity
com.google.zxing.decoding.CaptureActivityHandler
com.google.zxing.decoding.DecodeHandler
com.google.zxing.view.ViewfinderView
(7) 配置权限
在 AndroidManifest.xml里增加权限申请代码:
(8) 在AndroidManifest.xml里增加摄像头扫描Activity的配置代码
(9) 完成以上步骤后,通过调用CaptureActivity就可以实现扫码功能,可参考MainActivity中的代码
打开二维码扫描界面代码:
if(CommonUtil.isCameraCanUse()){
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}else{
Toast.makeText(this,"请打开此应用的摄像头权限!",Toast.LENGTH_SHORT).show();
}
扫描结果回调代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//扫描结果回调
if (resultCode == RESULT_OK) { //RESULT_OK = -1
Bundle bundle = data.getExtras();
String scanResult = bundle.getString("qr_scan_result");
//将扫描出的信息显示出来
qrCodeText.setText(scanResult);
}
}
二维码生成代码:
try {
//获取输入的文本信息
String str = text.getText().toString().trim();
if(str != null && !"".equals(str.trim())) {
//根据输入的文本生成对应的二维码并且显示出来
Bitmap mBitmap = EncodingHandler.createQRCode(text.getText().toString(), 500);
if(mBitmap != null){
Toast.makeText(this,"二维码生成成功!",Toast.LENGTH_SHORT).show();
qrCode.setImageBitmap(mBitmap);
}
}else{
Toast.makeText(this,"文本信息不能为空!",Toast.LENGTH_SHORT).show();
}
} catch (WriterException e) {
e.printStackTrace();
}