Use ZXing on Android to identify barcode/QR code

Source: Internet
Author: User

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.

For example, there are many source files in ChecksumException. java, which are not listed yet:

 

 

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 Code

<?xml version="1.0" encoding="utf-8"?><FrameLayout android:id="@+id/FrameLayout01"android:layout_width="fill_parent" android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><SurfaceView android:layout_height="fill_parent"android:id="@+id/sfvCamera" android:layout_width="fill_parent"></SurfaceView><RelativeLayout android:id="@+id/RelativeLayout01"android:layout_height="fill_parent"android:layout_width="fill_parent"><ImageView android:id="@+id/ImageView01"android:layout_height="100dip" android:layout_width="160dip"></ImageView><View android:layout_centerVertical="true"android:layout_centerHorizontal="true" android:layout_width="300dip"android:background="#55FF6666" android:id="@+id/centerView"android:layout_height="180dip"></View><TextView android:layout_centerHorizontal="true"android:layout_width="wrap_content"android:layout_below="@+id/centerView"android:layout_height="wrap_content" android:text="Scanning..."android:id="@+id/txtScanResult" android:textColor="#FF000000"></TextView></RelativeLayout></FrameLayout>

TestCamera. java is the main class that controls Camera and decodes images. The source code is as follows:

 

Java code Package com. testbarcode. android; </p> <p> 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. testbarcode. 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 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 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, false); <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 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/>}< br/>

SFHCamera. java is a Camera control class. The source code is as follows:

 

Java code Package com. testbarcode. android; </p> <p> 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 Cam Era. previewCallback previewCallback; </p> <p> public SFHCamera (SurfaceHolder holder, int w, int h, Camera. previewCallback 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 surfaceChan Ged (SurfaceHolder arg0, int arg1, int arg2, int arg3) {<br/> Camera. parameters 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 (S UrfaceHolder 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 preview <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 camera) {<br/> if (success) {// focus successful, callback Camera. previewCallback <br/> mCamera. setOneShotPreviewCallback (previewCallback); <br/>}< br/>}; </p> <p >}</p> <p>

 

Among them, Camera. PreviewCallback 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.

 

 

---------------------------------

This article from javaeye (http://17zouguo.javaeye.com/blog/857349), the program running is not very good, but also need to improve.

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.