網站首頁 編程語言 正文
關于zxing
ZXing是一個開放源碼的,用Java實現的多種格式的1D/2D條碼圖像處理庫,它包含了聯系到其他語言的端口。Zxing可以實現使用手機的內置的攝像頭完成條形碼的掃描及解碼。
該項目可實現的條形碼編碼和解碼。我們支持以下格式:
UPC-A,UPC-E
EAN-8,EAN-13
39碼
93碼
代碼128
創新及科技基金
庫德巴
RSS-14(所有的變體)
RSS擴展(大多數變體)
QR碼
數據矩陣
阿茲臺克人('測試版'質量)
PDF 417('阿爾法'的質量)
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.
?表示二維位矩陣。 在下面的函數參數中,以及整個通用模塊中,x 是列位置,y 是行位置。 排序始終為 x, y。 原點在左上角。
在內部,這些位以 32 位整數的一維數組表示。 但是,每一行都以一個新的 int 開頭。 這是有意完成的,以便我們可以非常有效地將一行復制到 BitArray 中。
位的順序是行優先的。 在每個 int 中,首先使用最低有效位,這意味著它們代表較低的 x 值。 這與 BitArray 的實現兼容。
位矩陣配置
/// 1.設置二維碼相關配置 ///
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
// 字符轉碼格式設置
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);
位矩陣轉換成位圖
int width = matrix.getWidth();
int height = matrix.getHeight();
//二維矩陣轉為一維像素數組
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_
//通過像素數組生成bitmap
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
matrix.get(i, j)是位矩陣中判斷該點是否黑點
x - 水平分量(即哪一列)
y - 垂直分量(即哪一行)
關于位矩陣生成一位像素數組
按行遍歷
int width = matrix.getWidth();
int height = matrix.getHeight();
//二維矩陣轉為一維像素數組
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();
//二維矩陣轉為一維像素數組
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;
}
}
}
?實現
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<>();
// 字符轉碼格式設置
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();
//二維矩陣轉為一維像素數組
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);
//通過像素數組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
提示:Android在位矩陣生成一維像素數組時,顏色值直接用16進制的顏色值0xFF000000,不要用getResources().getColor(R.color.black)這種方法,否則轉化時間將會多消耗5-6倍
總結?
原文鏈接:https://blog.csdn.net/mozushixin_1/article/details/126719523
相關推薦
- 2022-12-12 Android?Google?AutoService框架使用詳解_Android
- 2022-05-17 ubuntu No package ‘libzmq‘ found
- 2022-06-14 C#通過cmd調用7z軟件實現壓縮和解壓文件_C#教程
- 2022-08-02 深入了解Golang的map增量擴容_Golang
- 2022-06-11 C#把EXCEL數據轉換成DataTable_C#教程
- 2021-12-13 基于C#實現端口掃描器(單線程和多線程)_C#教程
- 2023-01-18 RabbitMq如何做到消息的可靠性投遞_Golang
- 2023-02-07 Pytorch中的廣播機制詳解(Broadcast)_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支