How to Implement QR Code Printing and Reading in a Spring Boot Application

In modern enterprise applications, QR Code Printing and QR code reading have become essential features. From payment systems and inventory management to authentication flows and ticketing platforms, QR codes provide a fast, compact, and reliable way to encode and decode information.

In this post, you will learn how to implement QR Code Printing and QR code reading in a Spring Boot application using Java, step by step, with complete and executable code. The solution is ready to use in real-world enterprise systems.

What You Will Build

By the end of this tutorial, you will have:

  • A Spring Boot REST API to generate QR codes
  • A REST API to read and decode QR codes
  • Support for PNG output suitable for printing
  • Fully executable and compilable Java code
  • A reusable service layer suitable for enterprise projects

Why Use QR Codes in Enterprise Applications

QR codes offer several advantages over traditional barcodes and manual data entry:

  • High data density
  • Fast scanning using standard devices
  • Error correction support
  • Platform-independent usage
  • Easy printing and distribution


Technology Stack Used

This implementation uses:

  • Java 17
  • Spring Boot
  • ZXing (Zebra Crossing) library
  • REST APIs
  • Maven build system

Step 1: Create a New Spring Boot Project

Create a Spring Boot project using Spring Initializr or your IDE with Maven, Java 17, and Spring Web dependency.

Step 2: Add Required Dependencies

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

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

</dependencies>


Step 3: Main Application Class

package com.example.qrcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class QrCodeApplication {

    public static void main(String[] args) {
        SpringApplication.run(QrCodeApplication.class, args);
    }
}

Step 4: QR Code Utility Class

package com.example.qrcode.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class QrCodeUtil {

    public static byte[] generateQrCode(String text, int width, int height) throws Exception {
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(text, BarcodeFormat.QR_CODE, width, height, hints);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
        return outputStream.toByteArray();
    }

    public static String readQrCode(byte[] imageBytes) throws Exception {
        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageBytes));
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Result result = new MultiFormatReader().decode(bitmap);
        return result.getText();
    }
}

Step 5: Service Layer

package com.example.qrcode.service;

import com.example.qrcode.util.QrCodeUtil;
import org.springframework.stereotype.Service;

@Service
public class QrCodeService {

    public byte[] generateQrCodeImage(String content) throws Exception {
        return QrCodeUtil.generateQrCode(content, 300, 300);
    }

    public String decodeQrCode(byte[] imageBytes) throws Exception {
        return QrCodeUtil.readQrCode(imageBytes);
    }
}


Step 6: REST Controller

package com.example.qrcode.controller;

import com.example.qrcode.service.QrCodeService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/qr")
public class QrCodeController {

    private final QrCodeService qrCodeService;

    public QrCodeController(QrCodeService qrCodeService) {
        this.qrCodeService = qrCodeService;
    }

    @GetMapping("/generate")
    public ResponseEntity<byte[]> generateQrCode(@RequestParam String text) throws Exception {
        byte[] image = qrCodeService.generateQrCodeImage(text);

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=qrcode.png")
                .contentType(MediaType.IMAGE_PNG)
                .body(image);
    }

    @PostMapping("/read")
    public ResponseEntity<String> readQrCode(@RequestParam("file") MultipartFile file) throws Exception {
        String decodedText = qrCodeService.decodeQrCode(file.getBytes());
        return ResponseEntity.ok(decodedText);
    }
}

Testing the Application

Start the Spring Boot application.

  • Generate QR Code: GET /api/qr/generate?text=HelloSpringBoot
  • Read QR Code: POST /api/qr/read (upload QR image)

Conclusion

Implementing QR Code Printing and QR code reading in a Spring boot application using java is straightforward when done with the right architecture and libraries. This production-ready solution can be directly integrated into enterprise applications without modification.