Friday, 9 April 2021

String to QR Code Image Generator Using Java

 Hi ,


Hope You are doing well.


I came up with the requirement, where I need to generate QR code Image file for the input String.


package demo;


import com.google.zxing.BarcodeFormat;

import com.google.zxing.BinaryBitmap;

import com.google.zxing.ChecksumException;

import com.google.zxing.DecodeHintType;

import com.google.zxing.FormatException;

import com.google.zxing.MultiFormatReader;

import com.google.zxing.NotFoundException;

import com.google.zxing.Result;

import com.google.zxing.WriterException;


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 com.google.zxing.qrcode.QRCodeReader;

import com.google.zxing.qrcode.QRCodeWriter;


import java.awt.image.BufferedImage;


import java.io.ByteArrayInputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;


import java.nio.file.FileSystems;

import java.nio.file.Path;


import java.util.Base64;


import java.util.HashMap;

import java.util.Map;


import javax.imageio.ImageIO;


public class QRCodeGenerator {

    private static final String QR_CODE_IMAGE_PATH = "C:\\Users\\sah75706.DS\\Desktop\\POC\\GeneratedFiles\\MyQRCoderr.png";


    private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException,

                                                                                                        IOException {

        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);


        Path path = FileSystems.getDefault().getPath(filePath);

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }


    public static void main(String[] args) throws NotFoundException {

        try {

            generateQRCodeImage("This is my first QR Code", 350, 350, QR_CODE_IMAGE_PATH);

            System.out.println("QRCode Generated");


        } catch (WriterException e) {

            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());

        } catch (IOException e) {

            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());

        }

    }

}




Cheers!!

3 comments:

  1. Since almost all modern-day smartphones come with QR-code-scanning capabilities, you can make your marketing campaign accessible to anyone. generator kodow qr

    ReplyDelete
  2. Proper servicing keeps marine generators running smoothly. Cummins Onan Marine Generators

    ReplyDelete

String to QR Code Image Generator Using Java

 Hi , Hope You are doing well. I came up with the requirement, where I need to generate QR code Image file for the input String. package dem...