Scan with zxing class library under Android

Source: Internet
Author: User

Program source code and executable file: Http://files.cnblogs.com/rainboy2010/zxingdemo.zip

Zxing, an extremely powerful bar code parsing class Library, the following explains how to use the Zxing class library to implement sweep sweep function, first put on a:

The main code is as follows:

1. Get the preview image in the Onpreviewframe method and pass it to Decodehandler to parse

public void Onpreviewframe (byte[] data, camera camera)   {point    cameraresolution = Configmanager.getcameraresolution ();    if (!useoneshotpreviewcallback)     {      camera.setpreviewcallback (null);    }    if (Previewhandler! = null)     {      Message message = Previewhandler.obtainmessage (Previewmessage, CAMERARESOLUTION.X,CAMERARESOLUTION.Y, data);      Message.sendtotarget ();      Previewhandler = null;    }     else     {      log.d (TAG, "Got preview callback, but no handler for it");    }  }

2. Call the Zxing class library in the Decode method to parse the image

private void Decode (byte[] data, int width, int height) {Long start = System.currenttimemillis ();        Result rawresult = null;    byte[] Rotateddata = new Byte[data.length];  for (int y = 0, y < height; y++) {for (int x = 0; x < width; x + +) rotateddata[x * height + height    -Y-1] = data[x + y * Width]; } int tmp = width;    Here we is swapping, that's the difference to #11 width = height;        height = tmp;    Planaryuvluminancesource Source = Cameramanager.get (). Buildluminancesource (rotateddata, width, height);    Binarybitmap bitmap = new Binarybitmap (new Hybridbinarizer (source));    try {rawresult = multiformatreader.decodewithstate (bitmap);    } catch (Readerexception re) {//Continue} finally {Multiformatreader.reset ();      } if (Rawresult! = null) {Long end = System.currenttimemillis ();      LOG.D (TAG, "Found Barcode (" + (End-start) + "ms): \ n" + rawresult.tostring ()); Message message =Message.obtain (Activity.gethandler (), r.id.decode_succeeded, Rawresult);      LOG.D (TAG, "Sending decode succeeded message ...");    Message.sendtotarget ();      } else {Message message = Message.obtain (Activity.gethandler (), r.id.decode_failed);    Message.sendtotarget (); }  }

Scan with zxing class library under Android

Related Article

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.