Android 二维码开发功能实现(四)------基于Zxing实现编码功能(生成二维码,一维码等)

版权声明:本文为博主原创文章,转载必须注明出处!! https://blog.csdn.net/qq_34902522/article/details/83582507

Android 二维码开发功能实现(四)------基于Zxing实现编码功能(生成二维码,一维码等)

前言

关于Google的开源库Zxing,前面也写了几遍文章进行介绍.我们先简单的回顾一下!

  1. Android 二维码的扫码功能实现(一)
    这篇文章主要介绍了,Zxing是什么?怎么上手?适合没有接触过条码相关开发,0经验的朋友.
  2. Android 基于Zxing的扫码功能实现(二)
    这篇文章则介绍了,如何通过zxing来实现扫码功能,并且对zxing的源码进行分析,介绍了扫码的原理.还分享了一个基于Zxing开源的YZxing库(YZxing ).方便大家学习,掌握.
  3. Android 基于Zxing扫码实现(三)、从相册选取二维码
    这篇文章主要是对扫码逻辑的一个优化处理,来提高扫码的准确度和速度.以及介绍如何实现从相册中选择二维码来扫码的功能.

我们可以看到,之前的文章讲的都是利用zxing来解码(扫码)的,所以这次我们说说编码(生成条形码)的功能实现. 我们先看一下实现的效果图!


效果图

效果图


核心代码

private void encode(String content) {
        Log.e(TAG, "encode: content = " + content);
        if (content == null) {
            return;
        }
        Map<EncodeHintType, Object> hints = null;
        String encoding = guessAppropriateEncoding(content);
        if (encoding != null) {
            hints = new EnumMap<>(EncodeHintType.class);
            hints.put(EncodeHintType.CHARACTER_SET, encoding);
        }
        BitMatrix result;
        try {
            result = new MultiFormatWriter().encode(content, getWantedCodeType(mType)
                    , barcodeImageWidth, barcodeImageHeight, hints);
            int width = result.getWidth();
            int height = result.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] = result.get(x, y) ? BLACK : WHITE;
                }
            }

            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            mHandler.post(mUpdateImageRunnable);
        } catch (Exception iae) {
            // Unsupported format
            Log.e(TAG, "encode: " + iae.getMessage());
        }
    }

实现编码主要涉及到以下几个类.

  1. MultiFormatWriter(这是一个工厂类,根据传入的BarcodeFormat来找到最合适的一种**Writer类来编码)
  2. QRCodeWriter (渲染二维码)
  3. Encoder(编码核心类)
  4. BitMatrix(相当于编码后对数据进行渲染的一个容器,约束)
  5. MatrixUtil(渲染矩阵的工具类)

如果对编码的底层逻辑有很深的需求的话,需要根据源码,我上面所提到的这几个类,并且结合相关资料,可以自行了解.因为现实中开发任务,不涉及到很深的编码算法分析,本文是对编码流程的一个简单分析.

先看上面代码中的:

String encoding = guessAppropriateEncoding(content);
        if (encoding != null) {
            hints = new EnumMap<>(EncodeHintType.class);
            hints.put(EncodeHintType.CHARACTER_SET, encoding);
        }

这里对编码的内容进行判断,来选择合适的字符格式.这个格式参数在哪里用到了呢?在Encoder中的encode方法中,有它的身影.

static final String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

public static QRCode encode(String content,
                              ErrorCorrectionLevel ecLevel,
                              Map<EncodeHintType,?> hints) throws WriterException {

    // Determine what character encoding has been specified by the caller, if any
    String encoding = DEFAULT_BYTE_MODE_ENCODING;
    if (hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET)) {
      encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
    }

    // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
    // multiple modes / segments even if that were more efficient. Twould be nice.
    Mode mode = chooseMode(content, encoding);
    ...
    省略...
    ...
    }

我们可以看到,encoding在之前如果没有传入值的话,这边会默认其值为"ISO-8859-1" ,并且,通过encoding和传入的要编码的内容来选择,合适的Mode类型.选择mode的方法是**chooseMode(String content, String encoding)**官方对该方法的解释是寻找一个最合适的examining编码内容的mode.选择的逻辑还是比较简单的,这里看下就好.

private static Mode chooseMode(String content, String encoding) {
    if ("Shift_JIS".equals(encoding) && isOnlyDoubleByteKanji(content)) {
      // Choose Kanji mode if all input are double-byte characters
      return Mode.KANJI;
    }
    boolean hasNumeric = false;
    boolean hasAlphanumeric = false;
    for (int i = 0; i < content.length(); ++i) {
      char c = content.charAt(i);
      if (c >= '0' && c <= '9') {
        hasNumeric = true;
      } else if (getAlphanumericCode(c) != -1) {
        hasAlphanumeric = true;
      } else {
        return Mode.BYTE;
      }
    }
    if (hasAlphanumeric) {
      return Mode.ALPHANUMERIC;
    }
    if (hasNumeric) {
      return Mode.NUMERIC;
    }
    return Mode.BYTE;
  }

接着看这行代码:


       
        BitMatrix result = new MultiFormatWriter().encode(content, getWantedCodeType(mType)
                    , barcodeImageWidth, barcodeImageHeight, hints);

这行代码是实现编码功能的核心代码,

encode方法接受几个参数:

  1. content -编码内容
  2. format -编码格式
  3. width -条码图像宽度
  4. height -条码图像高度
  5. Map<EncodeHintType, Object> -参数集 ,可选可不选,可以通过该参数集设置CHARACTER_SET参数,ERROR_CORRECTION参数等…

我们进入MultiFormatWriter的encode方法,发现这个方法会根据format的类型,继续调用对应format的子集Writer的encode方法.

@Override
  public BitMatrix encode(String contents,
                          BarcodeFormat format,
                          int width, int height,
                          Map<EncodeHintType,?> hints) throws WriterException {

    Writer writer;
    switch (format) {
      case EAN_8:
        writer = new EAN8Writer();
        break;
      case UPC_E:
        writer = new UPCEWriter();
        break;
      case EAN_13:
        writer = new EAN13Writer();
        break;
      case UPC_A:
        writer = new UPCAWriter();
        break;
      case QR_CODE:
        writer = new QRCodeWriter();
        break;
      case CODE_39:
        writer = new Code39Writer();
        break;
      case CODE_93:
        writer = new Code93Writer();
        break;
      case CODE_128:
        writer = new Code128Writer();
        break;
      case ITF:
        writer = new ITFWriter();
        break;
      case PDF_417:
        writer = new PDF417Writer();
        break;
      case CODABAR:
        writer = new CodaBarWriter();
        break;
      case DATA_MATRIX:
        writer = new DataMatrixWriter();
        break;
      case AZTEC:
        writer = new AztecWriter();
        break;
      default:
        throw new IllegalArgumentException("No encoder available for format " + format);
    }
    return writer.encode(contents, format, width, height, hints);
  }

因为我们主要研究QR Code 二维码,所以我们看下,QRCodeWriter的encode方法.

@Override
  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) {
      if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
        errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
      }
      if (hints.containsKey(EncodeHintType.MARGIN)) {
        quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
      }
    }

    QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
    return renderResult(code, width, height, quietZone);
  }

首先进行了,一些常规的错误排查判断.然后设置了默认的错误纠正率级别,默认的是ErrorCorrectionLevel.L.
纠正率级别是一个枚举类型,有四种级别.级别越高,当你生成的二维码被遮挡后,你扫码成功的概率越大.

 /** L = ~7% correction */
  L(0x01),
  /** M = ~15% correction */
  M(0x00),
  /** Q = ~25% correction */
  Q(0x03),
  /** H = ~30% correction */
  H(0x02);

接着,通过Encoder的encode方法,对内容进行字节编码运算然后封装成QRCode对象
.通过renderResult方法,把结果渲染封装成BitMatrix对象,在这个方法里用到了我们之前传入的宽高参数,quietZone.这两部分的具体逻辑有需求的话可以自己进源码看,逻辑不是很复杂,但是比较繁琐,代码量挺多的,这边就不在仔细分析了.

BitMatrix result;
        try {
            result = new MultiFormatWriter().encode(content, getWantedCodeType(mType)
                    , barcodeImageWidth, barcodeImageHeight, hints);
            int width = result.getWidth();
            int height = result.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] = result.get(x, y) ? BLACK : WHITE;
                }
            }

            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            mHandler.post(mUpdateImageRunnable);

获得的BitMatrix对象,在经过上面的bitmap对象生成操作之后,就会生成对应内容的条码,生成条形码的逻辑就完成了.

结语

通过zxing实现生成条码的功能就完成了,本文所演示的代码和效果,已上传至开源库YZxing上,项目地址如下:
YZxing项目地址

如果需要对生成条形码的深层算法有兴趣的话,可以参考文章二维码生成细节 ,结合zxing源码,重点是我上文中提到的那几个类的实现.

猜你喜欢

转载自blog.csdn.net/qq_34902522/article/details/83582507