Press enter to see results or esc to cancel.


How to Generate or Read QR code Dynamically using JAVA

    This project is developer in zxing API.

  1. Project Structure in Eclipse,

    
    
  2. The 3 jars used are,
    1. zxing-core-2.0.jar
    2. zxing-1.7-javase.jar
    3. MySQL Connector
  3. The below code will generate the QR code with data chillyfacts.com
    Create_QR.java

    
    package com.chillyfacts.com;
    import java.io.File;
    import java.util.HashMap;
    import java.util.Map;
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter; 
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    public class Create_QR {
        public static void main(String[] args) {
            try {
                String qrCodeData = "www.chillyfacts.com";
                String filePath = "D:\\QRCODE\\chillyfacts.png";
                String charset = "UTF-8"; // or "ISO-8859-1"
                Map < EncodeHintType, ErrorCorrectionLevel > hintMap = new HashMap < EncodeHintType, ErrorCorrectionLevel > ();
                hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
                BitMatrix matrix = new MultiFormatWriter().encode(
                    new String(qrCodeData.getBytes(charset), charset),
                    BarcodeFormat.QR_CODE, 200, 200, hintMap);
                MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
                    .lastIndexOf('.') + 1), new File(filePath));
                System.out.println("QR Code image created successfully!");
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }
    
  4. Output of the above code,

    
    
  5. Read_QR.java

    
    package com.chillyfacts.com;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import javax.imageio.ImageIO;
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.EncodeHintType;
    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.common.HybridBinarizer;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    public class Read_QR {
        public static void main(String[] args) throws WriterException, IOException,
            NotFoundException {
                try {
                    String filePath = "D:\\QRCODE\\chillyfacts.png";
                    String charset = "UTF-8";
                    Map < EncodeHintType, ErrorCorrectionLevel > hintMap = new HashMap < EncodeHintType, ErrorCorrectionLevel > ();
                    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
                    System.out.println("Data read from QR Code: " + readQRCode(filePath, charset, hintMap));
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        public static String readQRCode(String filePath, String charset, Map hintMap)
        throws FileNotFoundException, IOException, NotFoundException {
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
                new BufferedImageLuminanceSource(
                    ImageIO.read(new FileInputStream(filePath)))));
            Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap);
            return qrCodeResult.getText();
        }
    }
    
  6. Out put of above code,

    Data read from QR Code: www.chillyfacts.com
    
  7. Generate the QR code with data from MySQL database.
    DBConnection.java

    
    package com.chillyfacts.com;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class DBConnection {        
    	public Connection getConnection(){          
    		Connection connection=null;                      
    		System.out.println("Connection called");                  
    		try {                                                                  
    			Class.forName("com.mysql.jdbc.Driver");              
    		connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return connection;
    	}
    }
     
    
  8. The below code will take Data from mysql table and create QR for for each link in mysql table.
    MySQL table data,

    Dyanamic_QR.java

    
    package com.chillyfacts.com;
    import java.io.File;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.HashMap;
    import java.util.Map;
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    public class Dyanamic_QR {
    	public static void main(String[] args) {
    		try {
    			DBConnection obj_DBConnection = new DBConnection();
                Connection connection = obj_DBConnection.getConnection();
                String query = "select * from links";
                Statement stmt = null;
                stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                while (rs.next()) {
                	Dyanamic_QR.generate_qr(rs.getString("sl_no"),rs.getString("links"));
                }
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    	}
    	public static void generate_qr(String image_name,String qrCodeData) {
            try {
                String filePath = "D:\\QRCODE\\"+image_name+".png";
                String charset = "UTF-8"; // or "ISO-8859-1"
                Map < EncodeHintType, ErrorCorrectionLevel > hintMap = new HashMap < EncodeHintType, ErrorCorrectionLevel > ();
                hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
                BitMatrix matrix = new MultiFormatWriter().encode(
                    new String(qrCodeData.getBytes(charset), charset),
                    BarcodeFormat.QR_CODE, 200, 200, hintMap);
                MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
                    .lastIndexOf('.') + 1), new File(filePath));
                System.out.println("QR Code image created successfully!");
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }
    
  9. Output of Dynamic QR generated as per the data from MySQL Table

    
    

Download the source code.

QR_Write_Read.rar