zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的實(shí)戰(zhàn)教程
關(guān)于zxing
ZXing是一個(gè)開放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫,它包含了聯(lián)系到其他語言的端口。Zxing可以實(shí)現(xiàn)使用手機(jī)的內(nèi)置的攝像頭完成條形碼的掃描及解碼。
該項(xiàng)目可實(shí)現(xiàn)的條形碼編碼和解碼。我們支持以下格式:
UPC-A,UPC-E
EAN-8,EAN-13
39碼
93碼
代碼128
創(chuàng)新及科技基金
庫德巴
RSS-14(所有的變體)
RSS擴(kuò)展(大多數(shù)變體)
QR碼
數(shù)據(jù)矩陣
阿茲臺(tái)克人('測(cè)試版'質(zhì)量)
PDF 417('阿爾法'的質(zhì)量)
Zxing庫的主要部分支持以下幾個(gè)功能:核心代碼的使用、適用于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ù)中,以及整個(gè)通用模塊中,x 是列位置,y 是行位置。 排序始終為 x, y。 原點(diǎn)在左上角。
在內(nèi)部,這些位以 32 位整數(shù)的一維數(shù)組表示。 但是,每一行都以一個(gè)新的 int 開頭。 這是有意完成的,以便我們可以非常有效地將一行復(fù)制到 BitArray 中。
位的順序是行優(yōu)先的。 在每個(gè) int 中,首先使用最低有效位,這意味著它們代表較低的 x 值。 這與 BitArray 的實(shí)現(xiàn)兼容。
位矩陣配置
/// 1.設(shè)置二維碼相關(guān)配置 /// Hashtable<EncodeHintType, String> hints = new Hashtable<>(); // 字符轉(zhuǎn)碼格式設(shè)置 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 容錯(cuò)率設(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)為一維像素?cái)?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_
//通過像素?cái)?shù)組生成bitmap
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);matrix.get(i, j)是位矩陣中判斷該點(diǎn)是否黑點(diǎn)
x - 水平分量(即哪一列)
y - 垂直分量(即哪一行)
關(guān)于位矩陣生成一位像素?cái)?shù)組
按行遍歷
int width = matrix.getWidth();
int height = matrix.getHeight();
//二維矩陣轉(zhuǎn)為一維像素?cái)?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)為一維像素?cái)?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;
}
}
}實(shí)現(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");
// 容錯(cuò)率設(shè)置
hints.put(EncodeHintType.ERROR_CORRECTION, "L");
// 空白邊距設(shè)置
hints.put(EncodeHintType.MARGIN, "0");
//生成二維矩陣,編碼時(shí)指定大小,不要生成了圖片以后再進(jìn)行縮放,這樣會(huì)模糊導(dǎo)致識(shí)別失敗
BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, w,
int width = matrix.getWidth();
int height = matrix.getHeight();
//二維矩陣轉(zhuǎn)為一維像素?cái)?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);
//通過像素?cái)?shù)組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}提示:Android在位矩陣生成一維像素?cái)?shù)組時(shí),顏色值直接用16進(jìn)制的顏色值0xFF000000,不要用getResources().getColor(R.color.black)這種方法,否則轉(zhuǎn)化時(shí)間將會(huì)多消耗5-6倍
總結(jié)
到此這篇關(guān)于zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的文章就介紹到這了,更多相關(guān)zxing二維碼位矩陣轉(zhuǎn)Bitmap內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Theme以及解決啟動(dòng)黑屏的方法詳解
這篇文章主要給大家介紹了關(guān)于Android Theme以及解決啟動(dòng)黑屏的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Android設(shè)備adb連接后顯示device unauthorized解決方案
這篇文章主要為大家介紹了Android設(shè)備adb連接后顯示device unauthorized解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
詳解Android Scroller與computeScroll的調(diào)用機(jī)制關(guān)系
這篇文章主要介紹了詳解Android Scroller與computeScroll的調(diào)用機(jī)制關(guān)系的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android 中圖片和按鈕按下狀態(tài)變化實(shí)例代碼解析
這篇文章通過實(shí)例代碼給大家總結(jié)了android 中圖片和按鈕按下狀態(tài)變化問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-06-06
Android Handler移除Message詳解及實(shí)例代碼
這篇文章主要介紹了Android Handler移除Message詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
使用PlatformView將?Android?控件view制作成Flutter插件
這篇文章主要為大家介紹了使用PlatformView將?Android?控件view制作成Flutter插件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

