Android中如何改变二维码的探测点的颜色

公司设计对二维码的样式有这个需求,以前也没修改过二维码的样式,刚开始在网上找了很久都没有这方面的资料,后来就想在ZXing源码里去修改,在网上搜索ZXing源码时找到了这篇文章。http://blog.csdn.net/mrwangxsyz/article/details/52447323 这个时网址。

 

先说说我的需求是生成出来的二维码,定位图案是灰色的,数据吗和纠错码时模块时黑色的,其他是白色的。


原理其实很简单,利用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方法直接从网上找的。

下面附上源码地址:下载地址请点击


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值