When using the Zxing Open Source Library, found that the larger picture cannot be scanned successfully, reported the following exception:
Com.google.zxing.NotFoundException
The above problem can be solved by the scale down bitmap, 720 is a magic number that can be adjusted according to your own project
The bitmap if it is bigger than we need.int width = bm.getwidth (); int height = bm.getheight (); final int Targ Etwidth = 720, Targetheight = 720;if (Width > Targetwidth | | height > targetheight) { float scale = 0.0f; if (width >= height) {scale = (float) targetwidth/width; } else {scale = (float) Targetheight/hei ght; } int w = math.round (scale * width); int h = math.round (scale * height); BM = BITMAP.CREATESCALEDBITMAP (BM, W, H, true);}
Compare the complete code as follows:
... new Qrcodescantask (PicturePath). Execute (); Private class Qrcodescantask extends Asynctask<void, Void, Result > {private String ImagePath; Private ProgressDialog Mprogressdialog; Public Qrcodescantask (String imagePath) {this.imagepath = ImagePath; } @Override public void OnPreExecute () {mprogressdialog = new ProgressDialog (mactivity); Mprogressdialog.setmessage (mactivity.getstring (r.string.qrcode_scanning)); Mprogressdialog.show (); Public Result doinbackground (Void ... params) {try{Multiformatreader Multiformatreader = new Multiformatreader (); Bitmap BM = bitmapfactory.decodefile (ImagePath); The bitmap if it is bigger than we need. int width = bm.getwidth (); int height = bm.getheight (); Final int targetwidth = 720, targetheight = 720; if (width> Targetwidth | | Height > Targetheight) {float scale = 0.0f; if (width >= height) {scale = (float) targetwidth/width; } else {scale = (float) targetheight/height; } int w = math.round (scale * width); int h = math.round (scale * height); BM = BITMAP.CREATESCALEDBITMAP (BM, W, H, true); } Result result = Multiformatreader.decodewithstate (new Binarybitmap (New hybridbinarize R (New Bitmapluminancesource (BM))); return result; } catch (Exception e) {return null; }} @Override public void OnPostExecute (result result) {Mprogressdialog.dismiss (); if (result = = null) toast.maketext (mactivity, R.string.qrcode_notfound, Toast.length_short). Show (); else Handleqrcoderesult (result); } }
Ps:
1. There are related discussions here https://code.google.com/p/zxing/issues/detail?id=513
2. zxing GitHub link:https://github.com/zxing/zxing/
Zxing Scan from large photo