Android中怎么利用zxing生成二維碼-創(chuàng)新互聯(lián)

這篇文章主要介紹了Android中怎么利用zxing生成二維碼的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Android中怎么利用zxing生成二維碼文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

發(fā)展壯大離不開廣大客戶長(zhǎng)期以來(lái)的信賴與支持,我們將始終秉承“誠(chéng)信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠(chéng)服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及純水機(jī)等,在網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷推廣、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。

二維碼生成原理(即工作原理)

二維碼官方叫版本Version。Version 1是21 x 21的矩陣,Version 2是 25 x 25的矩陣,Version 3是29的尺寸,每增加一個(gè)version,就會(huì)增加4的尺寸,公式是:(V-1)*4 + 21(V是版本號(hào)) 高Version 40,(40-1)*4+21 = 177,所以高是177 x 177 的正方形。

下面是一個(gè)二維碼的樣例:

Android中怎么利用zxing生成二維碼

效果圖如下:

Android中怎么利用zxing生成二維碼

前提:

導(dǎo)入 zxing 的 jar 后開始操作,老規(guī)矩最后有源碼,作者布局默認(rèn)相對(duì)布局。

第一步:定義二維碼的長(zhǎng)寬高及圖片控件

Android中怎么利用zxing生成二維碼

第二步:實(shí)例化 QRCodeWriter 后利用 for 循環(huán)將二維碼畫出來(lái),然后用圖片控件加載圖片。

Android中怎么利用zxing生成二維碼

源碼如下:

布局文件:**

  <Button
        android:id="@+id/mybutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:text="點(diǎn)擊顯示二維碼"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="192dp"
        android:src="@drawable/ic_launcher_background" />

    <EditText
        android:id="@+id/myeditText"
        android:layout_width="300dp"
        android:maxLines="1"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mybutton"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="請(qǐng)輸入要加載成二維碼的內(nèi)容" />

java 文件:

public class MainActivity extends Activity implements View.OnClickListener {


    private int width = 300;
    private int height = 300;
    private ImageView imageView;
    private Bitmap bit;
    private Button mybutton;
    private EditText myeditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }


    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        mybutton = (Button) findViewById(R.id.mybutton);
        mybutton.setOnClickListener(this);
        myeditText = (EditText) findViewById(R.id.myeditText);
        myeditText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mybutton:
          String name=myeditText.getText().toString();
          if(name.equals("")){
              myeditText.setError("請(qǐng)輸入內(nèi)容");
          }else{
              zxing(name);
          }

                break;
        }
    }
    private void zxing(String name){
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //記得要自定義長(zhǎng)寬
        BitMatrix encode = null;
        try {
            encode = qrCodeWriter.encode(name, BarcodeFormat.QR_CODE, width, height, hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int[] colors = new int[width * height];
           //利用for循環(huán)將要表示的信息寫出來(lái)
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (encode.get(i, j)) {
                    colors[i * width + j] = Color.BLACK;
                } else {
                    colors[i * width + j] = Color.WHITE;
                }
            }
        }

        bit = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
        imageView.setImageBitmap(bit);
    }

}

關(guān)于“Android中怎么利用zxing生成二維碼”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Android中怎么利用zxing生成二維碼”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱欄目:Android中怎么利用zxing生成二維碼-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://muchs.cn/article20/ejdjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、定制開發(fā)、做網(wǎng)站、網(wǎng)站維護(hù)、電子商務(wù)移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)