QR code is getting famous because of its easy readable nature. You can take snapshot and get the data from it. Now its time to generate PDF file with full QR codes.

Libraries we need 

Libraries needed for QR Code generation
  1. zxing-core-1.7.jar
  2. zxing-j2se-1.7.jar
Libraries needed for PDF generation
  1. itextpdf-5.5.0

Code to generate QR code

    /**
     * To Generate QR Code Image from text
     * @param qrCodeData - text for QR Code image
     * @param charset 
     * @param qrCodeheight - QR Code image height
     * @param qrCodewidth - QR Code image width
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static BufferedImage createQRCode(String qrCodeData, String charset,
            int qrCodeheight, int qrCodewidth) throws WriterException,
            IOException {
        // Generate BitMatrix
        BitMatrix matrix = new MultiFormatWriter().encode(
                new String(qrCodeData.getBytes(charset), charset),
                BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        // Converting BitMatrix to Buffered Image 
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }

Code to generate PDF file

Document document = new Document();
        
// Get file
PdfWriter.getInstance(document, new FileOutputStream("qrcode.pdf"));
document.open();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
// Get QR code for "Hello"
BufferedImage bi = createQRCode("Hello","UTF-8", 200, 200);
ImageIO.write(bi, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
Image image = Image.getInstance(imageInByte);

// Add image to PDF document
document.add(image);
document.close();

Java class for Exporting QR Codes to PDF files

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class ExporToPDF {
    // color codes
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    public static void main(String[] args) throws WriterException, IOException,
            NotFoundException {
        
        String charset = "UTF-8"; // or "ISO-8859-1"
        Document document = new Document();
        String filePath="qrcode.pdf";
        try {
            
            // Get file
            PdfWriter.getInstance(document, new FileOutputStream("qrcode.pdf"));
            document.open();
            
            // data to covert to QR code
            String[] qrdata = {"srinivas","dasari","blog.sodhanalibrary.com","pojo.sodhanalibrary.com","flaticon.sodhanalibrary.com","google","facebook","reddit","blogger","yourname"};
            float x = 100f, y = 830f, posx, posy;
            int n=0;
            for (int j = 0; j < 5; j++) {
                posy = y - j * 200f;
                for (int i = 0; i < 2; i++) {
                    posx = x + i * 200f;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    
                    // Get QR code
                    BufferedImage bi = createQRCode(qrdata[n], charset, 200, 200);
                    ImageIO.write(bi, "png", baos);
                    baos.flush();
                    byte[] imageInByte = baos.toByteArray();
                    baos.close();
                    Image image = Image.getInstance(imageInByte);
                    image.setAbsolutePosition(posx, posy);
                    
                    // Add image to PDF document
                    document.add(image);
                    n++;
                }
            }
            document.close();
            System.out.println(new File(filePath).getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * To Generate QR Code Image from text
     * @param qrCodeData - text for QR Code image
     * @param charset 
     * @param qrCodeheight - QR Code image height
     * @param qrCodewidth - QR Code image width
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static BufferedImage createQRCode(String qrCodeData, String charset,
            int qrCodeheight, int qrCodewidth) throws WriterException,
            IOException {
        
        // Generate BitMatrix
        BitMatrix matrix = new MultiFormatWriter().encode(
                new String(qrCodeData.getBytes(charset), charset),
                BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        
        // Converting BitMatrix to Buffered Image 
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }
    
}

0 comments:

Blogroll

Follow this blog by Email

Popular Posts