Android編程實現(xiàn)二維碼的生成與解析
更新時間:2015年11月28日 09:58:22 作者:lee0oo0
這篇文章主要介紹了Android編程實現(xiàn)二維碼的生成與解析方法,結合實例分析了Android二維碼的生成與讀取二維碼的相關技巧,并提供了二維碼jar包供讀者下載,需要的朋友可以參考下
本文實例講述了Android編程實現(xiàn)二維碼的生成與解析。分享給大家供大家參考,具體如下:
直接上代碼,代碼上面有具體的解析,并且提供jar供下載:二維碼Jar包.rar 。
根據(jù)文本生成對應的二維碼:
// 生成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(); } }
根據(jù)二維碼圖片讀取內容:
// 解析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程序設計有所幫助。
相關文章
Android?ViewPager你可能不知道的刷新操作分享
這篇文章主要為大家詳細介紹了Android中ViewPager你可能不知道的刷新操作,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下2023-05-05Android版微信跳一跳小游戲利用技術手段達到高分的操作方法
朋友圈到處都是曬微信跳一跳小游戲的,很多朋友能達到二三百分了。下面小編給大家分享Android版微信跳一跳小游戲利用技術手段達到高分的操作方法,需要的朋友一起看看吧2018-01-01flutter監(jiān)聽app進入前后臺狀態(tài)的實現(xiàn)
在開發(fā)app的過程中,我們經常需要知道app處于前后臺的狀態(tài),本文主要介紹了flutter監(jiān)聽app進入前后臺狀態(tài)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04