ZXing這個第三方是我用的第三方,用來掃描二維碼的,之前沒有看到ZXing還提供了掃描本地圖片二維碼的功能,現在整理如下。
跳轉到選取本地圖片的頁面
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("iamge/*");
startActivityForResult(intent, 300);
然後選取圖片之後來到onActivityResult回調
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
//獲取照片數據
Bitmap camera = data.getParcelableExtra("data");
iv1.setImageBitmap(camera);
}
if (requestCode == 200) {
if (data != null) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
iv1.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
if (requestCode == 300) {
String photoPath = getRealFilePath(this, data.getData());
if (photoPath == null) {
LogUtil.fussenLog().d("路徑獲取失敗");
} else {
//解析圖片
prasePhoto(photoPath);
}
}
}
我們只用關心requestCode 爲 300 的這種情況
裏面有兩個自定義的方法 分別是:
getRealFilePath 方法 和 解析圖片的 prasePhoto 方法
getRealFilePath方法 是用來獲取選取的圖片的絕對地址
具體方法在之前的一篇文章介紹過:根據uri獲取文件路徑
這裏直接貼出來方法:
private String getRealFilePath(Context c, Uri uri) {
String result;
Cursor cursor = c.getContentResolver().query(uri,
new String[]{MediaStore.Images.ImageColumns.DATA},//
null, null, null);
if (cursor == null) result = uri.getPath();
else {
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(index);
cursor.close();
}
return result;
}
其實之前都是準備工作,接下來就是主要的核心內容了,就是用ZXing掃描一直路徑的圖片的功能了
就是parsePhoto方法 由於這個方法是耗時的 所以需要用AsyncTask 或者Rxjava 這裏用的是AsyncTask
具體代碼如下:
private void prasePhoto(final String path) {
AsyncTask myTask = new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... params) {
// 解析二維碼/條碼
return QRCodeDecoder.syncDecodeQRCode(path);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (null == s) {
LogUtil.fussenLog().d("圖片獲取失敗,請重試");
} else {
// 識別出圖片二維碼/條碼,內容爲s
btn3.setText(s);
LogUtil.fussenLog().d(s);
}
}
}.execute(path);
}
裏面有一個類是自己寫的 QRCodeDecoder 裏面是最最重要的代碼 如下:
public class QRCodeDecoder {
public static final Map<DecodeHintType, Object> HINTS = new EnumMap<>(DecodeHintType.class);
static {
List<BarcodeFormat> allFormats = new ArrayList<>();
allFormats.add(BarcodeFormat.AZTEC);
allFormats.add(BarcodeFormat.CODABAR);
allFormats.add(BarcodeFormat.CODE_39);
allFormats.add(BarcodeFormat.CODE_93);
allFormats.add(BarcodeFormat.CODE_128);
allFormats.add(BarcodeFormat.DATA_MATRIX);
allFormats.add(BarcodeFormat.EAN_8);
allFormats.add(BarcodeFormat.EAN_13);
allFormats.add(BarcodeFormat.ITF);
allFormats.add(BarcodeFormat.MAXICODE);
allFormats.add(BarcodeFormat.PDF_417);
allFormats.add(BarcodeFormat.QR_CODE);
allFormats.add(BarcodeFormat.RSS_14);
allFormats.add(BarcodeFormat.RSS_EXPANDED);
allFormats.add(BarcodeFormat.UPC_A);
allFormats.add(BarcodeFormat.UPC_E);
allFormats.add(BarcodeFormat.UPC_EAN_EXTENSION);
HINTS.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);
HINTS.put(DecodeHintType.POSSIBLE_FORMATS, allFormats);
HINTS.put(DecodeHintType.CHARACTER_SET, "utf-8");
}
private QRCodeDecoder() {
}
/**
* 同步解析本地圖片二維碼。該方法是耗時操作,請在子線程中調用。
*
* @param picturePath 要解析的二維碼圖片本地路徑
* @return 返回二維碼圖片裏的內容 或 null
*/
public static String syncDecodeQRCode(String picturePath) {
return syncDecodeQRCode(getDecodeAbleBitmap(picturePath));
}
/**
* 同步解析bitmap二維碼。該方法是耗時操作,請在子線程中調用。
*
* @param bitmap 要解析的二維碼圖片
* @return 返回二維碼圖片裏的內容 或 null
*/
public static String syncDecodeQRCode(Bitmap bitmap) {
Result result = null;
RGBLuminanceSource source = null;
try {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
source = new RGBLuminanceSource(width, height, pixels);
result = new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(source)), HINTS);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
if (source != null) {
try {
result = new MultiFormatReader().decode(new BinaryBitmap(new GlobalHistogramBinarizer(source)), HINTS);
return result.getText();
} catch (Throwable e2) {
e2.printStackTrace();
}
}
return null;
}
}
/**
* 將本地圖片文件轉換成可解碼二維碼的 Bitmap。爲了避免圖片太大,這裏對圖片進行了壓縮。感謝 https://github.com/devilsen 提的 PR
*
* @param picturePath 本地圖片文件路徑
* @return
*/
private static Bitmap getDecodeAbleBitmap(String picturePath) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, options);
int sampleSize = options.outHeight / 400;
if (sampleSize <= 0) {
sampleSize = 1;
}
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(picturePath, options);
} catch (Exception e) {
return null;
}
}
}
以上就是本文最最重要的代碼整理成工具類
裏面三個方法實際上就是 先把傳入的圖片的絕對地址轉換成Bitmap,然後把Bitmap用zxing裏面的方法讀取圖片的二維碼掃描結果然後在整合返回就可以了,實際上只調用最上面那個syncDecodeQRCode這個方法就可以了。
----------------------------------------------------------------------------------------------------------------
Ps,別忘了這個是耗時任務,所以有些要考慮的別忘考慮了。