Android二維碼產生功能

來源:互聯網
上載者:User

標籤:

調用系統相簿掃描返回一個rusult,然後通過輸入文字實現二維碼產生的功能。

第一步下載一個zxing第三方包 

  然後初始化控制項

activity_main.xml

布局檔案

<?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>

第二步調用系統相機實現掃描二維碼並返回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>

第三步實現輸入文本點擊返回一個Bitmap對象,然後將ImageView設定到Bitmap裡面,然後二維碼顯示出來了。當然你還可以加背景圖片,總而言之實現起來還是比較方便!

最後實現效果如下


Android二維碼產生功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.