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, the src replication under the zxing-1.6core overwrites the src of the project; in addition to the use of PlanarYUVLuminanceSource. java under the zxing-1.6android.

PS: zxing-1.6android is the source code of barcodepipeline, this program is equivalent to the lite version of barcodepipeline, only keep 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:

View plaincopy to clipboardprint?
<? 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>
<? 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:

View plaincopy to clipboardprint?
Package com. testCamera;
Import java. util. Timer;
Import java. util. TimerTask;
Import com. google. zxing. BinaryBitmap;
Import com. google. zxing. MultiFormatReader;
Import com. google. zxing. Result;
Import com. google. zxing. Android. PlanarYUVLuminanceSource;
Import com. google. zxing. common. HybridBinarizer;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. hardware. Camera;
Import android. OS. Bundle;
Import android. view. SurfaceView;
Import android. view. View;
Import android. widget. ImageView;
Import android. widget. TextView;
Public class testCamera extends Activity {
/** Called when the activity is first created .*/
Private SurfaceView sfvCamera;
Private SFHCamera sfhCamera;
Private ImageView imgView;
Private View centerView;
Private TextView txtScanResult;
Private Timer mTimer;
Private MyTimerTask mTimerTask;
// Follow the standard HVGA
Final static int width = 480;
Final static int height = 320;
Int dstLeft, dstTop, dstWidth, dstHeight;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

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.