国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現二維碼的生成與解析

2020-04-11 11:14:50
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現二維碼的生成與解析。分享給大家供大家參考,具體如下:

直接上代碼,代碼上面有具體的解析,并且提供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();  }}

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹溪县| 嵩明县| 岫岩| 武冈市| 合作市| 六安市| 珲春市| 金平| 许昌市| 出国| 普陀区| 五指山市| 吉安县| 甘孜| 娄烦县| 左贡县| 镇江市| 安溪县| 大悟县| 平南县| 莲花县| 陆良县| 科技| 静安区| 永济市| 丰城市| 连平县| 牟定县| 麟游县| 玉田县| 靖州| 香格里拉县| 永川市| 电白县| 清水河县| 缙云县| 甘德县| 广南县| 固阳县| 宜昌市| 巴南区|