Java barcode generation technology barcode4j

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


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
版权声明
本文为[mb60f93b6c4c5e3]所创,转载请带上原文链接,感谢
https://javamana.com/2021/07/20210723172157016Z.html

  1. redis 监控的一些构思
  2. Some ideas of redis monitoring
  3. Spring中获取request的几种方法,及其线程安全性分析
  4. Several methods of getting request in spring and its thread security analysis
  5. 20分钟带你掌握JavaScript Promise和 Async/Await
  6. 20 minutes with async and async
  7. Mybatis - introduction, simple entry program
  8. Java Concurrent Programming: concurrent HashMap
  9. Mybatis: one to one association query
  10. Java IO
  11. Object oriented [day07]: review of knowledge points (11)
  12. Chapter 2 spring
  13. Process of building minicube in local k8s environment
  14. Interpretation of jQuery source code
  15. Making web full screen effect with jquery
  16. Java 7 exception handling new features - address suppressed () method
  17. Spring 4 uses websocket
  18. Using openldap to answer questions and using java to complete LDAP authentication
  19. Implementation of MySQL high availability cluster with corosync + pacemaker and DRBD
  20. Java production of a simple tank war
  21. Design pattern day02
  22. Hadoop pseudo Distributed installation and running test example
  23. Java Concurrent Programming: callable, future and futuretask
  24. Understanding the three characteristics of Java encapsulation
  25. Understanding closure of JavaScript
  26. Java set excel worksheet to read only
  27. Distributed Java service platform baratine
  28. Use springboot + MySQL + JPA to add, delete, change, query and page the database
  29. Distributed services framework Dubbo / dubbox getting started example
  30. Zookeeper notes (1) installation, deployment and hello world
  31. Oracle database trigger Usage Summary
  32. MySQL master replication with slave server