Mastering QR Code Generation in Java: From Simple Tokens to Custom Logos
Learn how to generate various QR codes in Java—including login tokens, promotional URLs, and temporary visitor codes—by analyzing use cases, selecting the ZXing library, and following step‑by‑step code examples with best‑practice tips and common pitfalls.
Business analysis: QR code use cases
We first break down QR code applications into typical scenarios:
Login QR code – temporary, often changes, contains a token or URL.
Event poster QR code – embedded in images, requires high visual quality.
QR code with logo – adds a brand logo for easier recognition.
Parameter promotion QR code – carries user ID, promotion code, etc., for tracking.
Temporary visitor QR code – time‑sensitive, may be encrypted and have an expiration.
In short, a QR code is not just a picture; it needs "brain".
Technology selection
In the Java ecosystem, the main options for QR code generation are:
ZXing (Zebra Crossing) – classic, stable, Google‑backed.
QrCode – lightweight, suitable for quick development.
QRCodeUtils – a custom wrapper for reuse.
We chose ZXing plus a custom wrapper because the community is active, it supports QR and barcodes, offers many configurable parameters, and has a lightweight dependency.
Hands‑on demo: generating QR codes in Java
1. Add Maven dependencies
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.2</version>
</dependency>2. Implement a QR code utility class
package com.moyu.qr;
import com.google.zxing.*;
import com.google.zxing.client.j2se.*;
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 an image file */
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));
}
}3. Generate a simple promotional QR code
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 successfully, scan to see!");
}
}4. Generate a branded QR code with a logo
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("QR code with logo generated!");
}
}Best practices & common pitfalls
Use high error‑correction level ErrorCorrectionLevel.H when adding a logo.
QR code size should be larger than 250 × 250 pixels for clear scanning on posters.
Keep content length reasonable; overly long URLs may cause generation failures.
Never set margin to 0 – most scanners will fail.
A logo that is too large will obscure the code.
Check file paths carefully to avoid I/O errors.
Conclusion
Although a QR code looks like a simple black‑and‑white pattern, it can carry rich business logic, marketing strategies, and user‑behavior tracking, making it a powerful bridge between offline and online experiences.
“QR code is the most primitive yet strongest bridge connecting the offline and online worlds.”
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
