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
Libraries needed for PDF generation
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();
Read my latest article http://blog.sodhanalibrary.com/2014/07/export-qr-code-to-pdf-table-in-java.html#.U-oBOuOSxJk to know more
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; } }
คาสิโนออนไลน์ที่น่าเชื่อถือและมีความเป็นมืออาชีพที่สุดในตอนนี้
ReplyDeleteโปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
สมัครสล็อตออนไลน์ >>> Goldenslot
สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย
Thank you for making me aware of this news.
ReplyDeleteหางานราชการ
You are very good at that.
ReplyDeleteงานราชการ