Android QR code generation function

Source: Internet
Author: User

Call the System album scan to return a rusult, and then through the input text to achieve the function of the two-dimensional code generation.

First step Download a zxing third party package

Then initialize the control

Activity_main.xml

Layout file

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:background= "@android: Color/white"
android:orientation= "Vertical" >


<button
Android:id= "@+id/btn_scan_barcode"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_margintop= "30DP"
android:text= "Open Camera"
android:onclick= "Scanner"/>

<linearlayout
android:orientation= "Horizontal"
android:layout_margintop= "10DP"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" >

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textcolor= "@android: Color/black"
Android:textsize= "18SP"
android:text= "Scan Result:"/>

<textview
Android:id= "@+id/tv_scan_result"
Android:layout_width= "Fill_parent"
Android:textsize= "18SP"
Android:textcolor= "@android: Color/black"
android:layout_height= "Wrap_content"/>
</LinearLayout>

<edittext
Android:id= "@+id/et_qr_string"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_margintop= "30DP"
Android:hint= "Input the text"/>

<button
Android:id= "@+id/btn_add_qrcode"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Generate QRCode"
android:onclick= "Make"/>

<imageview
Android:id= "@+id/iv_qr_image"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_margintop= "20DP"
android:layout_gravity= "Center"
android:background= "@drawable/weixin_logo"/>


</LinearLayout>

The second step calls the system camera implementation to scan the QR code and return reuslt


Camera.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >


<surfaceview
Android:id= "@+id/preview_view"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"/>


<com.zxing.view.viewfinderview
Android:id= "@+id/viewfinder_view"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>


<relativelayout
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_gravity= "Center"
android:orientation= "Vertical" >


<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparenttop= "true"
Android:layout_centerinparent= "true"
android:background= "@drawable/navbar"
android:gravity= "Center"
Android:paddingbottom= "10DP"
android:paddingtop= "10DP"
android:text= "Scan Barcode"
Android:textcolor= "@android: Color/white"
Android:textsize= "18SP"
android:textstyle= "Bold"/>


<button
Android:id= "@+id/btn_cancel_scan"
Android:layout_width= "230DP"
android:layout_height= "40DP"
Android:layout_alignparentbottom= "true"
Android:layout_centerinparent= "true"
Android:layout_marginbottom= "75DP"
android:text= "Cancel"
Android:textsize= "15SP"
android:textstyle= "Bold"/>


<!--<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
android:orientation= "Vertical" >


<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_margin= "2DP"
android:gravity= "Center"
android:text= "@string/scan_prompt_info"
Android:textcolor= "@android: Color/white"
Android:textsize= "15SP"
android:textstyle= "Bold"/>
</LinearLayout>

</RelativeLayout>


</FrameLayout>

The third step implements the input text click return a Bitmap object, then set the ImageView to bitmap inside, then the QR code is displayed. Of course you can also add background pictures, all in all, it is more convenient to achieve!

The final result is as follows


Android QR code generation 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.