Mastering QR Code Generation in Java: From Simple Tokens to Branded Logos

This article walks through the business scenarios for QR codes, evaluates Java libraries, provides a reusable utility class, and demonstrates step‑by‑step code for creating plain, parameterized, and logo‑embedded QR codes while sharing best‑practice tips and common pitfalls.

Top Architect
Top Architect
Top Architect
Mastering QR Code Generation in Java: From Simple Tokens to Branded Logos

Introduction

QR codes are widely used for login tokens, promotional posters, branded logos, temporary visitor passes, and parameterized referral links. Developers need a flexible way to generate them in Java.

Use Cases

Login QR code – short‑lived token or URL.

Promotional poster – embedded in images, high visual quality.

Logo QR code – center logo for brand recognition.

Parameterized referral – URL with user ID or campaign parameters.

Temporary visitor – time‑sensitive, possibly encrypted.

Library Selection

Two main Java libraries are available: ZXing (Zebra Crossing) , a mature library that supports QR codes and barcodes, and the lightweight com.google.zxing.qrcode module. The example uses ZXing because of its active community, extensive configuration options, and small dependency footprint.

Maven Dependencies

com.google.zxing:core:3.5.2
com.google.zxing:javase:3.5.2

Utility Class (QrCodeUtils)

package com.moyu.qr;

import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * General QR code generation utility.
 */
public class QrCodeUtils {

    /** Generate a simple QR code and save it as PNG. */
    public static void generateSimpleQrCode(String content, String filePath,
                                           int width, int height) throws Exception {
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        Path path = new File(filePath).toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

    /** Generate a QR code with a centered logo. */
    public static void generateQrCodeWithLogo(String content, String logoPath,
                                              String outputPath) throws Exception {
        int width = 300;
        int height = 300;
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 1);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix,
                new MatrixToImageConfig());
        BufferedImage logo = ImageIO.read(new File(logoPath));

        int logoWidth = qrImage.getWidth() / 5;
        int logoHeight = qrImage.getHeight() / 5;
        int x = (qrImage.getWidth() - logoWidth) / 2;
        int y = (qrImage.getHeight() - logoHeight) / 2;

        Graphics2D g = qrImage.createGraphics();
        g.drawImage(logo, x, y, logoWidth, logoHeight, null);
        g.dispose();

        ImageIO.write(qrImage, "PNG", new File(outputPath));
    }
}

Sample Usage

public class Main {
    public static void main(String[] args) throws Exception {
        String content = "https://yourdomain.com/register?ref=userid_123456";
        String savePath = "D:/qrcode/promo.png";
        QrCodeUtils.generateSimpleQrCode(content, savePath, 300, 300);
        System.out.println("Promotion QR code generated.");
    }
}
public class LogoDemo {
    public static void main(String[] args) throws Exception {
        String content = "https://yourapp.com/share?id=abc123";
        String logo = "D:/logo/logo.png";
        String output = "D:/qrcode/share_with_logo.png";
        QrCodeUtils.generateQrCodeWithLogo(content, logo, output);
        System.out.println("Logo QR code generated.");
    }
}

Best Practices & Pitfalls

Set ErrorCorrectionLevel.H when adding a logo to keep the code readable.

Use a minimum image size of 250 × 250 px for reliable scanning on posters.

Keep the encoded content short; very long URLs may exceed QR capacity.

Leave a small white margin (default 1) – most scanners require it.

Logo should occupy no more than 20 % of the QR area.

Verify file paths; incorrect paths cause runtime errors.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backendJavaZxingbest-practicesqrcodeqr-code-generationcode-sample
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.