Press enter to see results or esc to cancel.


Generate Barcodes Dynamically Using JAVA


In this video I have shown how to create Barcodes in Image and PDF dynamically using JAVA.

  1. Project Structure
  2. In this project 3 jars are used,
    1. barcode4j.jar
    2. itextpdf-5.1.0.jar
    3. Mysql connector jar
  3. Barcode_Image.java
    
    package BARCODE;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.FileOutputStream;
    import org.krysalis.barcode4j.impl.code128.Code128Bean;
    import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
    public class Barcode_Image {
    	public static void main(String[] args) {
    		Barcode_Image.createImage("chillyfacts.png", "999999999");
    		System.out.println("finished");
    	}
    	public static void createImage(String image_name,String myString)  {
    		try {
    		Code128Bean code128 = new Code128Bean();
    		code128.setHeight(15f);
    		code128.setModuleWidth(0.3);
    		code128.setQuietZone(10);
    		code128.doQuietZone(true);
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/x-png", 300, BufferedImage.TYPE_BYTE_BINARY, false, 0);
    		code128.generateBarcode(canvas, myString);
    		canvas.finish();
    		//write to png file
    		FileOutputStream fos = new FileOutputStream("C:\\Users\\MIRITPC\\Desktop\\jas\\New folder\\"+image_name);
    		fos.write(baos.toByteArray());
    		fos.flush();
    		fos.close();
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    	}
    }
  4. The above code will generate chillyfacts.png as below,
  5. Barcode_PDF.java
    
    package BARCODE;
    import java.io.FileOutputStream;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.PageSize;
    import com.itextpdf.text.pdf.Barcode128;
    import com.itextpdf.text.pdf.BarcodeEAN;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfWriter;
    public class Barcode_PDF {
    	public static void main(String[] args) {
    		Barcode_PDF.createPDF("chillyfacts.pdf","jinujawad");
    	}
    	public static void createPDF(String pdfFilename,String myString) {
    	    Document doc = new Document();
    	    PdfWriter docWriter = null;
    	    try {
    	        docWriter = PdfWriter.getInstance(doc, new FileOutputStream("C:\\Users\\MIRITPC\\Desktop\\jas\\New folder\\"+pdfFilename));
    	        doc.addAuthor("jinujawad");
    	        doc.addCreationDate();
    	        doc.addProducer();
    	        doc.addCreator("chillyfacts.com");
    	        doc.addTitle("chillyfacts Barcode test");
    	        doc.setPageSize(PageSize.LETTER);
    	        doc.open();
    	        PdfContentByte cb = docWriter.getDirectContent();
    
    	        Barcode128 code128 = new Barcode128();
    	        code128.setCode(myString.trim());
    	        code128.setCodeType(Barcode128.CODE128);
    	        Image code128Image = code128.createImageWithBarcode(cb, null, null);
    	        code128Image.setAbsolutePosition(10, 700);
    	        code128Image.scalePercent(125);
    	        doc.add(code128Image);
    
    	        BarcodeEAN codeEAN = new BarcodeEAN();
    	        codeEAN.setCode(myString.trim());
    	        codeEAN.setCodeType(BarcodeEAN.EAN13);      
    	        Image codeEANImage = code128.createImageWithBarcode(cb, null, null);
    	        codeEANImage.setAbsolutePosition(10, 600);
    	        codeEANImage.scalePercent(125);
    	        doc.add(codeEANImage);
    	    } catch (DocumentException dex) {
    	        dex.printStackTrace();
    	    } catch (Exception ex) {
    	        ex.printStackTrace();
    	    } finally {
    	        if (doc != null) {
    	            doc.close();
    	        }
    	        if (docWriter != null) {
    	            docWriter.close();
    	        }
    	    }
    	}
    }
    
    
  6. The above code will generate chillyfacts.png as below,
  7. To create the Barcodes dynamically from MySQL database you can use below class,
    DBConnection.java

    
    package com.chillyfacts.com;
    import java.sql.Connection;
    import java.sql.DriverManager; 
    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/jsp_crud","root", "root");
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return connection;
    	}
    }
    
    

    Generate_Dynamic_Bar_Code.java

    
    package com.chillyfacts.com;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import BARCODE.Barcode_Image;
    import BARCODE.Barcode_PDF;
    public class Generate_Dynamic_Bar_Code {
    	public static void main(String[] args) {
    		 	PreparedStatement ps=null;
    			Connection connection=null;
    			DBConnection obj_DBConnection=new DBConnection();
    			connection=obj_DBConnection.getConnection();
    			ResultSet rs=null;
    		try { 
    			String query="select * from user";
    			ps = connection.prepareStatement(query);
    			rs=ps.executeQuery();
    			while(rs.next()){
    				Barcode_Image.createImage(rs.getString("mobile")+".png", rs.getString("mobile"));
    				Barcode_PDF.createPDF(rs.getString("mobile")+".pdf", rs.getString("mobile"));
    				System.out.println("Creating Barcode for "+rs.getString("mobile"));
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			if(connection!=null){
    				try {
    						connection.close();
    					}
    				 catch (Exception e2) {
    					e2.printStackTrace();
    				}
    			}
    			if(ps!=null){
    				try {
    					ps.close();
    					}
    				 catch (Exception e2) {
    					 e2.printStackTrace();
    				}
    			}
    		}
    		}	
    }
    
  8. Download complete project here,
    Generate_barcodes.rar