Integrated development of Android QR code scanning function

Source: Internet
Author: User

The development of QR code mainly relies on zxing open source project

Project Address: https://github.com/zxing/zxing

This open source project can scan one-dimensional, and two-dimensional code,

One-dimensional code refers to the barcode behind the book.

First configure the Zxing library and Android engineering

The project itself is very large, we only need to use the Lite library

After import

The Testqrcode project is used as a library by other projects.

Barcodetestactivity

 PackageCom.ericssonlabs;Importcom.google.zxing.WriterException;Importcom.zxing.activity.CaptureActivity;ImportCom.zxing.encoding.EncodingHandler;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classBarcodetestactivityextendsActivity {/**Called when the activity is first created.*/    PrivateTextView Resulttextview; PrivateEditText Qrstredittext; PrivateImageView Qrimgimageview; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.main); Resulttextview= (TextView) This. Findviewbyid (R.id.tv_scan_result); Qrstredittext= (EditText) This. Findviewbyid (r.id.et_qr_string); Qrimgimageview= (ImageView) This. Findviewbyid (R.id.iv_qr_image); Button Scanbarcodebutton= (Button) This. Findviewbyid (R.id.btn_scan_barcode); Scanbarcodebutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Scan barcode or QR code by opening scan interfaceIntent opencameraintent =NewIntent (barcodetestactivity. This, Captureactivity.class); Startactivityforresult (Opencameraintent,0);                }        }); Button Generateqrcodebutton= (Button) This. Findviewbyid (R.id.btn_add_qrcode); Generateqrcodebutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Try{String contentstring=Qrstredittext.gettext (). toString (); if(!contentstring.equals ("")) {                        //generate a two-dimensional code image from a string and display it on the interface, the second parameter is the size of the picture (350*350)Bitmap Qrcodebitmap = Encodinghandler.createqrcode (contentstring, 350);                    Qrimgimageview.setimagebitmap (QRCODEBITMAP); }Else{toast.maketext (barcodetestactivity). This, "Text can is not being empty", Toast.length_short). Show (); }                                    } Catch(writerexception e) {//TODO auto-generated Catch blockE.printstacktrace ();    }            }        }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); //processing Scan results (displayed on the interface)        if(ResultCode = =RESULT_OK) {Bundle Bundle=Data.getextras (); String Scanresult= bundle.getstring ("Result");        Resulttextview.settext (Scanresult); }    }}

Create a project Testqrcode to use the Barcodetest library

XML file

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/container"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.jikexueyuan.testqrcode.MainActivity"Tools:ignore= "Mergerootframe" >    <ButtonAndroid:id= "@+id/scan"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Start scanning QR Code" />    <TextViewAndroid:id= "@+id/text"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Show"        />   <EditTextAndroid:id= "@+id/input"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the content to encode"       />   <ButtonAndroid:id= "@+id/gen"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Generate two-dimensional code"       />   <ImageViewAndroid:id= "@+id/img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal"       /></LinearLayout>

Mainactivity

  PackageCom.jikexueyuan.testqrcode;Importjunit.framework.Test;Importcom.google.zxing.WriterException;Importcom.zxing.activity.CaptureActivity;ImportCom.zxing.encoding.EncodingHandler;Importandroid.app.Activity;ImportAndroid.app.ActionBar;Importandroid.app.Fragment;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportAndroid.os.Build; Public classMainactivityextendsActivity {PrivateButton Scanbutton; PrivateTextView text; PrivateEditText input; PrivateButton Genbutton; PrivateImageView img; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Scanbutton=(Button) Findviewbyid (R.id.scan); Text=(TextView) Findviewbyid (R.id.text); Scanbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {toast.maketext (mainactivity. This, "You can scan barcodes or QR codes.", Toast.length_short). Show (); Intent Startscan=NewIntent (mainactivity. This, captureactivity.class); //startactivity (startscan);Startactivityforresult (Startscan, 0);        }        }); Input=(EditText) Findviewbyid (r.id.input); Genbutton=(Button) Findviewbyid (R.id.gen); IMG=(ImageView) Findviewbyid (r.id.img); Genbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {String in=Input.gettext (). toString (); if(In.equals ("") {toast.maketext (mainactivity). This, "Please enter text", Toast.length_short). Show (); } Else {                    Try{Bitmap QRCode= Encodinghandler.createqrcode (In, 400);                    Img.setimagebitmap (QRCode); } Catch(writerexception e) {e.printstacktrace ();    }                }            }        }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode = =RESULT_OK) {String result= Data.getextras (). getString ("Result");        Text.settext (result); }    }}

Register Captureactivity.java

< Activity             android:configchanges = "Orientation|keyboardhidden"             android:name= "com.zxing.activity.CaptureActivity"            android:screenorientation  = "Portrait"            android:theme= "@android: Style/theme.notitlebar.fullscreen"              android:windowsoftinputmode= "Statealwayshidden">         </activity>

Also add two permissions, one is vibration, the other is a camera

    <android:name= "Android.permission.CAMERA"/>    <  android:name= "Android.permission.VIBRATE"/>

: Link: Http://pan.baidu.com/s/1uJVII Password: 0uof

Integrated development of Android QR code scanning function

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.