android 实现条形码生成技术-Barcode4j、zxing

android 实现条形码生成技术-Barcode4j、zxing


解决方案

Java生成条形码的方案 -- barcode4j、zxing

barcode4j

barcode4j开源Java条形码生成库。支持多种编码格式,比如:code-39,code-128等
http://barcode4j.sourceforge.net/
 

zxing

是由google开源的1D/2D编解码类库。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android
 
本次采用了barcode4j作为解决方案
 

环境准备

下载barcode4j-light
barcode4j 依赖的lib包略显臃肿,其中包括了avalon-framework/servelet-api,
因此本次选择的是轻量级的版本barcode4j-light
 
maven地址
http://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j-light/2.0
 
1
2
3
4
5
< dependency >
< groupId >net.sf.barcode4j</ groupId >
< artifactId >barcode4j-light</ artifactId >
< version >2.0</ version >
</ dependency >

  

//另外,也可以下载barcode4j-bin包
http://sourceforge.net/projects/barcode4j/files/barcode4j/Barcode4J%202.0/
 
 

代码实例

BarcodeUtil工具类
 
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;
 
/**
  * 条形码工具类
  *
  * @author tangzz
  * @createDate 2015年9月17日
  *
  */
public  class  BarcodeUtil {
 
     /**
      * 生成文件
      *
      * @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;
     }
 
     /**
      * 生成字节
      *
      * @param msg
      * @return
      */
     public  static  byte [] generate(String msg) {
         ByteArrayOutputStream ous =  new  ByteArrayOutputStream();
         generate(msg, ous);
         return  ous.toByteArray();
     }
 
     /**
      * 生成到流
      *
      * @param msg
      * @param ous
      */
     public  static  void  generate(String msg, OutputStream ous) {
         if  (StringUtils.isEmpty(msg) || ous ==  null ) {
             return ;
         }
 
         Code39Bean bean =  new  Code39Bean();
 
         // 精细度
         final  int  dpi =  150 ;
         // module宽度
         final  double  moduleWidth = UnitConv.in2mm( 1 .0f / dpi);
 
         // 配置对象
         bean.setModuleWidth(moduleWidth);
         bean.setWideFactor( 3 );
         bean.doQuietZone( false );
 
         String format =  "image/png" ;
         try  {
 
             // 输出到流
             BitmapCanvasProvider canvas =  new  BitmapCanvasProvider(ous, format, dpi,
                     BufferedImage.TYPE_BYTE_BINARY,  false 0 );
 
             // 生成条形码
             bean.generateBarcode(canvas, msg);
 
             // 结束绘制
             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

二维码相对于条形码的优势

   数据容量更大;超越了字母数字的限制;具有抗损毁能力

关于条形码的各种编码格式
 
 android 实现条形码生成技术-Barcode4j、zxing_第1张图片
 
如何生成或识别二维码
推荐使用ZXing项目  https://github.com/zxing/zxing

你可能感兴趣的:(android)