最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android利用zxing生成二維碼的過程記錄

 更新時(shí)間:2021年07月02日 11:34:28   作者:計(jì)蒙不吃魚  
Android中二維碼生成的最常用庫就是zxing了,正好目前項(xiàng)目有了生成二維碼的需求,所以下面這篇文章主要給大家介紹了關(guān)于Android利用zxing生成二維碼的相關(guān)資料,需要的朋友可以參考下

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

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

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

效果圖如下:

前提:

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

第一步:定義二維碼的長寬高及圖片控件

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

源碼如下:

布局文件:**

  <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="請輸入要加載成二維碼的內(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("請輸入內(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"); //記得要自定義長寬
        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)將要表示的信息寫出來
        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);
    }

}


總結(jié)

到此這篇關(guān)于Android利用zxing生成二維碼的文章就介紹到這了,更多相關(guān)Android zxing生成二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

龙海市| 贡觉县| 西乌珠穆沁旗| 天气| 姜堰市| 通化县| 游戏| 夏邑县| 晋中市| 钦州市| 都昌县| 牡丹江市| 龙州县| 龙游县| 泾川县| 剑河县| 克东县| 遂平县| 白水县| 灵璧县| 都匀市| 白城市| 安吉县| 平阴县| 吴堡县| 扶风县| 安多县| 麦盖提县| 鄂伦春自治旗| 积石山| 玛沁县| 永平县| 吴旗县| 六安市| 扬中市| 五指山市| 太仓市| 台东市| 苍南县| 延吉市| 扎囊县|