How to generate QR image using java and spring boot

To generate a QR code image using Java and Spring Boot, you can use the zxing library which is an open-source, multi-format 1D/2D barcode image processing library implemented in Java. 

 Here are the steps to generate a QR code image using Java and Spring Boot:

 1. Add the zxing library to your Spring Boot project by adding the following dependency to 
      your pom.xml file:

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




2. Create a QRCodeGenerator class that will generate the QR code image. 
     Here's an example of a QRCodeGenerator class:
  import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class QRCodeGenerator {

    private static final int QR_CODE_SIZE = 256;
    private static final String QR_CODE_TYPE = "png";

    public byte[] generateQRCodeImage(String text) throws WriterException, IOException {
        Map hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, QR_CODE_SIZE,
                                                                        QR_CODE_SIZE, hints);
        BufferedImage bufferedImage = new BufferedImage(QR_CODE_SIZE, QR_CODE_SIZE, 
                                                                 BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < QR_CODE_SIZE; x++) {
            for (int y = 0; y < QR_CODE_SIZE; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, QR_CODE_TYPE, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }
}

3. In your Spring Boot controller, inject the QRCodeGenerator class and call the
     generateQRCodeImage() method to generate the QR code image. 
     Here's an example of a Spring Boot controller:
   import com.google.zxing.WriterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

@RestController
public class QRCodeController {

    @Autowired
    private QRCodeGenerator qrCodeGenerator;

    @GetMapping(value = "/qrcode/{text}", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] generateQRCodeImage(@PathVariable String text) throws WriterException, IOException {
        return qrCodeGenerator.generateQRCodeImage(text);
    }
}

   
4. Test the QR code generator by running your Spring Boot application and accessing the /qrcode/{text}
     endpoint in your web browser. Replace {text} with the text that you want to encode in the QR code.

Comments

Popular posts from this blog

What is java?

What is TensorFlow ? Example with springboot and maven

What are different Power BI components and Architecture