Generating QR Codes in Java with ZXing and Maven
This tutorial demonstrates how to create a Maven Java project, add the ZXing library, implement utility classes for QR code generation and decoding, and run a sample application that produces a QR code image and reads its content back, complete with code examples and configuration details.
The article walks through creating a standard Maven project and configuring the <project> <modelVersion>4.0.0</modelVersion> <groupId>org.javaboy</groupId> <artifactId>QRCode</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- ZXing QR code library provided by Google --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> </dependencies> </project> to include the ZXing core jar.
Two utility classes are provided:
BufferedImageLuminanceSource – a custom LuminanceSource implementation that converts a BufferedImage to the grayscale data required by ZXing. The full source code is wrapped in a ... block to preserve all method definitions, including getRow , getMatrix , crop , and rotation support.
QRCodeUtil – contains static methods to create QR code images, optionally embed a logo, write the image to disk, and decode a QR code from a file or path. Key constants such as CHARSET , FORMAT_NAME , QRCODE_SIZE , WIDTH , and HEIGHT are defined, and the implementation uses MultiFormatWriter , BitMatrix , and BinaryBitmap from ZXing. The complete class source is included inside a ... block.
The QRCodeApplication class shows a simple main method that:
Defines the text (e.g., a URL) to encode.
Calls QRCodeUtil.encode to generate a QR code image file.
Calls QRCodeUtil.decode to read the content back from the generated image.
Prints the decoded string to the console.
All code snippets are presented unchanged inside ... tags, ensuring readers can copy and run them directly. The article also includes screenshots of the generated QR code and links to the source repository (gitee.com/jian_bo_bai/QRCode).
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.
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.