1) 添加依赖
1 <dependency> 2 <groupId>net.sf.barcode4j</groupId> 3 <artifactId>barcode4j-light</artifactId> 4 <version>2.0</version> 5 </dependency>
2)编写工具类
1 mport org.apache.commons.codec.binary.Base64; 2 import org.apache.commons.lang.StringUtils; 3 import org.krysalis.barcode4j.impl.code39.Code39Bean; 4 import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; 5 import org.krysalis.barcode4j.tools.UnitConv; 6 7 import java.awt.image.BufferedImage; 8 import java.io.*; 9 10 /** 11 * @author :CX 12 * @Date :Create in 2018/9/3 11:20 13 * @Effect : 获取条形码 14 */ 15 public class BarCodeUtil { 16 17 /** 18 * 生成文件 19 * 20 * @param msg 21 * @param path 22 * @return 23 */ 24 public static File generateFile(String msg, String path) { 25 File file = new File(path); 26 try { 27 generate(msg, new FileOutputStream(file)); 28 } catch (FileNotFoundException e) { 29 throw new RuntimeException(e); 30 } 31 return file; 32 } 33 34 /** 35 * 生成字节 36 * 37 * @param msg 38 * @return 39 */ 40 private static byte[] generate(String msg) { 41 ByteArrayOutputStream ous = new ByteArrayOutputStream(); 42 generate(msg, ous); 43 return ous.toByteArray(); 44 } 45 46 /** 47 * 生成到流 48 * 49 * @param msg 50 * @param ous 51 */ 52 public static void generate(String msg, OutputStream ous) { 53 if (StringUtils.isEmpty(msg) || ous == null) { 54 return; 55 } 56 57 Code39Bean bean = new Code39Bean(); 58 59 // 精细度 60 final int dpi = 80; 61 // module宽度 62 final double moduleWidth = UnitConv.in2mm(2.0f / dpi); 63 64 // 配置对象 65 bean.setModuleWidth(moduleWidth); 66 bean.setWideFactor(3); 67 bean.doQuietZone(false); 68 69 String format = "image/png"; 70 try { 71 72 // 输出到流 73 BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi, 74 BufferedImage.TYPE_BYTE_BINARY, false, 0); 75 76 // 生成条形码 77 bean.generateBarcode(canvas, msg); 78 79 // 结束绘制 80 canvas.finish(); 81 } catch (IOException e) { 82 throw new RuntimeException(e); 83 } 84 } 85 86 /** 87 *@参数 88 *@返回值 89 *@创建人 cx 90 *@创建时间 91 *@描述 条形码的 64 位字符串 92 */ 93 public static String getBarCodeBase64Str(String orderNo) { 94 95 byte[] bytes = Base64.encodeBase64(generate(orderNo)); 96 String utf8 = null; 97 try { 98 utf8 = new String(bytes, "utf8"); 99 } catch (UnsupportedEncodingException e) { 100 e.printStackTrace(); 101 } 102 return utf8 ; 103 } 104 /** 105 *@参数 106 *@返回值 107 *@创建人 cx 108 *@创建时间 109 *@描述 file 转bate[] 110 */ 111 private static byte[] getBytes(File file){ 112 byte[] buffer = null; 113 try { 114 FileInputStream fis = new FileInputStream(file); 115 ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); 116 byte[] b = new byte[1000]; 117 int n; 118 while ((n = fis.read(b)) != -1) { 119 bos.write(b, 0, n); 120 } 121 fis.close(); 122 bos.close(); 123 buffer = bos.toByteArray(); 124 } catch (FileNotFoundException e) { 125 e.printStackTrace(); 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } 129 return buffer; 130 } 131 }
3) 页面展示
<img src="data:image/png;base64, 条形码的 BASE64编码字符串 "/>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
注意: 如果不需要将生成的图片文件保存到数据库的话在获取条码的时候获取到流之后转换成base64 字符串给前段展示就好了,不用获取文件!