QR code Zxing & amp; Zbar, zxing

Source: Internet
Author: User

QR code Zxing & Zbar, zxing

QR code Zxing & Zbar

 

Preface:This project mainly introduces QR code scanning, flash enabling, local QR code image recognition, and QR code generation. Zxing and zbar (grid QR code) are implemented respectively. Run the project apk for specific effects...

 

Development Environment: AndroidStudio2.2.1 + gradle-2.14.1

 

Knowledge:
1. Zxing and Zbar (GRID) QR code scanning
2. Enable and disable the flashlight
3. Local QR code recognition
4. QR code generation
5. Handler Mechanism
6. butterknife annotation-based development

 

Introduce dependency:

 

    compile 'com.android.support:appcompat-v7:22.+'    compile 'com.google.zxing:core:3.2.1'    compile 'com.jakewharton:butterknife:7.0.1'    compile files('libs/zbar.jar')

 

Some code:

/*** Zbar QR code scan + flashlight + local QR code recognition */public class ZbarActivity extends AppCompatActivity implements QRCodeView. delegate {@ Bind (R. id. zbarview) ZBarView mQRCodeView; @ Bind (R. id. scancode_lamplight) ToggleButton toggleButton; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. zbartest_scan_layout); ButterKnife. bind (this); initLayout ();} Private void initLayout () {mQRCodeView. setDelegate (this); mQRCodeView. startSpotAndShowRect (); // display the scan box and start to recognize toggleButton after 1.5 seconds. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked) {mQRCodeView. openFlashlight ();} else {mQRCodeView. closeFlashlight () ;}}) ;}@ OnClick ({R. Id. line_back, R. id. scancode_localimg}) protected void onClickBtn (View view) {switch (view. getId () {case R. id. line_back: finish (); break; case R. id. scancode_localimg: Intent intent = new Intent (); intent. setAction (Intent. ACTION_PICK); intent. setType ("image/*"); startActivityForResult (intent, 0x11); break; default: break ;}@override protected void onActivityResult (int requestCode, int resultCo De, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (resultCode = RESULT_ OK & requestCode = 0x11) {Uri uri = data. getData (); String path = null; if (! TextUtils. isEmpty (uri. getAuthority () {Cursor cursor = getContentResolver (). query (uri, new String [] {MediaStore. images. media. DATA}, null); if (null = cursor) {Toast. makeText (ZbarActivity. this, "image not found", Toast. LENGTH_SHORT ). show (); return;} cursor. moveToFirst (); path = cursor. getString (cursor. getColumnIndex (MediaStore. images. media. DATA); cursor. close ();} else {path = uri. getPath ();} If (null! = Path) {codeDiscriminate (path);} else {Toast. makeText (ZbarActivity. this, "the image path is empty", Toast. LENGTH_SHORT ). show (); return ;}}@ Override protected void onRestart () {mQRCodeView. startCamera (); super. onRestart () ;}@ Override public void onResume () {super. onResume (); mQRCodeView. startSpotAndShowRect (); // display the scan box and start to recognize it after 1.5 seconds.} @ Override protected void onStop () {mQRCodeView. stopCamera (); // mQRCodeVi Ew. closeFlashlight (); super. onStop ();} private void vibrate () {Vibrator vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE); vibrator. vibrate (200);} private void codeDiscriminate (final String path) {new Thread (new Runnable () {@ Override public void run () {Looper. prepare (); String result = null; if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.KITKAT) {result = QRCodeDecoder. syncDecodeQR Code (path);} else {result = QRCodeDecoder. syncDecodeQRCode2 (path);} Log. I ("zbar_result", Build. VERSION. SDK_INT + "--->" + result); Message msg = mHandler. obtainMessage (); // encapsulate the Message id msg. what = 1; // as the identifier to facilitate message receiving msg. obj = result; mHandler. sendMessage (msg); // send message }}). start () ;}// create a local Hander Class Object and use the handleMessage () Hook method to update the UI Control Handler mHandler = new Handler () {public void handleMessage (Message msg ){ // Obtain the id of the encapsulated message to match if (1 = msg. what) {if (null! = Msg. obj) onScanQRCodeSuccess (msg. obj. toString () ;}};@ Override protected void onDestroy () {mQRCodeView. onDestroy (); ButterKnife. unbind (this); super. onDestroy () ;}@ Override public void onScanQRCodeSuccess (String result) {Log. I ("zbar_result", "result:" + result); Toast. makeText (this, "QR code data:" + result, Toast. LENGTH_SHORT ). show (); vibrate (); mQRCodeView. startSpot () ;}@ Override public void onScanQRCodeOpenCameraError () {Log. e ("zbar_result", "camera error"); Toast. makeText (this, "An error occurred while opening the camera", Toast. LENGTH_SHORT ). show ();}}

 

 

Download source code...

 

 

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.