Developer Documentation
Java
Updated September 19, 2026

Generate QR Codes in Java

Generate QR codes for text or URLs and export them as images in Java.

QR code libraries handle matrix construction, masking, and error correction for you. Your application still needs to choose suitable error correction, margin, image size, and payload encoding.

Quick Start#

The following example uses the standard Java API or a widely used package for this task.

Using ZXing#

Java
1// Dependencies: com.google.zxing:core and javase2QRCodeWriter writer = new QRCodeWriter();3BitMatrix matrix = writer.encode(4    "https://toolporta.com",5    BarcodeFormat.QR_CODE,6    ,7    8);9MatrixToImageWriter.writeToPath(matrix, "PNG", Path.of("toolporta.png"));

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Set error correction#

Java
1Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);2hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);3hints.put(EncodeHintType.MARGIN, );45BitMatrix matrix = writer.encode(payload, BarcodeFormat.QR_CODE, , , hints);

Important Notes#

Use UTF-8-capable payload handling and test actual scanners when encoding long or non-ASCII content.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement QR Code in other languages: