implementation'com.google.zxing:core:3.3.0' //庫
使用如下:
try{
Result ss = parsePic(bitmap);//解析二維碼
? ? Log.i("erwe", ss.getText().toString() +"");
}catch (Exception e){
e.printStackTrace();
}
public ResultparsePic(Bitmap bitmaps) { //解析函數(shù)
//解析轉(zhuǎn)換類型UTF-8
? ? Hashtable hints =new Hashtable();
? ? hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
? ? //新建一個(gè)RGBLuminanceSource對(duì)象亏狰,將bitmap圖片傳給此對(duì)象
? ? RGBLuminanceSource rgbLuminanceSource =new RGBLuminanceSource(bitmaps);
? ? //將圖片轉(zhuǎn)換成二進(jìn)制圖片
? ? BinaryBitmap binaryBitmap =new BinaryBitmap(new HybridBinarizer(rgbLuminanceSource));
? ? //初始化解析對(duì)象
? ? QRCodeReader reader =new QRCodeReader();
? ? //開始解析
? ? Result result =null;
? ? try {
result = reader.decode(binaryBitmap, hints);
? ? }catch (Exception e) {
e.printStackTrace();
? ? }
return result;
}
public class RGBLuminanceSourceextends LuminanceSource {
private byte bitmapPixels[];
? ? protected RGBLuminanceSource(Bitmap bitmap) {
super(bitmap.getWidth(), bitmap.getHeight());
? ? ? ? // 首先,要取得該圖片的像素?cái)?shù)組內(nèi)容
? ? ? ? int[] data =new int[bitmap.getWidth() * bitmap.getHeight()];
? ? ? ? this.bitmapPixels =new byte[bitmap.getWidth() * bitmap.getHeight()];
? ? ? ? bitmap.getPixels(data, 0, getWidth(), 0, 0, getWidth(), getHeight());
? ? ? ? // 將int數(shù)組轉(zhuǎn)換為byte數(shù)組,也就是取像素值中藍(lán)色值部分作為辨析內(nèi)容
? ? ? ? for (int i =0; i < data.length; i++) {
this.bitmapPixels[i] = (byte) data[i];
? ? ? ? }
}
@Override
? ? public byte[]getMatrix() {
// 返回我們生成好的像素?cái)?shù)據(jù)
? ? ? ? return bitmapPixels;
? ? }
@Override
? ? public byte[]getRow(int y, byte[] row) {
// 這里要得到指定行的像素?cái)?shù)據(jù)
? ? ? ? System.arraycopy(bitmapPixels, y * getWidth(), row, 0, getWidth());
? ? ? ? return row;
? ? }
}