Recently in the work of a small partner encountered a problem about two-dimensional code: using Zxing, given the specified size of two-dimensional code size, when the size is less than a certain range, the size of the two-dimensional code generated and the size of the specified size difference is large. For example: for example, I give a TWO-DIMENSIONAL code generation size of 36 * ️36px, the actual size of the generated two-dimensional code is 21 * 21 px.

The principle of

If you are familiar with the two-dimensional code mechanism, you should know that two-dimensional code has good error correction ability and large capacity to carry information. In order to achieve better information processing at different scales, the TWO-DIMENSIONAL code provides 40 levels of specifications, and the generated size range is 21 * 21 ~ 177 * 177.

In the above scenario, the QR code generated by Zxing is Version 1, and the qr code returned is a matrix information of 21 * 21. The given size 36 * 36 is not an integer multiple of 21, and can only accommodate one 21 * 21 size information. Therefore, we can only generate 21 * 21 PX two-dimensional code when we convert the information matrix into two-dimensional code pictures.

The specific generation principle and details have been described in detail in this article. Please refer to the basic structure and generation principle of QR Code. Here I only list the knowledge points that are relevant to this article:

1, the Version

There are 1 to 40 different versions of qr codes, each with its own “code structure”. (that is, the square black and white dots of the QR code) starts from version 1(21 codes ×21 codes), and increases by 4 codes in vertical and horizontal directions, respectively, until version 40(177 codes ×177 codes).

2, error correction level

In other words, there are four levels of error tolerance. The higher the level, the higher the error correction ability will be, but the data volume will increase, and the coding size will also increase.

Error correction level Fault tolerance
L About 7%
M About 15%
O About 25%
Q About 30%

3. Coding method

That is, two-dimensional code information encoding mode, two-dimensional code can be encoded according to numbers, English alphanumeric, binary, Chinese characters, Japanese and other ways of information.

How to solve

According to the above principle of TWO-DIMENSIONAL code, the size of two-dimensional code is always a multiple of one of the 40 versions. Therefore, for the generation of two-dimensional code of smaller size, the version and fault tolerance of two-dimensional code and the coding method can be adjusted to meet the needs of a given size. In our actual production environment, we use UTF8 to encode information, so the size of the QR code depends on the version selected and the level of error correction. Specific ideas are as follows:

  • 1. Find the version closest to the given size
  • 2. Determine whether the version can contain the given information and find the optimal fault tolerance level
  • 3. Return code size and fault tolerance level code as follows:
Public class QrCodeTest {// Maximum size, Specific data please refer to the https://www.qrcode.com/zh/about/version.html static final int [] [] VERSION_ERROR_ARR = new int [] [] { 20,16,12,8,8,7,4 {10}, {}, {32,26,20,15}, {48,38,28,21},,52,37,27 {65}, {82,65,45,36},,75,53,39 {95}, {118,93,66,52}, ,93,74,80,60 {141111}, {167131}, {198155109} 30, {226177125}, {262204149109}, {282223159120}, {320254180136}, {361277198154}, {397310224173}, {442345243191}, {488384272208}, {528410297235}, {572438314248}, {618480348270}, {672528376284}, {721561407315}, {784614440330}, {842652462365}, {902692496385}, {940732534405}, {1002778559430}, {1066843604457}, {1132894634486}, {1201947684518}, 1273100 {2719553}, {1347106} 0756590, 1417111 {3790605}, {1496117} 6832647. 1577122 {4876673}, {1661129} 2923701, 1729136 {2972750}, {1817143, 5102, 4784}}; public static int[] getQrCodeVersionAndError(String str, int size) throws Exception{if(21 > size){
            throw new IllegalAccessException("Qr code size should not be smaller than 21*21px"); } // Find the version closest to the given size int tVersion = (int) math.floor ((size-21)/4.0); int errorCode = -1; int len = str.length(); int minLen = len; int tLen1 = Math.abs(size - tVersion * 4 - 21); int tLen2 = Math.abs(size - (tVersion + 1) * 4 - 21); int version = tLen1 <= tLen2 ? tVersion : tVersion + 1;if(VERSION_ERROR_ARR[version][0] > len){
            returnnew int[]{version, 0}; } // Too small to hold user-given datafor(int i = version+1; i < VERSION_ERROR_ARR.length; i++){
            for(int j = 0; j < VERSION_ERROR_ARR[i].length; j++){
                int min = Math.abs(len - VERSION_ERROR_ARR[i][j]);
                if(min == 0){
                    minLen = min;
                    version = i;
                    errorCode = j;
                    break;
                }else if(min <= minLen){ minLen = min; version = i; errorCode = j; }}if(1! = errorCode){break; }}if(-1 == errorCode){
            throw new IllegalAccessException("This string is too large");
        }

        return new int[]{version, errorCode};
    }

    public static void main(String[] args) throws Exception{
        print("hello", 21);print("hello", 22);print("hellohellohellohellohello", 42);print("hello"43); } private static voidprint(String str, int size) throws Exception{
        int[] result = getQrCodeVersionAndError(str,size);
        System.out.println(str+","+size + ": ["+result[0] + "," + result[1]+"]"); }}Copy the code

Thank you

  • Two-dimensional code generation details and principles
  • The amount of information and version of the QR code
  • QR code error correction function