当前位置 : 主页 > 手机开发 > android >

zxing二维码位矩阵转换成Bitmap位图的实战教程

来源:互联网 收集:自由互联 发布时间:2023-02-01
目录 关于zxing 关于zxing开源库中的位矩阵BitMatrix 位矩阵配置 位矩阵生成 关于位矩阵生成一位像素数组 总结 关于zxing ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库
目录
  • 关于zxing
  • 关于zxing开源库中的位矩阵BitMatrix
  • 位矩阵配置
  • 位矩阵生成
  • 关于位矩阵生成一位像素数组
  • 总结 

关于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倍

总结 

到此这篇关于zxing二维码位矩阵转换成Bitmap位图的文章就介绍到这了,更多相关zxing二维码位矩阵转Bitmap内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

上一篇:Android使用ViewStub实现布局优化方法示例
下一篇:没有了
网友评论