Java生成条形码code128

Java生成条形码code128

版权声明:原创文章欢迎转载,转载请注明出处 https://blog.csdn.net/w605283073/article/details/81637674

生成code 128条形码工具类

maven依赖

 
  1. <!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j -->

  2. <dependency>

  3. <groupId>net.sf.barcode4j</groupId>

  4. <artifactId>barcode4j</artifactId>

  5. <version>2.1</version>

  6. </dependency>

gradle依赖

compile("net.sf.barcode4j:barcode4j:2.1")

工具代码:

 
  1.  
  2. import org.apache.commons.lang.ObjectUtils;

  3. import org.krysalis.barcode4j.HumanReadablePlacement;

  4. import org.krysalis.barcode4j.impl.code128.Code128Bean;

  5. import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

  6.  
  7. import java.awt.image.BufferedImage;

  8. import java.io.ByteArrayOutputStream;

  9. import java.io.IOException;

  10.  
  11. /**

  12. * 条形码工具类

  13. *

  14. * @author 明明如月

  15. * @date 2018/08/13

  16. */

  17. public class BarCodeUtils {

  18.  
  19.  
  20. /**

  21. * 生成code128条形码

  22. *

  23. * @param height 条形码的高度

  24. * @param width 条形码的宽度

  25. * @param message 要生成的文本

  26. * @param withQuietZone 是否两边留白

  27. * @param hideText 隐藏可读文本

  28. * @return 图片对应的字节码

  29. */

  30. public static byte[] generateBarCode128(String message, Double height, Double width, boolean withQuietZone, boolean hideText) {

  31. Code128Bean bean = new Code128Bean();

  32. // 分辨率

  33. int dpi = 512;

  34. // 设置两侧是否留白

  35. bean.doQuietZone(withQuietZone);

  36.  
  37. // 设置条形码高度和宽度

  38. bean.setBarHeight((double) ObjectUtils.defaultIfNull(height, 9.0D));

  39. if (width != null) {

  40. bean.setModuleWidth(width);

  41. }

  42. // 设置文本位置(包括是否显示)

  43. if (hideText) {

  44. bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);

  45. }

  46. // 设置图片类型

  47. String format = "image/png";

  48.  
  49. ByteArrayOutputStream ous = new ByteArrayOutputStream();

  50. BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,

  51. BufferedImage.TYPE_BYTE_BINARY, false, 0);

  52.  
  53. // 生产条形码

  54. bean.generateBarcode(canvas, message);

  55. try {

  56. canvas.finish();

  57. } catch (IOException e) {

  58. //ByteArrayOutputStream won't happen

  59. }

  60.  
  61. return ous.toByteArray();

  62. }

  63.  
  64.  
  65. }

 

CODE 128 条码生成方法

2012年06月20日 18:17:05 dengchonglin 阅读数 4258

Code 128 Barcode Table

ValueCode Set
A
Code Set
B
Code Set
C
Bar/Space Pattern
B S B S B S
0SPSP002 1 2 2 2 2
1!!012 2 2 1 2 2
2""022 2 2 2 2 1
3##031 2 1 2 2 3
4$$041 2 1 3 2 2
5%%051 3 1 2 2 2
6&&061 2 2 2 1 3
7''071 2 2 3 1 2
8((081 3 2 2 1 2
9))092 2 1 2 1 3
10**102 2 1 3 1 2
11++112 3 1 2 1 2
12,,121 1 2 2 3 2
13--131 2 2 1 3 2
14..141 2 2 2 3 1
15//151 1 3 2 2 2
1600161 2 3 1 2 2
1711171 2 3 2 2 1
1822182 2 3 2 1 1
1933192 2 1 1 3 2
2044202 2 1 2 3 1
2155212 1 3 2 1 2
2266222 2 3 1 1 2
2377233 1 2 1 3 1
2488243 1 1 2 2 2
2599253 2 1 1 2 2
26::263 2 1 2 2 1
27;;273 1 2 2 1 2
28<<283 2 2 1 1 2
29==293 2 2 2 1 1
30>>302 1 2 1 2 3
31??312 1 2 3 2 1
32@@322 3 2 1 2 1
33AA331 1 1 3 2 3
34BB341 3 1 1 2 3
35CC351 3 1 3 2 1
36DD361 1 2 3 1 3
37EE371 3 2 1 1 3
38FF381 3 2 3 1 1
39GG392 1 1 3 1 3
40HH402 3 1 1 1 3
41II412 3 1 3 1 1
42JJ421 1 2 1 3 3
43KK431 1 2 3 3 1
44LL441 3 2 1 3 1
45MM451 1 3 1 2 3
46NN461 1 3 3 2 1
47OO471 3 3 1 2 1
48PP483 1 3 1 2 1
49QQ492 1 1 3 3 1
50RR502 3 1 1 3 1
51SS512 1 3 1 1 3
52TT522 1 3 3 1 1
53UU532 1 3 1 3 1
54VV543 1 1 1 2 3
55WW553 1 1 3 2 1
56XX563 3 1 1 2 1
57YY573 1 2 1 1 3
58ZZ583 1 2 3 1 1
59[[593 3 2 1 1 1
60\\603 1 4 1 1 1
61]]612 2 1 4 1 1
62^^624 3 1 1 1 1
63__631 1 1 2 2 4
64NUL`641 1 1 4 2 2
65SOHa651 2 1 1 2 4
66STXb661 2 1 4 2 1
67ETXc671 4 1 1 2 2
68EOTd681 4 1 2 2 1
69ENQe691 1 2 2 1 4
70ACKf701 1 2 4 1 2
71BELg711 2 2 1 1 4
72BSh721 2 2 4 1 1
73HTi731 4 2 1 1 2
74LFj741 4 2 2 1 1
75VTk752 4 1 2 1 1
76FFI762 2 1 1 1 4
77CRm774 1 3 1 1 1
78SOn782 4 1 1 1 2
79SIo791 3 4 1 1 1
80DLEp801 1 1 2 4 2
81DC1q811 2 1 1 4 2
82DC2r821 2 1 2 4 1
83DC3s831 1 4 2 1 2
84DC4t841 2 4 1 1 2
85NAKu851 2 4 2 1 1
86SYNv864 1 1 2 1 2
87ETBw874 2 1 1 1 2
88CANx884 2 1 2 1 1
89EMy892 1 2 1 4 1
90SUBz902 1 4 1 2 1
91ESC{914 1 2 1 2 1
92FS|921 1 1 1 4 3
93GS}931 1 1 3 4 1
94RS~941 3 1 1 4 1
95USDEL951 1 4 1 1 3
96FNC 3FNC 3961 1 4 3 1 1
97FNC 2FNC 2974 1 1 1 1 3
98SHIFTSHIFT984 1 1 3 1 1
99CODE CCODE C991 1 3 1 4 1
100CODE BFNC 4CODE B1 1 4 1 3 1
101FNC 4CODE ACODE A3 1 1 1 4 1
102FNC 1FNC 1FNC 14 1 1 1 3 1
103Start AStart AStart A2 1 1 4 1 2
104Start BStart BStart B2 1 1 2 1 4
105Start CStart CStart C2 1 1 2 3 2
106StopStopStop2 3 3 1 1 1 2

 

 

 

Example... To calculate the check digit for the barcode data string 'Code 128'

                              Value		     Total
			      =====                  =====
             Start Code B      104                    104
Position 1              C       35         1 x 35 =    35
Position 2              o       79         2 x 79 =   158
Position 3              d       68         3 x 68 =   204
Position 4              e       69         4 x 69 =   276
Position 5                       0         5 x  0 =     0
Position 6              1       17         6 x 17 =   102
Position 7              2       18         7 x 18 =   126
Position 8              8       24         8 x 24 =   192
                                                     =====
                                                     1197
                                                     =====
             1197/103 = 11 remainder 64
#endregion
即根据上表, 把没个字符的value值 乘上 字符在字符串中的位置(从1开始算)
把没个乘积加起来
因为CODESET B 为104(这是规定, 不要问我为什么)
所以, 还要加上 104 
最后, 除以103, 所得的余数就是 校验码 的value值, 然后在表中找到对应的字符
就可以了.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值