Quick media Chinese QR code support

Quick media Chinese QR code support

Quick-Media The project provides a series of out of the box tools for multimedia operations, such as image editing and synthesis, markdown/html/svg rendering, audio processing; and of course, the two-dimensional code generation and analysis highlighted in this paper

QRcode plugin supports the generation of rich and cool QR codes. About ten days ago, a little friend proposed a very interesting direction. Can we change the black and white squares in the QR code into Chinese

Take advantage of the free time on the eve of the Dragon Boat Festival holiday, integrate this into the QrCode plug-in. The generation effect is as follows (from left to right, read from top to bottom, thousand character text 😝)

<!-- more -->

1. I nstructions for use

1. maven import

For the small partners of java environment, we can use maven to introduce dependency package

<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>2.4.1</version>
</dependency>

Or use jitpack to import dependencies

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


<!-- Please note that groupId and github There are some differences -->
<dependency>
    <groupId>com.github.liuyueyi.quick-media</groupId>
    <artifactId>qrcode-plugin</artifactId>
    <version>2.4</version>
</dependency>

2. Source code

The use of the source code is relatively simple. Download the source code and write the test case directly in the test directory

Source code address: Quick-Media

3. Using case

Let's take a look at how to generate a text QR code. The simplest use case is as follows

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

The above use can be said to be very simple and clear. The default text set provided by QRcode plugin is thousand character text, and the font is Song typeface. If you want to generate the top two-dimensional code (three standard detection graphics, higher recognition rate), add an option. setDetectSpecial()

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

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

Of course, we can also use custom text to generate QR code, and specify that the method of selecting text is random

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

    try {
        boolean ans = QrCodeGenWrapper.of(msg)
                .setQrText("Welcome to your attention")
                // Specifies how text is rendered randomly
                .setQrTxtMode(QrCodeOptions.TxtMode.RANDOM)
                // If true, the probe graph has its own drawing rules
                .setDetectSpecial()
                .setErrorCorrection(ErrorCorrectionLevel.H)
                .setDrawStyle(QrCodeOptions.DrawStyle.TXT)
                // When the adjacent NxN are small black squares, zoom in (use with caution, because some men, such as' Yi ', can't fill 2x2 squares in a friendly way)
                .setDrawEnableScale(true)
                .setPicType("png")
                .asFile("/tmp/fontQr3.png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

4. Background text

In addition to the above text mode, there is also a case in which the QR code displays a word, as shown in the figure below

The above two-dimensional code is mainly realized by the rendering method of background image. The background image is a light gray background color and red letter. The QR code adopts the penerate background image penetration mode. The specific implementation is as follows

@Test
public void bgQrTxt() {
    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("Song style", 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();
    }
}

2. Others

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

A gray personal blog, records all the blog articles in study and work. Welcome to visit

2. Statement

It's not as good as a letter. The above contents are all from one family. Due to limited personal ability, there are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct, and thank you

3. Scan attention

A gray blog

Tags: Mobile QRCode Maven github Java

Posted on Mon, 29 Jun 2020 22:22:46 -0400 by webref.eu