Java QR Code Generator

Java QR Code Generator

Generating QR codes in Java is straightforward using a reliable library. This article shows a concise, practical method using ZXing (Zebra Crossing) to create PNG QR codes from text or URLs, plus options for size, error correction, and custom colors.

What you’ll need

  • Java 8+ (JDK)
  • Maven or Gradle (or add ZXing jars manually)
  • ZXing core and javase modules

Maven dependencies:

xml

<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.5.1</version> </dependency>

Simple QR code generator (PNG)

This example creates a PNG file from input text, with configurable size and error correction.

java

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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; public class QRCodeGenerator { public static void generate(String text, int size, String filePath) throws Exception { Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, “UTF-8”); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // L, M, Q, H hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter().encode( text, BarcodeFormat.QR_CODE, size, size, hints ); // Optional: custom colors (foreground, background) int foreground = 0xFF000000; // black int background = 0xFFFFFFFF; // white MatrixToImageConfig config = new MatrixToImageConfig(foreground, background); Path path = Paths.get(filePath); MatrixToImageWriter.writeToPath(bitMatrix, “PNG”, path, config); } public static void main(String[] args) throws Exception { String text = https://example.com”; int size = 300; String filePath = “qrcode.png”; generate(text, size, filePath); System.out.println(“QR code generated: “ + filePath); } }

Customization options

  • Size: change the width/height (e.g., 150–1000 px).
  • Error correction: L (7%), M (15%), Q (25%), H (30%) — higher gives more resiliency but larger modules.
  • Margin: white border in modules; default often 4; reduce for denser images.
  • Colors: set foreground/background in MatrixToImageConfig.
  • Encoding: use UTF-8 for non-ASCII text.

Embedding logos

To add a small logo at center:

  1. Generate the QR as BufferedImage (MatrixToImageWriter.toBufferedImage).
  2. Load logo, scale to ~15–25% of QR size.
  3. Draw logo centered using Graphics2D with anti-aliasing.
  4. Ensure error correction level is at least Q or H so scanning still works.

Tips for production

  • Test with multiple scanner apps/devices.
  • For print use higher DPI and larger pixel size.
  • Avoid heavy foreground patterns that reduce contrast.
  • Use high error correction when embedding logos.
  • Cache generated images for repeated requests.

Troubleshooting

  • Blank or unreadable QR: increase size, margin, or error correction.
  • Non-ASCII text appears wrong: ensure UTF-8 encoding.
  • Too dense or scanner fails: reduce data (shorten URL) or increase module size.

Minimal web-service example (Spring Boot)

  • Expose an endpoint that returns image/png generated on-the-fly using the same ZXing code and writing to HttpServletResponse output stream.

Quick summary

Use ZXing with proper hints (UTF-8, error correction), choose an appropriate size and margin, and optionally customize colors or embed a logo. Test across devices and increase error correction for logos or damage tolerance.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *