Today, to learn how to implement two-dimensional code sweep sweep and the generation of two-dimensional code.
To realize the two-dimensional code sweep sweep and the generation of two-dimensional code, it is necessary to understand the characteristics of the two-dimensional code.
QRCode:QRCode Introduction:
- QRCode Full name Quick Response Code.
- Encode by using black and white pixels within a rectangular region.
- High error correction, high availability, and high recognition.
zxing Introduction:
- Zxing is an open source, implemented in Java in a variety of formats for the 1D/2D Barcode Graphics processing library, which contains links to other languages of the port.
- Zxing can be implemented using the phone's built-in camera to complete the barcode scanning and decoding.
zxing Project Address:
- Http://github.com/zxing/zxing
the source code for the Zxing project is particularly large, with more than 120 m. The Zxing feature is particularly powerful, it not only scans the QR code, it can also scan other encodings. It is not only available on Android, but also on other platforms such as Java SE, JavaScript and many other platforms. Such a large source of code is difficult for us to use, so we have to streamline this source code. Extract the encoding and decoding functions we need. as a result, there are many great gods on the internet that have already put forward the encoding and decoding of Android scanning in zxing. has become a third-party class library that provides us with the use. : http://download.csdn.net/detail/btt2013/9502843Sweep--Parse the QR code:we will sweep the third-party library into our engineering space and create a demo project that we sweep.
Source code implementation:layout file: Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:tools=" Http://schemas.android.com/tools " android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:orientation= "vertical" tools:context= " Com.xbmu.scan.MainActivity "> <button android:layout_width=" match_parent " android:layout_ height= "Wrap_content" android:text= "open sweep" android:onclick= "Startscan"/> <textview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "18SP" android:text= "Result:"/> <textview android:id= "@+id/tv_result" android:layout_width= " Match_parent " android:layout_height=" wrap_content " android:textsize=" 20SP "/></linearlayout>
Mainactivity.java
Package Com.xbmu.scan;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.view.view;import Android.widget.textview;import Com.xys.libzxing.zxing.activity.captureactivity;public Class Mainactivity extends Activity {private TextView tvresult; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tvresult = (TextView) Findviewbyid (R.id.tv_result); /** * Click the Sweep button to open the scan QR code * @param view */public void Startscan (view view) {//jump to sweep Starta Ctivityforresult (New Intent (Mainactivity.this, Captureactivity.class), 0); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityr Esult (Requestcode, ResultCode, data); if (RESULTCODE==RESULT_OK) {Bundle bundle = Data.getextras (); String result = bundle.getstring ("ResULT "); Tvresult.settext (result); } }}Where: String result = bundle.getstring ("result"); In this code, "result" can be read according to the source of the scan to know:
To this, it realizes the function of scanning QR code. we can generate two-dimensional code from this website (http://cli.im/):
install this demo on our phone to scan the QR code. Operating Effect:
generate two-dimensional code:Two-dimensional code without logo:layout file: Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:orientation= "vertical" tools:context= "com.xbmu.scan.MainActivity" > <button A Ndroid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Open Sweep" Android: onclick= "Startscan"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:textsize=" 18sp "android:text=" Result: "/> <textview android:id=" @+id/tv_result " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:textsize= "20sp"/> <edittext android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:hint= " Please enter the text "android:id=" @+id/et_iNput_text "android:textsize=" 22sp "/> <button android:layout_width=" Match_parent "Android:lay out_height= "Wrap_content" android:text= "generate two-dimensional code for input text" android:onclick= "Makeqrcode"/> <imageview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/iv_result" android:background= "@drawable/ic_launcher" android:layout_gravity= "center"/></linearlayout>
Mainactivity.java
Package Com.xbmu.scan;import Android.app.activity;import Android.content.intent;import android.graphics.Bitmap; Import Android.os.bundle;import android.view.view;import android.widget.edittext;import Android.widget.ImageView; Import Android.widget.textview;import Android.widget.toast;import com.xys.libzxing.zxing.activity.CaptureActivity Import Com.xys.libzxing.zxing.encoding.encodingutils;public class Mainactivity extends Activity {private TextView TvR Esult; Private EditText Etinputtext; Private ImageView Ivresult; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tvresult = (TextView) Findviewbyid (R.id.tv_result); Etinputtext = (EditText) Findviewbyid (R.id.et_input_text); Ivresult = (ImageView) Findviewbyid (R.id.iv_result); /** * Click the Sweep button to open the scan QR code * @param view */public void Startscan (view view) {//jump to sweep StartaCtivityforresult (New Intent (Mainactivity.this, Captureactivity.class), 0); /** * generates QR code * @param view */public void Makeqrcode (view view) {String Inputtext = Etinputtext. GetText (). toString (); if (Inputtext.equals (")") {Toast.maketext (Mainactivity.this, "the text entered cannot be empty", 0). Show (); }else{Bitmap Bitmap = Encodingutils.createqrcode (Inputtext, $, n, NULL); Ivresult.setimagebitmap (bitmap); }} @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onacti Vityresult (Requestcode, ResultCode, data); if (RESULTCODE==RESULT_OK) {Bundle bundle = Data.getextras (); String result = bundle.getstring ("result"); Tvresult.settext (result); } }}Operating effect:
Two-dimensional code with logo:Layout file Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:orientation= "vertical" tools:context= "com.xbmu.scan.MainActivity" > <button A Ndroid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Open Sweep" Android: onclick= "Startscan"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:textsize=" 18sp "android:text=" Result: "/> <textview android:id=" @+id/tv_result " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:textsize= "20sp"/> <edittext android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:hint= " Please enter the text "android:id=" @+id/et_iNput_text "android:textsize=" 22sp "/> <button android:layout_width=" Match_parent "Android:lay out_height= "Wrap_content" android:text= "generate two-dimensional code for input text" android:onclick= "Makeqrcode"/> <checkbox Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/cb_select" Android:text= "with logo"/> <imageview android:layout_width= "wrap_content" android:layout_height= "Wra P_content "android:id=" @+id/iv_result "android:background=" @drawable/ic_launcher "Android:layout_grav ity= "Center"/></linearlayout>
Mainactivity.java
Package Com.xbmu.scan;import Android.app.activity;import Android.content.intent;import android.graphics.Bitmap; Import Android.graphics.bitmapfactory;import Android.os.bundle;import Android.view.view;import Android.widget.checkbox;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.textview;import Android.widget.toast;import com.xys.libzxing.zxing.activity.CaptureActivity; Import Com.xys.libzxing.zxing.encoding.encodingutils;public class Mainactivity extends Activity {private TextView Tvre Sult; Private EditText Etinputtext; Private ImageView Ivresult; Private CheckBox Cbselect; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tvresult = (TextView) Findviewbyid (R.id.tv_result); Etinputtext = (EditText) Findviewbyid (R.id.et_input_text); Ivresult = (ImageView) Findviewbyid (R.id.iv_result); Cbselect = (CHECKBOx) Findviewbyid (r.id.cb_select); /** * Click the Sweep button to open the scan QR code * @param view */public void Startscan (view view) {//jump to sweep Starta Ctivityforresult (New Intent (Mainactivity.this, Captureactivity.class), 0); /** * generates QR code * @param view */public void Makeqrcode (view view) {String Inputtext = Etinputtext. GetText (). toString (); if (Inputtext.equals (")") {Toast.maketext (Mainactivity.this, "the text entered cannot be empty", Toast.length_short). Show (); }else{Bitmap Bitmap = Encodingutils.createqrcode (Inputtext, $, $, cbselect.ischecked () ? Bitmapfactory.decoderesource (Getresources (), R.drawable.xiaomao): null); Ivresult.setimagebitmap (bitmap); }} @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onacti Vityresult (Requestcode, ResultCode, data); if (resultcode==result_oK) {Bundle bundle = Data.getextras (); String result = bundle.getstring ("result"); Tvresult.settext (result); } }}Operating effect:
To this end, has been a smooth internship of the two-dimensional code scanning and generation. thanks to the web, the provision of teaching video, continuous learning, improve themselves.
Scanning and generation of Android QR code