Showing posts with label barcode. Show all posts
Showing posts with label barcode. Show all posts

Friday, February 17, 2017

Create a barcode Using Javascript Jquery

Hello, if you want to create a barcode using javascript you can do it as follows.

You can download the js file from this link, latter you can create a function for use the barcode.

In html we create the next div and the next button

 <div id="barcode"></div>  
 <button type="button" id="brnBarCode" onclick="load();">Barcode</button>  

Remember include the script downloaded jquery-barcode.min.js and the jquery script in the html file, for example:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>  
 <script src="<%=request.getContextPath()%>/static/js/jquery-barcode.min.js"></script>  


In this example we create a function called load, this function invoke the barcode method

 load = function(){  
  $("#barcode").barcode(  
  $('#docId').val(), // Value barcode (dependent on the type of barcode)   
  "code39" // types supported   
  );  
 }  

The button calls the function load, this function will create the barcode that it will be displayed in the div with id = barcode.


 Barcode types supported:  
 EAN 8  
 EAN 13  
 UPC  
 standard 2 of 5 (industrial)  
 interleaved 2 of 5  
 code 11  
 code 39  
 code 93  
 code 128  
 codabar  
 MSI  
 Data Matrix  

Reference: link

Wednesday, January 25, 2017

Java Create a Barcode using BarCode4J And Maven

If we want to create a barcode we can use the Barcode4J library.


If we are using Maven the dependency that we can use is this:

 <!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j -->  
 <dependency>  
   <groupId>net.sf.barcode4j</groupId>  
   <artifactId>barcode4j</artifactId>  
   <version>2.1</version>  
 </dependency>  
Ref: https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j/2.1


 If we are using Gradle the dependency that we can use is this:

 // https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j  
 compile group: 'net.sf.barcode4j', name: 'barcode4j', version: '2.1'  
Ref: https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j/2.1


The code in Java that we can use for create a barcode using Barcode4J is the next:

 import java.awt.image.BufferedImage;   
  import java.io.File;   
  import java.io.FileNotFoundException;   
  import java.io.FileOutputStream;   
  import java.io.OutputStream;   
  import org.krysalis.barcode4j.impl.code39.Code39Bean;   
  import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;   
  import org.krysalis.barcode4j.tools.UnitConv;   
  public class CodeBarServicesImpl implements CodeBarServices{   
    public void generateCodeBar(String codeBarValue) throws Exception {   
       String fullPath = null;   
       try {   
         Code39Bean bean = new Code39Bean();   
         final int dpi = 150;   
         String path = "D:\\";   
         String extension = ".jpg";   
         fullPath = path + codeBarValue + extension;   
         // Configure the barcode generator   
         bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); // makes the narrow   
         bean.setWideFactor(3);   
         bean.doQuietZone(false);           
         File outputFile = new File(fullPath);   
         OutputStream out = new FileOutputStream(outputFile);   
         try {             
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,   
            "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY,   
            false, 0);             
            // We Generate the barcode   
            bean.generateBarcode(canvas, codeBarValue);   
            canvas.finish();   
         } finally {   
            out.close();   
         }    
       } catch (Exception e) {           
         e.printStackTrace();   
         throw new Exception(e);   
       }    
    }      
  }