日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android使用Zxing库生成PDF417扫描后多一个字符A

發布時間:2023/12/20 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android使用Zxing库生成PDF417扫描后多一个字符A 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于zxing庫,用的是兩天前發布的3.5.1版本。

在Android配置文件build.gradle(app)中直接implementation的zxing.core。

implementation 'com.google.zxing:core:3.5.1'

在網上找到個代碼,可以創建PDF417碼制的二維碼。

public static Bitmap createPdf417(String text, int size) {try {Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// hints.put(EncodeHintType.ERROR_CORRECTION,"2");hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new PDF417Writer().encode(text,BarcodeFormat.PDF_417, size, size, hints);// int[] pixels = new int[size * size];int bitWidth = bitMatrix.getWidth();int bitHeight = bitMatrix.getHeight();int[] pixels = new int[bitWidth * bitHeight];//遍歷bitmatrix,為像素矩陣按一行行(橫列)設置像素顏色。for (int y = 0; y < bitHeight; y++) {//遍歷一行一行像素for (int x = 0; x < bitWidth; x++) {if (bitMatrix.get(x, y)) {pixels[y * bitWidth + x] = 0xff000000;} else {pixels[y * bitWidth + x] = 0xffffffff;}}}Bitmap bitmap = Bitmap.createBitmap(bitWidth, bitHeight,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, bitWidth, 0, 0, bitWidth, bitHeight);return bitmap;} catch (WriterException e) {e.printStackTrace();return null;}//return null; }

使用:自己的類.createPdf417(String text, int size)
text為二維碼的內容,size為大小。

直接copy,然后顯示的碼也挺漂亮。但是一掃就不對勁了前面總是有個A

用了pdf417的辦法,壓縮,緊湊,不失真–都不行。

然后就翻墻上github上看了一下

發現是個老bug老bug,具體的原因就是在增加hints的時候加上了UTF-8的標準。但是zxing庫這個默認的標準是iso_8859_1。
而且追了下發現是先是iso然后又轉成了UTF-8,所以出現了BUG。但是如果不加utf-8的話,就直接是iso標準,就不會有前面是A的方法了。
不過如果要求是utf-8那還是蠻麻煩的。

更改后的代碼,其實就是關了hints的utf-8

public static Bitmap createPdf417(String text, int size) {try {Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();// hints.put(EncodeHintType.ERROR_CORRECTION,"2");hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new PDF417Writer().encode(text,BarcodeFormat.PDF_417, size, size, hints);// int[] pixels = new int[size * size];int bitWidth = bitMatrix.getWidth();int bitHeight = bitMatrix.getHeight();int[] pixels = new int[bitWidth * bitHeight];//遍歷bitmatrix,為像素矩陣按一行行(橫列)設置像素顏色。for (int y = 0; y < bitHeight; y++) {//遍歷一行一行像素for (int x = 0; x < bitWidth; x++) {if (bitMatrix.get(x, y)) {pixels[y * bitWidth + x] = 0xff000000;} else {pixels[y * bitWidth + x] = 0xffffffff;}}}Bitmap bitmap = Bitmap.createBitmap(bitWidth, bitHeight,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, bitWidth, 0, 0, bitWidth, bitHeight);return bitmap;} catch (WriterException e) {e.printStackTrace();return null;}//return null; }

追了好久的源碼,也沒有找到到底那里轉換錯了。時間緊迫,就先這樣改了。

總結

以上是生活随笔為你收集整理的Android使用Zxing库生成PDF417扫描后多一个字符A的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。