日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

zxing二維碼位矩陣轉換成Bitmap位圖的實戰教程_Android

作者:不會寫代碼的猴子 ? 更新時間: 2022-11-01 編程語言

關于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

欄目分類
最近更新