JAVA生成條形碼


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 字符串給前段展示就好了,不用獲取文件!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM