Android編程實(shí)現(xiàn)二維碼的生成與解析
本文實(shí)例講述了Android編程實(shí)現(xiàn)二維碼的生成與解析。分享給大家供大家參考,具體如下:
直接上代碼,代碼上面有具體的解析,并且提供jar供下載:二維碼Jar包.rar 。
根據(jù)文本生成對(duì)應(yīng)的二維碼:
// 生成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;
}
// 把輸入的文本轉(zhuǎn)為二維碼
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ù)二維碼圖片讀取內(nèi)容:
// 解析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();
}
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
- Android 超簡(jiǎn)易Zxing框架 生成二維碼+掃碼功能
- Android 二維碼掃描和生成二維碼功能
- Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能
- Android中使用ZXing生成二維碼(支持添加Logo圖案)
- Android 二維碼 生成和識(shí)別二維碼 附源碼下載
- Android基于google Zxing實(shí)現(xiàn)二維碼的生成
- Android生成條形碼和二維碼功能
- Android 點(diǎn)擊生成二維碼功能實(shí)現(xiàn)代碼
- Android中二維碼的掃描和生成(使用zxing庫(kù))
相關(guān)文章
Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android CoordinatorLayout高級(jí)用法之自定義Behavior
這篇文章主要介紹了Android CoordinatorLayout高級(jí)用法之自定義Behavior,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Android 斷點(diǎn)下載和自動(dòng)安裝的示例代碼
本篇文章主要介紹了Android斷點(diǎn)下載和自動(dòng)安裝的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁(yè)源碼查看器實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁(yè)源碼查看器,結(jié)合實(shí)例形式分析了Android針對(duì)網(wǎng)絡(luò)圖片及網(wǎng)頁(yè)的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01
Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法
這篇文章主要介紹了Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法,本文講解了RGB值轉(zhuǎn)Bitmap、Color值轉(zhuǎn)Bitmap、字節(jié)數(shù)組轉(zhuǎn)Bitmap、讀取文件轉(zhuǎn)Bitmap、讀取資源轉(zhuǎn)Bitmap、輸入流轉(zhuǎn)Bitmap等內(nèi)容,需要的朋友可以參考下2015-01-01
Android基于OkHttpUtils網(wǎng)絡(luò)請(qǐng)求的二次封裝
這篇文章主要介紹了Android基于OkHttpUtils網(wǎng)絡(luò)請(qǐng)求的二次封裝,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android自定義View仿支付寶芝麻信用分儀表盤(pán)
前幾天支付寶剛剛升級(jí)到v9.9,看了一眼里面的芝麻信用分,儀表盤(pán)挺好看的,所以想著來(lái)寫(xiě)一個(gè)這個(gè)版本的儀表盤(pán),不說(shuō)完全一模一樣,只是為了猜測(cè)支付寶在做這個(gè)的時(shí)候是如何設(shè)計(jì)的,在此記錄一下,有需要的可以參考借鑒。2016-09-09

