search
HomeJavajavaTutorialHow to synthesize complex QR code images with java zxing

Overall idea:

Introduce zxing’s mature QR code generation interface to generate standard QR code files, and add relevant text descriptions to the QR codes through the java graphics and image processing API. As needed, you can synthesize Add relevant background to the final image. An example is shown in the figure below:

How to synthesize complex QR code images with java zxing

  • 1. Let’s take a bitmap first. The core code for generating a QR code image is as follows

        /**
         * 定义二维码的参数
         */
        HashMap<EncodeHintType, Object> hints = new HashMap();
        //指定字符编码为“utf-8”
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //指定二维码的纠错等级为中级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //设置图片的边距
        hints.put(EncodeHintType.MARGIN, 1);
        /**
         * 生成二维码
         */
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_HEIGHT, hints);
            Path file = new File(filePath).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            log.error("二维码生成出错:/permitDownload: error", e);
        }
  • 2. Add text to the QR code

    /**
     * 给二维码下方添加说明文字
     *
     * @param image 原二维码
     * @param topText  顶部说明文字
     * @param downText 底部说明文字
     * @return 带说明文字的二维码
     */
    private static BufferedImage addNote(BufferedImage image, String topText, String downText) {
        Image src = image.getScaledInstance(QRCODE_WIDTH, QRCODE_HEIGHT, Image.SCALE_DEFAULT);
        BufferedImage tag = new BufferedImage(QRCODE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = tag.createGraphics();//设置文字
        g2.setColor(Color.BLACK);
        g2.setBackground(Color.WHITE);
        g2.clearRect(0,0,QRCODE_WIDTH, IMAGE_HEIGHT);
        //设置顶部文本并计算坐标
        // 保证操作系统包含“宋体”字体,如果没有上传字体至JAVA_HOME/jre/lib/fonts下
        FontMetrics fm = getFontByWidth(new Font("宋体", Font.PLAIN, DEFAULT_FONT_SIZE), topText, g2);
        //文字的宽度
        int fontWidth = fm.stringWidth(topText);
        //文字高度
        int fontHeight = fm.getHeight();
        /**
         * 顶部添加文字并居中
         */
        g2.drawString(topText, (QRCODE_WIDTH - fontWidth) / 2,  (TEXT_DEFAULT_HEIGHT - fontHeight) / 2 + fm.getFont().getSize());
        /**
         * 绘制二维码
         */
        g2.drawImage(src, 0, TEXT_DEFAULT_HEIGHT, null);
        // 设置底部文字字体并计算坐标
        // 保证操作系统包含“宋体”字体,如果没有上传字体至JAVA_HOME/jre/lib/fonts下
        fm = getFontByWidth(new Font("宋体", Font.PLAIN, DEFAULT_FONT_SIZE), downText, g2);
        //文字的宽度
        fontWidth = fm.stringWidth(downText);
        //文字高度
        fontHeight = fm.getHeight();
        /**
         * 添加底部文字
         */
        g2.drawString(downText, (QRCODE_WIDTH - fontWidth) / 2,  QRCODE_HEIGHT + TEXT_DEFAULT_HEIGHT+(TEXT_DEFAULT_HEIGHT - fontHeight) / 2 + fm.getFont().getSize());
        g2.dispose();
        image = tag;
        image.flush();
        return image;
    }

Knowledge point: The length of the text at the bottom will change, and the current design only puts one line of text , so the text size will be dynamically changed according to the number of words in a reasonable range (not too small to be recognized), using the FontMetrics object, this point will be helpful to most students

  • 3. Dynamic Modify the font and size

    /**
     * 根据文字长度改变文字大小
     *
     * @param font 默认字体
     * @param note 文字内容
     * @param g2 图像画布
     * @return 处理后的字体封装
     */
    private static FontMetrics getFontByWidth(Font font, String note, Graphics2D g2) {
        FontMetrics fm = g2.getFontMetrics(font);
        int textWidth = fm.stringWidth(note);//文字的宽度
        if (textWidth > QRCODE_WIDTH) {
            int fontSize = (int) ((TEMP_PARAM / textWidth) * font.getSize());
            font = new Font(font.getName(), font.getStyle(), fontSize);
        }
        g2.setFont(font);
        return g2.getFontMetrics(font);
    }
  • 4. The last step is to combine the blue background image and the QR code image, and that’s it.

Quartet of picture synthesis

The first step: Create the canvas, you need to set the width and height of the canvas, the unit should be pixels

BufferedImage tag = new BufferedImage(QRCODE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);

The second step Step: Create the graphics object Graphics2D on the canvas. You can set information such as background, foreground, border, width and other information based on your understanding of the image knowledge points

Graphics2D g2 = tag.createGraphics();//设置文字

Step 3: Synthesize the picture, the order of adding the pictures in the process , picture size, and coordinate position will affect the final presentation effect. If the final effect does not meet the design requirements, adjusting these three parameters will definitely help

Image src = image.getScaledInstance(QRCODE_WIDTH, QRCODE_HEIGHT, Image.SCALE_DEFAULT);
...
 /**
* 绘制二维码
 */
g2.drawImage(src, 0, TEXT_DEFAULT_HEIGHT, null);

Step 4: Generate the final new picture

    /**
     * 给二维码图片添加背景图片
     *
     * @param qrPic   二维码
     * @param backImage 背景图片
     */
    private static void createNewPng(File qrPic, BufferedImage backImage) {
        try {
            if (!qrPic.isFile()) {
                log.error("二维码临时路径不存在!");
            }
            /**
             * 读取背景图片,并构建绘图对象--画布
             */
            Graphics2D g = backImage.createGraphics();
            /**
             * 读取二维码图片
             */
            BufferedImage qrcodeImage = ImageIO.read(qrPic);
            //开始绘制图片
            g.drawImage(qrcodeImage, 48, 120, qrcodeImage.getWidth(), qrcodeImage.getHeight(), null);
            g.dispose();
            ImageIO.write(backImage, "png", qrPic);
        } catch (Exception e) {
            log.error("绘制二维码出错!");
        }
    }

The second picture is generated in the same process as the first picture, just change the order in [2. Add text to the QR code] from top, middle and bottom to left and right

Pits that have been stepped on

  • The pixels and size of the background image need to match the QR code, otherwise the ratio between the QR code and the background will be seriously out of balance or the QR code display will be incomplete

  • QR code adds text garbled development environment (windows), test environment (centos server version). There are no problems in local development and testing. After packaging and deploying to the server, all Chinese characters appear garbled (various transcoding, font settings, debugging for a long time), and the problem still has not changed. Finally, I suddenly had an idea and suspected that it was a system font problem. I checked the font information of the test environment (centos) and indeed there was no "Songti". I set it to the system's own or default font, but the problem still persists. Finally, copy the font from the development system (c:\windows\fonts\simsun.ttc) to the test system (JAVA_HOME/jre/libs/fonts) and restart the application, and the problem is perfectly solved. It is a bit troublesome to prepare fonts for system installation and deployment. I don’t know if there is a better way. Fonts include logical fonts and physical fonts. Let’s do this for now.

  • The background image loading problem project is a springboot project. The background image is stored in the resources folder. No exceptions were found in local development and testing. The background image cannot be found after being packaged and deployed to the server. The original code is as follows

        String backFilePath = "template/down.png";
        ClassPathResource resource = new ClassPathResource(backFilePath);
        File file = resource.getFile();

I have tried all the ways to load images on the Internet, but it has no effect. Finally, I changed it to the input stream, and the image synthesis is normal. The code is as follows

        /**
         * 必须通过流方式,否则不同操作系统无法拿到背景图片信息
         */
        String backFilePath = "template/down.png";
        ClassPathResource resource = new ClassPathResource(backFilePath);
        BufferedImage bi = ImageIO.read(resource.getInputStream());

The above is the detailed content of How to synthesize complex QR code images with java zxing. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

Is Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksIs Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksMay 13, 2025 am 12:03 AM

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

Demystifying the JVM: Your Key to Understanding Java ExecutionDemystifying the JVM: Your Key to Understanding Java ExecutionMay 13, 2025 am 12:02 AM

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Is java still a good language based on new features?Is java still a good language based on new features?May 12, 2025 am 12:12 AM

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

What Makes Java Great? Key Features and BenefitsWhat Makes Java Great? Key Features and BenefitsMay 12, 2025 am 12:11 AM

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Top 5 Java Features: Examples and ExplanationsTop 5 Java Features: Examples and ExplanationsMay 12, 2025 am 12:09 AM

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor