Use zxing on Android to identify barcode/QR code

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

More and more mobile phones are equipped with auto-focus shooting functions, which also means these mobile phones can have bar code scanning functions .......The mobile phone has the bar code scanning function, which can optimize the shopping process and quickly store e-business cards (QR codes )..

This article uses zxing 1.6 for bar code/QR code recognition. Zxing is a very classic bar code/QR code recognition open source class library, long ago, there are developers on the use of zxing on the j2s, but to support JSR-234 specifications (Auto Focus) and many Android phones have the auto focus function.

The result of running the code in this article is as follows. When the mobile assistant 91 is used, real-time surfaceview images cannot be captured:

This article uses the zxing1.6 core, that is, to overwrite the SRC replication under the/zxing-1.6/CORE/Project SRC; also use the/zxing-1.6/Android/planaryuvluminancesource. java.

PS:/zxing-1.6/Android/is the barcodepipeline source code, this program is equivalent to the barcodepipeline Lite version, only retain the most basic recognition function.

The source project of this article is as follows: http://www.pudn.com/downloads349/sourcecode/comm/android/detail1521939.html

For example, there are many source files in checksumexception. Java, which are not listed yet:

In this example, you must enable the camera and auto focus. Otherwise, an exception will be reported during startup. The permissions used are as follows:

<Uses-Permission Android: Name = "android. permission. camera "> </uses-Permission> <br/> <uses-Permission Android: Name =" android. permission. write_external_storage "> </uses-Permission> <br/> <uses-feature Android: Name =" android. hardware. camera "/> <br/> <uses-feature Android: Name =" android. hardware. camera. autofocus "/>

The main. xml source code is as follows. The main. xml must use framelayout to overlap the control to implement the "range box" effect:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <framelayout Android: Id = "@ + ID/framelayout01" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" <br/> xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <surfaceview Android: layout_height = "fill_parent" <br/> Android: id = "@ + ID/sfvcamera" Android: layout_width = "fill_parent"> </surfaceview> <br/> <relativelayout Android: id = "@ + ID/relativelayout01" <br/> Android: layout_height = "fill_parent" Android: layout_width = "fill_parent"> <br/> <imageview Android: id = "@ + ID/imageview01" <br/> Android: layout_height = "100dip" Android: layout_width = "160dip"> </imageview> <br/> <view Android: layout_centervertical = "true" <br/> Android: layout_centerhorizontal = "true" Android: layout_width = "300dip" <br/> Android: Background = "#55ff6666" Android: id = "@ + ID/centerview" <br/> Android: layout_height = "180dip"> </View> <br/> <textview Android: layout_centerhorizontal = "true" <br/> Android: layout_width = "wrap_content" Android: layout_below = "@ + ID/centerview" <br/> Android: layout_height = "wrap_content" Android: TEXT = "scanning... "<br/> Android: Id =" @ + ID/txtscanresult "Android: textcolor = "# ff000000"> </textview> <br/> </relativelayout> <br/> </framelayout>

Testcamera. Java is the main class that controls camera and decodes images. The source code is as follows:

Package COM. testcamera; <br/> Import Java. util. timer; <br/> Import Java. util. timertask; <br/> Import COM. google. zxing. binarybitmap; <br/> Import COM. google. zxing. multiformatreader; <br/> Import COM. google. zxing. result; <br/> Import COM. google. zxing. android. planaryuvluminancesource; <br/> Import COM. google. zxing. common. hybridbinarizer; <br/> Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. hardware. camera; <br/> Import android. OS. bundle; <br/> Import android. view. surfaceview; <br/> Import android. view. view; <br/> Import android. widget. imageview; <br/> Import android. widget. textview; <br/> public class testcamera extends activity {<br/>/** called when the activity is first created. */<br/> private surfaceview sfvcamera; <br/> private sfhcamera; <br/> private imageview imgview; <br/> private view centerview; <br/> private textview txtscanresult; <br/> private timer mtimer; <br/> private mytimertask mtimertask; <br/> // follow the standard hvga <br/> final static int width = 480; <br/> final static int Height = 320; <br/> int dstleft, dsttop, dstwidth, dstheight; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> This. settitle ("android barcode/QR code recognition demo ----- hellogv"); <br/> imgview = (imageview) This. findviewbyid (R. id. imageview01); <br/> centerview = (View) This. findviewbyid (R. id. centerview); <br/> sfvcamera = (surfaceview) This. findviewbyid (R. id. sfvcamera); <br/> sfhcamera = new sfhcamera (sfvcamera. getholder (), width, height, <br/> previewcallback); <br/> txtscanresult((textview1_this.findviewbyid(r.id.txt scanresult ); <br/> // initialization timer <br/> mtimer = new timer (); <br/> mtimertask = new mytimertask (); <br/> mtimer. schedule (mtimertask, 0, 80 ); <br/>}</P> <p> class mytimertask extends timertask {<br/> @ override <br/> Public void run () {<br/> If (dstleft = 0) {// assign values only once <br/> dstleft = centerview. getleft () * width <br/>/getwindowmanager (). getdefadisplay display (). getwidth (); <br/> dsttop = centerview. gettop () * height <br/>/getwindowmanager (). getdefadisplay display (). getheight (); <br/> dstwidth = (centerview. getright ()-centerview. getleft () * width <br/>/getwindowmanager (). getdefadisplay display (). getwidth (); <br/> dstheight = (centerview. getbottom ()-centerview. gettop () * height <br/>/getwindowmanager (). getdefadisplay display (). getheight (); <br/>}< br/> sfhcamera. autofocusandpreviewcallback (); <br/>}< br/>/** <br/> * output image after auto focus <br/> */<br/> private camera. previewcallback = new camera. previewcallback () {<br/> @ override <br/> Public void onpreviewframe (byte [] data, camera arg1) {<br/> // get the data of frames in the specified range <br/> planaryuvluminancesource source = new planaryuvluminancesource (<br/> data, width, height, dstleft, dsttop, dstwidth, dstheight); <br/> // obtain the grayscale image <br/> bitmap mbitmap = source. rendercroppedgreyscalebitmap (); <br/> // displays the grayscale image <br/> imgview. setimagebitmap (mbitmap); <br/> binarybitmap bitmap = new binarybitmap (New hybridbinarizer (source); <br/> multiformatreader reader = new multiformatreader (); <br/> try {<br/> result = reader. decode (Bitmap); <br/> string strresult = "barcodeformat:" <br/> + result. getbarcodeformat (). tostring () + "text:" <br/> + result. gettext (); <br/> txtscanresult. settext (strresult); <br/>} catch (exception e) {<br/> txtscanresult. settext ("scanning"); <br/>}< br/>}; <br/>}

Sfhcamera. Java is a camera control class. The source code is as follows:

Package COM. testcamera; <br/> Import Java. io. ioexception; <br/> Import android. graphics. pixelformat; <br/> Import android. hardware. camera; <br/> Import android. util. log; <br/> Import android. view. surfaceholder; <br/> public class sfhcamera implements surfaceholder. callback {<br/> private surfaceholder holder = NULL; <br/> private camera mcamera; <br/> private int width and height; <br/> private camera. previewc Allback previewcallback; </P> <p> Public sfhcamera (surfaceholder holder, int W, int H, camera. previewcallback) {<br/> This. holder = holder; <br/> This. holder. addcallback (this); <br/> This. holder. settype (surfaceholder. surface_type_push_buffers); <br/> width = W; <br/> Height = H; <br/> This. previewcallback = previewcallback; <br/>}</P> <p> @ override <br/> Public void surfacechanged (surfaceholder ARG 0, int arg1, int arg2, int arg3) {<br/> camera. parameters = mcamera. getparameters (); <br/> parameters. setpreviewsize (width, height); // set the size <br/> parameters. setpictureformat (pixelformat. JPEG); <br/> mcamera. setparameters (parameters); <br/> mcamera. startpreview (); // start previewing <br/> log. E ("camera", "surfacechanged"); <br/>}< br/> @ override <br/> Public void surfacecreated (surfaceholder arg0) {<br/> Mcamera = camera. open (); // start the service <br/> try {<br/> mcamera. setpreviewdisplay (holder); // sets preview <br/> log. E ("camera", "surfacecreated"); <br/>} catch (ioexception e) {<br/> mcamera. release (); // release <br/> mcamera = NULL; <br/>}</P> <p >}< br/> @ override <br/> Public void surfacedestroyed (surfaceholder arg0) {<br/> mcamera. setpreviewcallback (null); <br/> mcamera. stoppreview (); // stop previewing <br/> mcamera = NULL; <br/> Log. E ("camera", "surfacedestroyed"); <br/>}< br/>/** <br/> * auto focus and callback camera. previewcallback <br/> */<br/> Public void autofocusandpreviewcallback () <br/>{< br/> If (mcamera! = NULL) <br/> mcamera. autofocus (mautofocuscallback); <br/>}</P> <p>/** <br/> * auto focus <br/> */<br/> private camera. autofocuscallback mautofocuscallback = new camera. autofocuscallback () {</P> <p> @ override <br/> Public void onautofocus (Boolean success, camera) {<br/> If (SUCCESS) {// focus successful, callback camera. previewcallback <br/> mcamera. setoneshotpreviewcallback (previewcallback); <br/>}< br/>}; </P> <p >}< br/>

Among them, camera. previewcallback of testcamera. Java is the logic core of the entire program and serves as a callback function to call the internal camera class of sfhcamera. java.

 

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.