I am participating in the Mid-Autumn Festival Creative Submission contest, please see: Mid-Autumn Festival Creative Submission Contest for details

Another year’s Mid-Autumn festival is coming, how to rub a wave of its heat? As a coder, can I write a poem in code? The idea is good, but it is a little difficult, so simple, to their two-dimensional code, add a moon cake

1. Add moon cakes to logo?

Add mooncakes in the TWO-DIMENSIONAL code, it seems that there is no difficulty, directly search a two-dimensional code generator online, and then find a mooncake picture, as the logo of the shoot, game over

(Avoid audit does not pass, deliberately erase the TWO-DIMENSIONAL code eye, can not identify is normal)

If that’s all there is to it, this article will be boring. Let’s look at something different

2. Reject black and white block, full screen QR code

Next, we will use the quick-media:qrcode-plugin on github to implement more interesting qrcode customization

First introduce dependencies

<dependency>
    <groupId>com.github.liuyueyi.media</groupId>
    <artifactId>qrcode-plugin</artifactId>
    <version>2.6.1</version>
</dependency>
Copy the code

Next we use three moon cake images to generate a TWO-DIMENSIONAL code with a screen full of moon cakes

To generate the above TWO-DIMENSIONAL code is also very simple, the implementation code is as follows

String sourcePrefix = "/Users/user/Documents/qr/";
QrCodeGenWrapper.of(msg)
        .setW(500)
        .setH(500)
        .setErrorCorrection(ErrorCorrectionLevel.M)
        .setDiaphaneityFill(true) // Fill the transparent part with the background color
        .setDrawBgColor(Color.WHITE)
        .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE_V2)
        .setImgResourcesForV2(RenderImgResourcesV2.create()
                // The following configuration 1 x 1 square corresponding moon cake picture
                .addSource(1.1).addImg(sourcePrefix + "00.png")
                .addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1) // The last 1 indicates that the moon cake appears only once
                .addImg(sourcePrefix + "03.png")
                .build()
                // The following configuration of 2 x 2 blocks corresponds to the moon cake picture
                .addSource(2.2)
                .addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()
                .addSource(3.3).addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()

        )
        .asFile(sourcePrefix + "/out0.jpg");
Copy the code

Although a TWO-DIMENSIONAL code with a screen full of moon cakes is generated above, recognition is a problem, and the key point is three code eyes (detection graphics).

3. Dress your eyes

Since the above code eye is not good enough, it is replaced with a custom code eye to replace the default generated, improve the recognition rate

To achieve the above two-dimensional code, the method is also very simple, on the basis of the previous, specify the detection graph

// Set the upper-left eye
.setLTDetectImg(sourcePrefix + "d1.jpg")
// Set the lower left corner of the screen
.setLDDetectImg(sourcePrefix + "d2.jpg")
// Set the code eye in the upper right corner
.setRTDetectImg(sourcePrefix + "d3.jpg")
Copy the code

The other Settings are no different from the previous ones, as follows

String sourcePrefix = "/Users/user/Documents/qr/";
QrCodeGenWrapper.of(msg)
        .setW(500)
        .setH(500)
        .setErrorCorrection(ErrorCorrectionLevel.M)
        .setDiaphaneityFill(true) // Fill the transparent part with the background color
        .setDrawBgColor(Color.WHITE)
        .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE_V2)
        .setLTDetectImg(sourcePrefix + "d1.jpg")
        .setLDDetectImg(sourcePrefix + "d2.jpg")
        .setRTDetectImg(sourcePrefix + "d3.jpg")
        .setImgResourcesForV2(RenderImgResourcesV2.create()
                .addSource(1.1).addImg(sourcePrefix + "00.png")
                .addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png")
                .build()
                .addSource(2.2)
                .addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()
                .addSource(3.3).addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()

        )
        .asFile(sourcePrefix + "/out_d0.jpg");
Copy the code

4. Qr codes with poems

Since the beginning of the Mid-Autumn festival to use the code to write poetry to rub heat, although I can not write poetry, but as a full-time CTRL + C/CTRL + V coder, COPY I can, then we put Su Shi’s great ancient works, to print on our two-dimensional code

The above two-dimensional code is a bit interesting, directly in Chinese to render, to achieve it is also very simple, the following simple configuration

String sourcePrefix = "/Users/user/Documents/qr/";
QrCodeGenWrapper.of(msg)
        .setDetectSpecial() // Use the standard brick code
        .setDrawStyle(QrCodeOptions.DrawStyle.TXT)
        .setQrText("When will the moon have wine to ask the sky, I do not know what year it is in the palace in the sky. I would like to ride the wind and return home, but I fear that the sky is cold and lonely and dance to find out what shadow is like in the world." +
                "When parting comes, people have joys and sorrows, the moon has its ups and downs, the moon has its ups and downs, this matter is difficult forever. I wish we can share the beauty of this beautiful moonlight for a long time.")
        .setLogo(sourcePrefix+"/logo2.jpg") // Add a logo
        .setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
        .setLogoBorderBgColor(Color.GRAY)
        .asFile(sourcePrefix + "/ft.jpg");
Copy the code

5. There are poems and qr codes of moon cakes

Only poems have no moon cakes are not beautiful; Only moon cakes without poetry is not beautiful, then put together, generate a qr code similar to the following

DrawStyle: qrcodeOption. DrawStyle: qrcodeOption. DrawStyle: qrcodeOption. DrawStyle: qrcodeOption. DrawStyle: qrcodeOption. DrawStyle

IMAGE_V2 {
    @Override
    public void draw(Graphics2D g2d, int x, int y, int w, int h, BufferedImage img, String txt) {
        String source  = "When will the moon have wine to ask the sky, I do not know what year it is in the palace in the sky. I would like to ride the wind and return home, but I fear that the sky is cold and lonely and dance to find out what shadow is like in the world." +
                "When parting comes, people have joys and sorrows, the moon has its ups and downs, the moon has its ups and downs, this matter is difficult forever. I wish we can share the beauty of this beautiful moonlight for a long time.";
        if ("false".equals(txt)) {
            if (Math.random() < 0.8 f) {
                if (Math.random() < 0.6 f) {
                    int offsetX = w / 5, offsetY = h / 5;
                    int width = w - offsetX * 2, height = h - offsetY * 2;
                    g2d.fillRect(x + offsetX, y + offsetY, width, height);
                    return;
                }

                int index = QuickQrUtil.getIndex();
                if (index >= source.length()) {
                    int offsetX = w / 5, offsetY = h / 5;
                    int width = w - offsetX * 2, height = h - offsetY * 2;
                    g2d.fillRect(x + offsetX, y + offsetY, width, height);
                } else {
                    Font oldFont = g2d.getFont();
                    if(oldFont.getSize() ! = w) { Font newFont = QuickQrUtil.font(oldFont.getName(), oldFont.getStyle(), w); g2d.setFont(newFont); } g2d.drawString(source.substring(index, index+1), x, y + w); g2d.setFont(oldFont); }}else {
                g2d.drawImage(img.getScaledInstance(w, h, Image.SCALE_SMOOTH), x, y, null); }}else {
            g2d.drawImage(img.getScaledInstance(w, h, Image.SCALE_SMOOTH), x, y, null); }}@Override
    public boolean expand(DotSize dotSize) {
        return true; }}Copy the code

6. Get the QR code moving

The above are static, then we give it the ability to move, to generate a GIF version of the TWO-DIMENSIONAL code, we need to do things are not much, find a GIF background

The above dynamic TWO-DIMENSIONAL code generation method is also very simple, the basic configuration is the same as before, the difference is to specify the background GIF

String sourcePrefix = "/Users/user/Documents/qr/";
QrCodeGenWrapper.of(msg)
        .setW(160)
        .setH(160)
        .setErrorCorrection(ErrorCorrectionLevel.M)
        .setDiaphaneityFill(true) // Fill the transparent part with the background color
        .setDrawBgColor(Color.WHITE)
        .setPadding(0)
        .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE_V2)
        .setDetectSpecial()
        .setImgResourcesForV2(RenderImgResourcesV2.create()
                .addSource(1.1).addImg(sourcePrefix + "00.png")
                .addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png")
                .build()
                .addSource(2.2)
                .addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()
                .addSource(3.3).addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()

        )
        .setLogo(sourcePrefix+"/logo.jpg")
        .setLogoRate(12)
        .setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
        .setLogoBorderBgColor(Color.GRAY)
        // The following is the setting background picture, which means that the TWO-DIMENSIONAL code is overlaid on the specified coordinates of the background picture
        .setBgImg(sourcePrefix + "/bg4.gif")
        .setBgStyle(QrCodeOptions.BgImgStyle.FILL)
        .setBgStartX(10)
        .setBgStartY(120)
        .asFile(sourcePrefix + "/out3.gif");
}
Copy the code

In addition to the above dynamic, you can also achieve a logo-like GIF effect, such as

It is also easy to implement the pose, using FtImg to specify the GIF image in front

String sourcePrefix = "/Users/user/Documents/qr/";
QrCodeGenWrapper.of(msg)
        .setW(500)
        .setH(500)
        .setDiaphaneityFill(true) // Fill the transparent part with the background color
        .setDrawBgColor(ColorUtil.OFF_WHITE) // Set the background color to beige to facilitate the GIF display effect
        .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE_V2)
        .setDetectSpecial()
        .setImgResourcesForV2(RenderImgResourcesV2.create()
                .addSource(1.1).addImg(sourcePrefix + "00.png")
                .addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png")
                .build()
                .addSource(2.2)
                .addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()
                .addSource(3.3).addImg(sourcePrefix + "00.png").addImg(sourcePrefix + "01.png")
                .addImg(sourcePrefix + "02.png".1)
                .addImg(sourcePrefix + "03.png").build()
        )
        // Download the GIF directly from the Internet, scale it to 112 * 120, and draw it in the middle of the QR code
        .setFtImg("https://b-ssl.duitang.com/uploads/item/201609/14/20160914224309_WNUaE.gif")
        .setFtW(112)
        .setFtH(120)
        .setFtStartX(-194)
        .setFtStartY(-190)
        .asFile(sourcePrefix + "/out_ft1.gif");
Copy the code

7. Last but not least

Finally, all resources in this article come from the network, if there is infringement, contact delete, all two-dimensional code generation in this article, are based on the open source project Quick-media to generate, interested partners can try github.com/liuyueyi/qu… After all, the moon cake festival arrived, how also have to eat

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
  • Wechat official account: One Grey Blog