android 二維碼 掃描與產生(內建)

來源:互聯網
上載者:User

標籤:android

本文使用 zxing-android-embedded 這個開源項目實現 二維碼掃描/產生 功能;

開發工具:android studio

1、如何將zxing-android-embedded添加到我們的項目中

    1.1  添加arr依賴包

    將以下代碼添加到build.gradle檔案中。

           

repositories {    mavenCentral()    maven {        url "https://dl.bintray.com/journeyapps/maven"    }}dependencies {    // Supports Android 4.0.3 and later (API level 15)    compile ‘com.journeyapps:zxing-android-embedded:[email protected]‘    // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.    // If you only plan on supporting Android 4.0.3 and up, you don‘t need to include this.    compile ‘com.journeyapps:zxing-android-legacy:[email protected]‘    // Convenience library to launch the scanning Activities.    // It automatically picks the best scanning library from the above two, depending on the    // Android version and what is available.    compile ‘com.journeyapps:zxing-android-integration:[email protected]‘    // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.    // This mostly affects encoding, but you should test if you plan to support these versions.    // Older versions e.g. 2.2 may also work if you need support for older Android versions.    compile ‘com.google.zxing:core:3.2.0‘    }


    注意:是app目錄下的build.gradle檔案

    650) this.width=650;" alt="android 二維碼 掃描與產生(內建) - jiatao_zhou - jiatao_zhou的部落格" src="http://img0.ph.126.net/8oAu1tFEKpbavsDMu4wbxw==/6630629060560175226.png" style="border:0px;height:auto;margin:0px 10px 0px 0px;width:300px;" />


    1.2  通過gradle同步你的項目

        點擊  "sync project with gradle files",android studio 將連網下載必要的檔案。

        650) this.width=650;" alt="android 二維碼 掃描與產生(內建) - jiatao_zhou - jiatao_zhou的部落格" src="http://img1.ph.126.net/Y1h3kWh5QPxsLZl_h2_KYw==/6630523507443606550.png" style="border:0px;height:auto;margin:0px 10px 0px 0px;width:500px;" />

        到這一步為止,我們的項目已經可以使用 zxing-android-embedded 的代碼了!






2、掃描二維碼

    2.1  啟動二維碼掃描介面

            在Activity中,使用預設選項啟動,可使用以下代碼:

new IntentIntegrator(this).initiateScan(); //‘this‘是當前的Activity

      在Fragment中,使用預設選項啟動,可使用以下代碼:

IntentIntegrator.forSupportFragment(this).initiateScan(); //‘this‘是當前的Fragment

            2.1.1 自訂選項,參考 IntentIntegrator

   2.2  接收掃描結果

    掃描完成,將會調用你的 onActivityResult 方法。

    因此我們需要重寫 onActivityResult 方法來擷取掃描結果。

@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent intent) {super.onActivityResult(requestCode, resultCode, intent);    if (resultCode == Activity.RESULT_OK) {        String contents = intent.getStringExtra("SCAN_RESULT");//映像內容        String format = intent.getStringExtra("SCAN_RESULT_FORMAT");    //映像格式        // Handle successful scan        Log.v("qr-code", contents + "####" + format);        tv_content.setText(contents);    } else if (resultCode == Activity.RESULT_CANCELED) {        // Handle cancel    }}

    到此步,完成了二維碼掃描功能。

    如果掃描這張以下二維碼圖片,你將在logcat中得到這條輸出語句 “www.google.com####QR_CODE”

650) this.width=650;" alt="android 二維碼 掃描與產生(內建) - jiatao_zhou - jiatao_zhou的部落格" src="http://img0.ph.126.net/1_TL1wB3prFjBHYP04dtJg==/6630664244932264602.png" style="border:0px;height:auto;margin:0px 10px 0px 0px;width:300px;" />





3 、產生二維碼

如果你需要將一段文本轉換成二維碼Bitmap,可參考以下代碼

private Bitmap generateQRCode(String qrCodeString){    Bitmap bmp = null;    //二維碼圖片    QRCodeWriter writer = new QRCodeWriter();    try {        BitMatrix bitMatrix = writer.encode(qrCodeString, BarcodeFormat.QR_CODE, 512, 512); //參數分別表示為: 條碼常值內容,條碼格式,寬,高        int width = bitMatrix.getWidth();        int height = bitMatrix.getHeight();        bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);                //繪製每個像素        for (int x = 0; x < width; x++) {            for (int y = 0; y < height; y++) {                bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);            }        }    } catch (WriterException e) {        e.printStackTrace();    }    return bmp;}


本文出自 “happy coding...” 部落格,請務必保留此出處http://jiataozhou.blog.51cto.com/7355162/1651712

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.