QR code creation and scanning, QR code creation and Scanning

Source: Internet
Author: User

QR code creation and scanning, QR code creation and Scanning

Yesterday, we introduced a QR code creation policy. During the test, we found that the generated QR code is a communication QR code. The QR code was successfully scanned by the UC browser. It was scanned by the QR code that came with Xiaomi mobile phone, you can correctly parse the information in the QR code and import it to the address book with one click. It is not clear why. It is very convenient for you to create a QR code E-business card. In this article, I will introduce how to create a QR code, scan the QR code, and parse it.

ZXing is an open-source Java class library used to parse 1D/2D bar codes in multiple formats. The goal is to decode the 1D barcode of QR code, Data Matrix, and UPC. It provides clients on multiple platforms, including J2SE, J2SE, and Android. Before development need to download to the official website need to use jar, the official website for our jar is very cumbersome, we are generally using a simplified version, for this jar you can refer to me for everyone demo: http://pan.baidu.com/s/1hqitkH2

Next I will introduce the code. We only need to modify the MainActivity file:

Public class MainActivity extends Activity {private final static int SCANNIN_GREQUEST_CODE = 1;/*** display scan result */private TextView mTextView; /*** display scanned images */private ImageView mImageView; private Button mButton; // ----------- generate the QR code ----------------- private Button creatButton; private EditText creatText; private ImageView creatView; @ Override protected void onCreate (Bundle savedInstanceState) {s Uper. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTextView = (TextView) findViewById (R. id. result); mImageView = (ImageView) findViewById (R. id. qrcode_bitmap); // click the Button to jump to the QR code scanning interface. startActivityForResult is used to jump to the page. // After scanning, adjust the page to mButton = (Button) findViewById (R. id. button1); mButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent intent = n Ew Intent (); intent. setClass (MainActivity. this, MipcaActivityCapture. class); intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); startActivityForResult (intent, SCANNIN_GREQUEST_CODE) ;}}); // ------------ create a QR code ----------- creatButton = (Button) findViewById (R. id. button2); creatText = (EditText) findViewById (R. id. editText); creatView = (ImageView) findViewById (R. id. qrcode_bitmap); creatButton. setOn ClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {if (creatText. getText ()! = Null) {try {// string to construct StringBuffer content = new StringBuffer (); content. append ("name:"); String str = creatText. getText (). toString (). trim (); content. append (str + ";"); content. append ("tel:"); content. append ("123456;"); str = content. toString (); Bitmap qrcode = EncodingHandler. createQRCode (str, 400); creatView. setImageBitmap (qrcode);} catch (WriterException e) {e. printStackTrace () ;}}}) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); switch (requestCode) {case SCANNIN_GREQUEST_CODE: if (resultCode = RESULT_ OK) {Bundle bundle = data. getExtras (); // display the scanned content String str = bundle. getString ("result"); // String Parsing String [] arr = str. split (";"); String name = ""; String tel = ""; for (int I = 0; I <arr. length; I ++) {String a = arr [I]; String [] keyValue =. split (":"); int keylength = keyValue [0]. length (); switch (keylength) {case 4: name = keyValue [1]; break; case 3: tel = keyValue [1]; break; default: break ;}} mTextView. setText ("name:" + name + "tel:" + tel); // display mImageView. setImageBitmap (Bitmap) data. getParcelableExtra ("bitmap");} break ;}}}

Our layout file activity_main.xml:

<RelativeLayout 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: background = "# ffe1e0de"> <EditText android: id = "@ + id/editText" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_alignParentTop = "true" android: hint = "Enter the content"/> <Button android: id = "@ + id/button2" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/editText" android: text = ""/> <Button android: id = "@ + id/button1" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/button2" android: text = "Scan QR code"/> <TextView android: id = "@ + id/result" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/button1" android: lines = "2" android: gravity = "center_horizontal" android: textColor = "@ android: color/black" android: textSize = "16sp"/> <ImageView android: id = "@ + id/qrcode_bitmap" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_alignParentLeft = "true" android: layout_below = "@ + id/result"/> </RelativeLayout>

We use external activities for QR code scanning, so we need to declare in the AndroidManifest. xml file:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.qr_codescan"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="16" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@android:style/Theme.NoTitleBar" >        <activity            android:name="com.example.qr_codescan.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".MipcaActivityCapture"            android:configChanges="orientation|keyboardHidden"            android:screenOrientation="portrait"            android:windowSoftInputMode="stateAlwaysHidden" >        </activity>    </application>    <uses-permission android:name="android.permission.VIBRATE" />    <uses-permission android:name="android.permission.CAMERA" />        <uses-feature android:name="android.hardware.camera" />    <uses-feature android:name="android.hardware.camera.autofocus" /></manifest>

When scanning the QR code here, you need to turn on the camera and take a picture, so we need to add this permission.

We recommend that you use the following jar packages: zxing. jar

The last two documents are provided for your reference:

  

  

Related Article

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.