Problems encountered in two-dimensional code parsing of Wechat applet

Recently, in a small program from the Wechat api to get the two-dimensional code, and then the two-dimensional code analysis returned to the third party platform, the third party platform according to the link to generate two-dimensional code for users to use, in the analysis of two-dimensional code encountered some problems, here to share.

1. Development documentation

https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html As can be seen in the development documents, there are about three kinds of generated two-dimensional codes. The first and the third are limited in number. Because the first kind of two-dimensional codes can not be parsed by zXing at present (maybe my version is lower), the way to generate the third kind of small program two-dimensional codes is as follows (width input parameter is 430, the result is a binary stream byte of two-dimensional codes []).

2. Cutting

Because there is a line of words at the bottom of the generated two-dimensional code (micro-message scanning, using small programs), which causes errors in the decoding process, the current idea is to tailor the two-dimensional code. First, the byte [] is converted into Buffered Image and then tailored.

ByteArrayInputStream inputStream= new ByteArrayInputStream(qrCodeRsp.getResult());
        BufferedImage image = ImageIO.read(inputStream);
        /**Clipping the original image, currently accessing Wechat Wechat, returns 470*535 pixels 170620*/
        BufferedImage subImage = image.getSubimage(0, 0, image.getWidth(), (int) (image.getHeight() * 0.85));

Here we encounter a problem. The input of wechat is 430. In fact, the return of wechat interface is 470*535 DPI two-dimensional code. There will be a problem if there is too much or too little tailoring. Here we adopt a certain proportion of tailoring (0.85).

3. Analytical clipped images

jar package dependency:

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.2.1</version>
        </dependency>

Analytical 2-D Code Tool Class:

package com.demo.kowalski.utils;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Kowalski on 2017/6/16
 * Updated by Kowalski on 2017/6/16
 */
public class QrCodeUtils {

    /**
     * Analytic Two-Dimensional Code (QRCode)
     * @param image
     * @return
     */
    public static String decodeQrcode(BufferedImage image) throws NotFoundException {

        MultiFormatReader formatReader = new MultiFormatReader();

        BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

        //Define the parameters of the two-dimensional code:
        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET,"utf-8");//Define Character Sets
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        Result result = formatReader.decode(binaryBitmap, hints);//Start parsing

        return result.getText();
    }

    /**
     * Streaming picture decoding
     * @param   input
     * @return  String
     */
    public static String decodeQrcode(InputStream input) throws NotFoundException, IOException {

        BufferedImage image = ImageIO.read(input);
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Map<DecodeHintType,Object> hints = new LinkedHashMap<DecodeHintType,Object>();
        // The decoding settings are: utf-8,
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        //Optimizing Accuracy
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        //Complex mode, open PURE_BARCODE mode
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        Result result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }
}

There are two parsing methods in the tool class, one is stream, the other is BufferedImage, and the result is the content of the two-dimensional code.

"https://mp.weixin.qq.com/a/~~IDPi5UlUcRc~6N6CY6nW1xdSyX7RhQPnEg~~"

You can see that the link is strange. I guess Wechat should have done special treatment when it scanned the small program's two-dimensional code (clicking on the link directly can't go in), but this does not affect, because when we pass it to the third party, the third party will generate the two-dimensional code according to the same rules, and the result of the generated two-dimensional code scanner still points to the link (currently testing). No problem)

Keywords: Google Java QRCode

Added by klaaz on Thu, 20 Jun 2019 02:57:26 +0300