Zxing Scan from large photo

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.