Android之二維碼的產生與解析

來源:互聯網
上載者:User

直接上代碼,代碼上面有具體的解析,並且提供jar供下載:二維碼Jar包.rar

根據文本產生對應的二維碼:

// 產生QR圖    private void createImage() {        try {            // 需要引入core包            QRCodeWriter writer = new QRCodeWriter();            String text = qr_text.getText().toString();            Log.i(TAG, "產生的文本:" + text);            if (text == null || "".equals(text) || text.length() < 1) {                return;            }            // 把輸入的文本轉為二維碼            BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,                    QR_WIDTH, QR_HEIGHT);            System.out.println("w:" + martix.getWidth() + "h:"                    + martix.getHeight());            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");            BitMatrix bitMatrix = new QRCodeWriter().encode(text,                    BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];            for (int y = 0; y < QR_HEIGHT; y++) {                for (int x = 0; x < QR_WIDTH; x++) {                    if (bitMatrix.get(x, y)) {                        pixels[y * QR_WIDTH + x] = 0xff000000;                    } else {                        pixels[y * QR_WIDTH + x] = 0xffffffff;                    }                }            }            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,                    Bitmap.Config.ARGB_8888);            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);            qr_image.setImageBitmap(bitmap);        } catch (WriterException e) {            e.printStackTrace();        }    }


根據二維碼圖片讀取內容:

// 解析QR圖片    private void scanningImage() {        Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>();        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");        // 獲得待解析的圖片        Bitmap bitmap = ((BitmapDrawable) qr_image.getDrawable()).getBitmap();        RGBLuminanceSource source = new RGBLuminanceSource(bitmap);        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));        QRCodeReader reader = new QRCodeReader();        Result result;        try {            result = reader.decode(bitmap1, hints);            // 得到解析後的文字            qr_result.setText(result.getText());        } catch (NotFoundException e) {            e.printStackTrace();        } catch (ChecksumException e) {            e.printStackTrace();        } catch (FormatException e) {            e.printStackTrace();        }    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.