Home > Java > javaTutorial > Use Java to generate QR code code sharing

Use Java to generate QR code code sharing

巴扎黑
Release: 2017-07-18 15:11:10
Original
1668 people have browsed it

Characteristics of QR code:

1. High-density coding, large information capacity

Can accommodate up to 1850 uppercase letters or 2710 numbers or 1108 Bytes, or more than 500 Chinese characters, the information capacity is about dozens of times higher than that of ordinary barcodes.

2. Wide encoding range

This barcode can encode pictures, sounds, text, signatures, fingerprints and other digitized information and express it with barcodes; it can Represents text in multiple languages; can represent image data.

3. Strong fault tolerance and error correction function

This allows the 2D barcode to be read correctly even if it is partially damaged due to perforations, stains, etc. , information can still be recovered if 50% of the damaged area is damaged.

4. High decoding reliability

It is much lower than ordinary barcode decoding error rate of 2 parts per million, and the bit error rate does not exceed 10 million one part.

5. Encryption measures can be introduced

Good confidentiality and anti-counterfeiting properties.

6. Low cost, easy to make, durable

Because of the above characteristics, QR codes are becoming more and more popular and their applications are becoming more and more widespread ( For detailed information, please see Baidu Encyclopedia (the introduction is not the focus of this article), so knowing how to develop QR codes is a very good knowledge reserve, so this article will explain how to generate and parse QR codes.

This article talks about the generation and analysis of QR codes through zxing. It looks very simple. Just enter the code directly.

import java.io.File;import java.io.IOException;import java.nio.file.Path;import java.util.HashMap;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class TestZXing {public static void main(String[] args) {int width=300;int height=300;
        String format="png";
        String contents="www.baidu.com";
        HashMap map=new HashMap();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");
        map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
        map.put(EncodeHintType.MARGIN, 0);try {
            BitMatrix bm = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height);
            Path file=new File("D:/img.png").toPath();
            MatrixToImageWriter.writeToPath(bm, format, file);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {            e.printStackTrace();
        }
    }
}
Copy after login

The above code will generate a The content link is the QR code of www.baidu.com

The code for parsing this QR code is as follows

import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.HashMap;import javax.imageio.ImageIO;import com.google.zxing.BinaryBitmap;import com.google.zxing.EncodeHintType;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;public class TestRead {public static void main(String[] args) {try {
            MultiFormatReader reader=new MultiFormatReader();//需要详细了解MultiFormatReader的小伙伴可以点我一下官方去看文档File f=new File("D:/img.png");
            BufferedImage image=ImageIO.read(f);
            BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            HashMap map =new HashMap();
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            Result result = reader.decode(bb,map);
            System.out.println("解析结果:"+result.toString());
            System.out.println("二维码格式类型:"+result.getBarcodeFormat());
            System.out.println("二维码文本内容:"+result.getText());
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
Copy after login

The execution results are as follows

解析结果:www.baidu.com
二维码格式类型:QR_CODE
二维码文本内容:www.baidu.com
Copy after login

You can download the specific jar package by yourself

Link: Password: kcjx

The above is the detailed content of Use Java to generate QR code code sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template