zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的實戰(zhàn)教程
關(guān)于zxing
ZXing是一個開放源碼的,用Java實現(xiàn)的多種格式的1D/2D條碼圖像處理庫,它包含了聯(lián)系到其他語言的端口。Zxing可以實現(xiàn)使用手機(jī)的內(nèi)置的攝像頭完成條形碼的掃描及解碼。
該項目可實現(xiàn)的條形碼編碼和解碼。我們支持以下格式:
UPC-A,UPC-E
EAN-8,EAN-13
39碼
93碼
代碼128
創(chuàng)新及科技基金
庫德巴
RSS-14(所有的變體)
RSS擴(kuò)展(大多數(shù)變體)
QR碼
數(shù)據(jù)矩陣
阿茲臺克人('測試版'質(zhì)量)
PDF 417('阿爾法'的質(zhì)量)
Zxing庫的主要部分支持以下幾個功能:核心代碼的使用、適用于J2SE客戶端的版本、適用于Android客戶端的版本(即BarcodeScanner)、Android的集成(通過Intent支持和BarcodeScanner的集成)等。
關(guān)于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 開頭。 這是有意完成的,以便我們可以非常有效地將一行復(fù)制到 BitArray 中。
位的順序是行優(yōu)先的。 在每個 int 中,首先使用最低有效位,這意味著它們代表較低的 x 值。 這與 BitArray 的實現(xiàn)兼容。
位矩陣配置
/// 1.設(shè)置二維碼相關(guān)配置 /// Hashtable<EncodeHintType, String> hints = new Hashtable<>(); // 字符轉(zhuǎn)碼格式設(shè)置 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 容錯率設(shè)置 hints.put(EncodeHintType.ERROR_CORRECTION, "L"); // 空白邊距設(shè)置 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 - 垂直分量(即哪一行)
關(guān)于位矩陣生成一位像素數(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.設(shè)置二維碼相關(guān)配置 ///
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
// 字符轉(zhuǎn)碼格式設(shè)置
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 容錯率設(shè)置
hints.put(EncodeHintType.ERROR_CORRECTION, "L");
// 空白邊距設(shè)置
hints.put(EncodeHintType.MARGIN, "0");
//生成二維矩陣,編碼時指定大小,不要生成了圖片以后再進(jìn)行縮放,這樣會模糊導(dǎo)致識別失敗
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進(jìn)制的顏色值0xFF000000,不要用getResources().getColor(R.color.black)這種方法,否則轉(zhuǎn)化時間將會多消耗5-6倍
總結(jié)
到此這篇關(guān)于zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的文章就介紹到這了,更多相關(guān)zxing二維碼位矩陣轉(zhuǎn)Bitmap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android利用二階貝塞爾曲線實現(xiàn)添加購物車動畫詳解
這篇文章主要給大家介紹了關(guān)于Android利用二階貝塞爾曲線實現(xiàn)添加購物車動畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Android編程將Activity背景設(shè)置為墻紙的簡單實現(xiàn)方法
這篇文章主要介紹了Android編程將Activity背景設(shè)置為墻紙的簡單實現(xiàn)方法,涉及Android簡單的屬性設(shè)置及XML配置修改等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android 第三方應(yīng)用接入微信平臺研究情況分享(一)
微信平臺開放后倒是挺火的,許多第三方應(yīng)用都想試下接入微信這個平臺,畢竟可以利用微信建立起來的關(guān)系鏈來拓展自己的應(yīng)用還是挺不錯的 最近由于實習(xí)需要也在研究這個東西,這里把我的整個研究情況給出來2013-01-01
Android中利用ViewHolder優(yōu)化自定義Adapter的寫法(必看)
下面小編就為大家?guī)硪黄狝ndroid中利用ViewHolder優(yōu)化自定義Adapter的寫法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
listview與SQLite結(jié)合實現(xiàn)記事本功能
這篇文章主要為大家詳細(xì)介紹了listview與SQLite結(jié)合實現(xiàn)記事本功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

