Quick-media Chinese QR code support

The Quick-Media project provides out-of-the-box utility classes for a number of multimedia operations, such as image editing and compositing, Markdown/HTML/SVG rendering, and audio processing. Of course, there are two-dimensional code generation and analysis of the focus of this article

Qrcode-plugin supports the generation of rich and cool TWO-DIMENSIONAL code. About ten days ago, a friend asked me a very interesting direction: could you change the black and white squares in the two-dimensional code into Chinese

Take advantage of the Dragon Boat Festival holiday eve free time, put this into QrCode plug-in, generate the following effect (from left to right, read from top to bottom, thousand characters 😝)

I. Instructions

1. The maven import

For Java users, maven can introduce dependency packages

<repositories>
    <repository>
        <id>yihui-maven-repo</id>
        <url>https://raw.githubusercontent.com/liuyueyi/maven-repository/master/repository</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.hui.media</groupId>
    <artifactId>qrcode-plugin</artifactId>
    <version>Against 2.4.1</version>
</dependency>
Copy the code

Or you can import dependencies using jitPack

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>


<! -- Please note that there are some differences between groupId and Github.
<dependency>
    <groupId>com.github.liuyueyi.quick-media</groupId>
    <artifactId>qrcode-plugin</artifactId>
    <version>2.4</version>
</dependency>
Copy the code

2. Source code

For the use of the source code is relatively simple, download the source code, directly in the test directory to write test case

Source code: Quick-media

3. The use case

First take a look, how to generate text TWO-DIMENSIONAL code, one of the simplest use case is as follows

/** * Text qr code, sequential rendering */
@Test
public void fontQr1(a) {
    String msg = "http://weixin.qq.com/r/FS9waAPEg178rUcL93oH";
    try {
        boolean ans = QrCodeGenWrapper.of(msg)
                .setErrorCorrection(ErrorCorrectionLevel.H)
                // Set the render mode to TXT
                .setDrawStyle(QrCodeOptions.DrawStyle.TXT)
                .setPicType("png")
                .asFile("/tmp/fontQr1.png");
    } catch(Exception e) { e.printStackTrace(); }}Copy the code

Qrcode-plugin provides the default text set of 1000 characters, font is Song Style, if you want to generate the top two-dimensional code (three standard detection graphics, recognition rate is higher) plus an option. SetDetectSpecial ()

/** * Text qr code, sequential rendering */
@Test
public void fontQr2(a) {
    String msg = "http://weixin.qq.com/r/FS9waAPEg178rUcL93oH";

    try {
        boolean ans = QrCodeGenWrapper.of(msg)
                // If no text is entered, thousand-character text is used by default
                // Default text order rendering
                // true probes have their own drawing rules
                .setDetectSpecial()
                .setErrorCorrection(ErrorCorrectionLevel.H)
                .setDrawStyle(QrCodeOptions.DrawStyle.TXT)
                .setPicType("png")
                .asFile("/tmp/fontQr2.png");
    } catch(Exception e) { e.printStackTrace(); }}Copy the code

Of course, we can also use custom text to generate two-dimensional code, and specify the way to select text for random

/** * Text qr code */
@Test
public void fontQr3(a) {
    String msg = "http://weixin.qq.com/r/FS9waAPEg178rUcL93oH";

    try {
        boolean ans = QrCodeGenWrapper.of(msg)
                .setQrText("Welcome to a gray.")
                // Specifies the random rendering of text
                .setQrTxtMode(QrCodeOptions.TxtMode.RANDOM)
                // true probes have their own drawing rules
                .setDetectSpecial()
                .setErrorCorrection(ErrorCorrectionLevel.H)
                .setDrawStyle(QrCodeOptions.DrawStyle.TXT)
                // Magnify the NxN when adjacent NXNS are small black blocks (caution, as some men such as' I 'can't fill 2x2 blocks nicely)
                .setDrawEnableScale(true)
                .setPicType("png")
                .asFile("/tmp/fontQr3.png");
    } catch(Exception e) { e.printStackTrace(); }}Copy the code

4. Background text

In addition to the above text, there is also a figure below, the TWO-DIMENSIONAL code shows a word

These two-dimensional codes are made using a background rendering method, a red letter on a light gray background. These two-dimensional codes are made using these paintings

@Test
public void bgQrTxt(a) {
    try {
        String msg = "http://weixin.qq.com/r/FS9waAPEg178rUcL93oH";
        BufferedImage bgImg = GraphicUtil.createImg(500.500.null);
        Graphics2D g2d = GraphicUtil.getG2d(bgImg);
        g2d.setColor(Color.LIGHT_GRAY);
        g2d.fillRect(0.0.500.500);

        Font font = new Font("宋体", Font.BOLD, 500);
        g2d.setFont(font);
        g2d.setColor(Color.RED);
        g2d.drawString("Code".0.500 - g2d.getFontMetrics().getDescent() / 2);
        g2d.dispose();

        boolean ans =
                QrCodeGenWrapper.of(msg).setBgImg(bgImg).setBgStyle(QrCodeOptions.BgImgStyle.PENETRATE).setBgW(500)
                        .setBgH(500).setW(500).asFile("/tmp/bqrTxt.png");
    } catch(Exception e) { e.printStackTrace(); }}Copy the code

II. The other

1. A gray Blog:liuyueyi.github.io/hexblog

A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll

2. Statement

As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate

  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840

3. Scan attention

A gray blog