Java barcode generation technology barcode4j
mb60f93b6c4c5e3 2021-07-28 21:21:58

background

At present, the application scenarios of QR code have spread all over the Internet platforms , It's usually the product / The unique number of the product is stored in the QR code for scanning identification .
And barcode technology for production environments still exists , Such as hardware equipment manufacturing 、 supply 、 Logistics transportation and so on .
In common product information management 、 In the material order system , There are multiple ways to generate and print barcodes ( One dimensional code ) Demand scenarios for .
 

Solution

Java Scheme for generating barcode -- barcode4j、zxing

barcode4j

barcode4j Open source Java Barcode generation library . Supports multiple encoding formats , such as :code-39,code-128 etc.
 

zxing

By google Open source 1D/2D Codec class library . The goal is to be able to deal with QR code 、Data Matrix、UPC Of 1D Bar code to decode . It provides clients on a variety of platforms, including :J2ME、J2SE and Android
 
This time barcode4j As a solution
 

Environmental preparation

download barcode4j-light
barcode4j Rely on the lib The bag is slightly bloated , Which includes avalon-framework/servelet-api,
Therefore, the lightweight version is selected this time barcode4j-light
 
maven Address
 
1
2
3
4
5
< dependency >
< groupId >net.sf.barcode4j</ groupId >
< artifactId >barcode4j-light</ artifactId >
< version >2.0</ version >
</ dependency >

// in addition , You can also download barcode4j-bin package
 
 

Code instance

BarcodeUtil Tool class
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package  utils;
 
import  java.awt.image.BufferedImage;
import  java.io.ByteArrayOutputStream;
import  java.io.File;
import  java.io.FileNotFoundException;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.OutputStream;
 
import  org.apache.commons.lang.StringUtils;
import  org.krysalis.barcode4j.impl.code39.Code39Bean;
import  org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import  org.krysalis.barcode4j.tools.UnitConv;
 
/**
  * Barcode tools
  *
  * @author tangzz
  * @createDate 2015 year 9 month 17 Japan
  *
  */
public  class  BarcodeUtil {
 
     /**
      * Generate the file
      *
      * @param msg
      * @param path
      * @return
      */
     public  static  File generateFile(String msg, String path) {
         File file =  new  File(path);
         try  {
             generate(msg,  new  FileOutputStream(file));
         catch  (FileNotFoundException e) {
             throw  new  RuntimeException(e);
         }
         return  file;
     }
 
     /**
      * Generate bytes
      *
      * @param msg
      * @return
      */
     public  static  byte [] generate(String msg) {
         ByteArrayOutputStream ous =  new  ByteArrayOutputStream();
         generate(msg, ous);
         return  ous.toByteArray();
     }
 
     /**
      * Generate to stream
      *
      * @param msg
      * @param ous
      */
     public  static  void  generate(String msg, OutputStream ous) {
         if  (StringUtils.isEmpty(msg) || ous ==  null ) {
             return ;
         }
 
         Code39Bean bean =  new  Code39Bean();
 
         // Fineness
         final  int  dpi =  150 ;
         // module Width
         final  double  moduleWidth = UnitConv.in2mm( 1 .0f / dpi);
 
         // Configuration object
         bean.setModuleWidth(moduleWidth);
         bean.setWideFactor( 3 );
         bean.doQuietZone( false );
 
         String format =  "image/png" ;
         try  {
 
             // Output to stream
             BitmapCanvasProvider canvas =  new  BitmapCanvasProvider(ous, format, dpi,
                     BufferedImage.TYPE_BYTE_BINARY,  false 0 );
 
             // Generate barcode
             bean.generateBarcode(canvas, msg);
 
             // Finish drawing
             canvas.finish();
         catch  (IOException e) {
             throw  new  RuntimeException(e);
         }
     }
 
     public  static  void  main(String[] args) {
         String msg =  "123456789" ;
         String path =  "barcode.png" ;
         generateFile(msg, path);
     }
}
 

FAQ

Advantages of QR code over bar code

    Larger data capacity ; Beyond the alphanumeric limit ; Resistant to damage

About various coding formats of bar code
 
 
 
How to generate or identify QR code
Recommended ZXing project   https://github.com/zxing/zxing
Please bring the original link to reprint ,thank
Similar articles

2021-06-04

2021-06-05

2021-06-13