生成二维码及扫描所需要的开源项目地址:https://github.com/zxing/zxing/releases
怎么配置可以自行百度或者参考本文下面其他博客的链接。
代码分析如下:
String info = "str";
//createQRCode(String str,int widthAndHeight)
Bitmap bitmap = EncodingHandler.createQRCode(info + "", 500);//生成二维码
上面info为生成的二维码内容。500为生成的二维码宽和高的大小。如果该参数大于400,建议压缩bitmap否则参数传递过程中会因为占用内存过大而报错。
public final class EncodingHandler {
private static final int BLACK = 0xff000000;
private static final int WHITE = 0xffffffff;
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight,hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}else {
pixels[y * width + x] = WHITE;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}
以上代码为生成二维码的代码。学习过图形学的应该能大致看懂,大意就是先解码str的内容,然后根据宽和高绘制一张bitmap。
/**
* 扫描二维码图片的方法
* @param path
* @return
*/
public Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
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);
int width = scanBitmap.getWidth();
int height = scanBitmap.getHeight();
int[] pixels = new int[width * height];
scanBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
/**
* 第三个参数是图片的像素
*/
RGBLuminanceSource source = new RGBLuminanceSource(width, height,
pixels);
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;
}
/**
* 处理扫描结果
* @param result
* @param barcode
*/
public void handleDecode(Result result, Bitmap barcode) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String resultString = result.getText();
onResultHandler(resultString, barcode);//处理二维码的方法
}
根据路径获取Bitmap,最后通过QRCodeReader 中的decode方法解析成Result对象并返回,最终传递给handleDecode方法。
权限:
<uses-feature android:name="android.hardware.camera" />//相机
<uses-feature android:name="android.hardware.camera.autofocus" />//聚焦
参考链接:
http://blog.csdn.net/fan7983377/article/details/51499508
http://blog.csdn.net/hai_qing_xu_kong/article/details/51260428#t1