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