zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的實戰(zhàn)教程
關于zxing
ZXing是一個開放源碼的,用Java實現(xiàn)的多種格式的1D/2D條碼圖像處理庫,它包含了聯(lián)系到其他語言的端口。Zxing可以實現(xiàn)使用手機的內(nèi)置的攝像頭完成條形碼的掃描及解碼。
該項目可實現(xiàn)的條形碼編碼和解碼。我們支持以下格式:
UPC-A,UPC-E
EAN-8,EAN-13
39碼
93碼
代碼128
創(chuàng)新及科技基金
庫德巴
RSS-14(所有的變體)
RSS擴展(大多數(shù)變體)
QR碼
數(shù)據(jù)矩陣
阿茲臺克人('測試版'質(zhì)量)
PDF 417('阿爾法'的質(zhì)量)
Zxing庫的主要部分支持以下幾個功能:核心代碼的使用、適用于J2SE客戶端的版本、適用于Android客戶端的版本(即BarcodeScanner)、Android的集成(通過Intent支持和BarcodeScanner的集成)等。
關于zxing開源庫中的位矩陣BitMatrix
Represents a 2D matrix of bits. In function arguments below, and throughout the common module, x is the column position, and y is the row position. The ordering is always x, y. The origin is at the top-left.
Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins with a new int. This is done intentionally so that we can copy out a row into a BitArray very efficiently.
The ordering of bits is row-major. Within each int, the least significant bits are used first, meaning they represent lower x values. This is compatible with BitArray's implementation.
表示二維位矩陣。 在下面的函數(shù)參數(shù)中,以及整個通用模塊中,x 是列位置,y 是行位置。 排序始終為 x, y。 原點在左上角。
在內(nèi)部,這些位以 32 位整數(shù)的一維數(shù)組表示。 但是,每一行都以一個新的 int 開頭。 這是有意完成的,以便我們可以非常有效地將一行復制到 BitArray 中。
位的順序是行優(yōu)先的。 在每個 int 中,首先使用最低有效位,這意味著它們代表較低的 x 值。 這與 BitArray 的實現(xiàn)兼容。
位矩陣配置
/// 1.設置二維碼相關配置 /// Hashtable<EncodeHintType, String> hints = new Hashtable<>(); // 字符轉(zhuǎn)碼格式設置 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 容錯率設置 hints.put(EncodeHintType.ERROR_CORRECTION, "L"); // 空白邊距設置 hints.put(EncodeHintType.MARGIN, "0");
位矩陣生成
BitMatrix matrix = new MultiFormatWriter().encode("二維碼中的字符串信息", BarcodeFormat.QR_CODE, w, h, hints);
位矩陣轉(zhuǎn)換成位圖
int width = matrix.getWidth(); int height = matrix.getHeight(); //二維矩陣轉(zhuǎn)為一維像素數(shù)組 int[] pixels = new int[width * height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { if (matrix.get(i, j)) { pixels[j * width + i] = Color.BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_ //通過像素數(shù)組生成bitmap bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
matrix.get(i, j)是位矩陣中判斷該點是否黑點
x - 水平分量(即哪一列)
y - 垂直分量(即哪一行)
關于位矩陣生成一位像素數(shù)組
按行遍歷
int width = matrix.getWidth(); int height = matrix.getHeight(); //二維矩陣轉(zhuǎn)為一維像素數(shù)組 int[] pixels = new int[width * height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { if (matrix.get(i, j)) { pixels[j * width + i] = Color.BLACK; } } }
按列遍歷
int width = matrix.getWidth(); int height = matrix.getHeight(); //二維矩陣轉(zhuǎn)為一維像素數(shù)組 int[] pixels = new int[width * height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (matrix.get(i, j)) { pixels[j * width + i] = Color.BLACK; } } }
實現(xiàn)
public Bitmap Create2DCode(String str, int w, int h){ if (TextUtils.isEmpty(str)) { return null; } if (w < 0 || h < 0) { return null; } /// 1.設置二維碼相關配置 /// Hashtable<EncodeHintType, String> hints = new Hashtable<>(); // 字符轉(zhuǎn)碼格式設置 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 容錯率設置 hints.put(EncodeHintType.ERROR_CORRECTION, "L"); // 空白邊距設置 hints.put(EncodeHintType.MARGIN, "0"); //生成二維矩陣,編碼時指定大小,不要生成了圖片以后再進行縮放,這樣會模糊導致識別失敗 BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, w, int width = matrix.getWidth(); int height = matrix.getHeight(); //二維矩陣轉(zhuǎn)為一維像素數(shù)組 int[] pixels = new int[width * height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { if (matrix.get(i, j)) { pixels[j * width + i] = Color.BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //通過像素數(shù)組生成bitmap,具體參考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
提示:Android在位矩陣生成一維像素數(shù)組時,顏色值直接用16進制的顏色值0xFF000000,不要用getResources().getColor(R.color.black)這種方法,否則轉(zhuǎn)化時間將會多消耗5-6倍
總結(jié)
到此這篇關于zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的文章就介紹到這了,更多相關zxing二維碼位矩陣轉(zhuǎn)Bitmap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android設備adb連接后顯示device unauthorized解決方案
這篇文章主要為大家介紹了Android設備adb連接后顯示device unauthorized解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06詳解Android Scroller與computeScroll的調(diào)用機制關系
這篇文章主要介紹了詳解Android Scroller與computeScroll的調(diào)用機制關系的相關資料,需要的朋友可以參考下2016-01-01Android 中圖片和按鈕按下狀態(tài)變化實例代碼解析
這篇文章通過實例代碼給大家總結(jié)了android 中圖片和按鈕按下狀態(tài)變化問題,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨腳本之家小編一起學習吧2018-06-06Android Handler移除Message詳解及實例代碼
這篇文章主要介紹了Android Handler移除Message詳解及實例代碼的相關資料,需要的朋友可以參考下2017-02-02使用PlatformView將?Android?控件view制作成Flutter插件
這篇文章主要為大家介紹了使用PlatformView將?Android?控件view制作成Flutter插件實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11