Program source code and executable file: Http://files.cnblogs.com/rainboy2010/zbardemo.zip
Android commonly used barcode Scanning class library has zxing and Zbaro, compared, found that zbar scanning speed faster, can identify more barcode types, the following explains how to use the Zbar implementation sweep, 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
@Override public void Onpreviewframe (byte[] data, camera camera) {if (!useoneshotpreviewcallback) { Camera.setpreviewcallback (NULL); } if (Previewhandler! = null) {camera.parameters Parameters = Camera.getparameters (); Size size = Parameters.getpreviewsize (); Message message = Previewhandler.obtainmessage (Previewmessage, size.width,size.height, data); Message.sendtotarget (); Previewhandler = null; } Else { log.d (TAG, "Got preview callback, but no handler for it"); } }
2. Call the Zbar class library in the Decode method to parse the image
private void Decode (byte[] data, int width, int height) {Image barcode = new Image (width, height, "Y800"); Rect scanimagerect = Activity.getviewfinderview (). Getscanimagerect (Height,width); Barcode.setcrop (Scanimagerect.top, Scanimagerect.left, Scanimagerect.bottom, scanimagerect.right); Barcode.setdata (data); int result = Scanner.scanimage (barcode); String strresult= ""; if (Result! = 0) {Symbolset syms = Scanner.getresults (); for (Symbol sym:syms) {strresult=sym.getdata (). Trim (); if (!strresult.isempty ()) {break; }}} if (!strresult.isempty ()) {Message message = Message.obtain (Activity.gethandler (), R.id.dec ode_succeeded, strresult);//message information is transmitted, a bit around log.d (TAG, "Sending decode succeeded message ..."); Message.sendtotarget (); } else {Message message = Message.obtain (Activity.gethandler (), r.id.decode_failed); Message.sendtotarget (); } }
Scan with Zbar class library under Android