先说说我的需求是生成出来的二维码,定位图案是灰色的,数据吗和纠错码时模块时黑色的,其他是白色的。
原理其实很简单,利用zxing的写二维码功能生成二维码图片时,对相应像素点进行着色即可。关键是如何准确获取到二维码探测点在matrix里的位置。
二维码一共有40个尺寸。官方叫版本Version。Version 1是21 x 21的矩阵,Version 2是 25 x 25的矩阵,Version 3是29的尺寸,每增加一个version,就会增加4的尺寸,公式是:(V-1)*4 + 21(V是版本号) 最高Version 40,(40-1)*4+21 = 177,所以最高是177 x 177 的正方形。……而三个角的探测点的长度相对来讲是固定的。
上面这段话是对二维码的尺寸规格的说明。另外,探测点在任何尺寸的二维码中,单边长度都是8个模块(包含外围的一条白边)。根据这些,我们就可以计算出探测点在像素矩阵中的具体位置了。
利用zxing生成二维码,是先生成Matrix,再由BarcodeEncoder转成Bitmap。看BarcodeEncoder中代码:
public Bitmap createBitmap(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
其中重要的一句
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
根据矩阵中的0,1做颜色转换,由此我们可以自己来设定自己所需要的颜色。现在我们的任务就是确定探测点的准确位置。
确定生成的二维码探测点位置
要想确定生成的二维码探测点位置,首先要确定生成的二维码的尺寸。看zxing中的QRCodeWriter类,其中
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (contents.isEmpty()) {
throw new IllegalArgumentException("Found empty contents");
}
if (format != BarcodeFormat.QR_CODE) {
throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
}
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
height);
}
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
int quietZone = QUIET_ZONE_SIZE;
if (hints != null) {
ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
if (requestedECLevel != null) {
errorCorrectionLevel = requestedECLevel;
}
Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
if (quietZoneInt != null) {
quietZone = quietZoneInt;
}
}
QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
return renderResult(code, width, height, quietZone);
}
主要是调用Encoder中的encode生成一个QRCode,再由renderResult方法生成一个BitMatrix。而生成的QRCode中包含了我们所需要的Version信息。所以我们可以自己改写一个QRCodeWriter类,拿到QRCode。直接将QRCodeWriter复制出来,重写几个方法即可。如我写的ColorQRCodeWriter:
利用zxing生成二维码,是先生成Matrix,再由BarcodeEncoder转成Bitmap。看BarcodeEncoder中代码:
public Bitmap createBitmap(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
其中重要的一句
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
根据矩阵中的0,1做颜色转换,由此我们可以自己来设定自己所需要的颜色。现在我们的任务就是确定探测点的准确位置。
确定生成的二维码探测点位置
要想确定生成的二维码探测点位置,首先要确定生成的二维码的尺寸。看zxing中的QRCodeWriter类,其中
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (contents.isEmpty()) {
throw new IllegalArgumentException("Found empty contents");
}
if (format != BarcodeFormat.QR_CODE) {
throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
}
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
height);
}
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
int quietZone = QUIET_ZONE_SIZE;
if (hints != null) {
ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
if (requestedECLevel != null) {
errorCorrectionLevel = requestedECLevel;
}
Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
if (quietZoneInt != null) {
quietZone = quietZoneInt;
}
}
QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
return renderResult(code, width, height, quietZone);
}
主要是调用Encoder中的encode生成一个QRCode,再由renderResult方法生成一个BitMatrix。而生成的QRCode中包含了我们所需要的Version信息。所以我们可以自己改写一个QRCodeWriter类,拿到QRCode。直接将QRCodeWriter复制出来,重写几个方法即可。如我写的ColorQRCodeWriter:
其中的addLogo方法直接从网上找的。
下面附上源码地址:下载地址请点击