Java生成条形码code128

前言

(51条消息) Java生成条形码code128_明明如月学长的博客-CSDN博客_code128bean怎么谁知两侧留白

 1 public class BarcodeUtil {
 2 
 3 
 4     public static File generateFile(String msg, String path) {
 5         File file = new File(path);
 6         try {
 7             generate(msg, new FileOutputStream(file));
 8         } catch (FileNotFoundException e) {
 9             throw new RuntimeException(e);
10         }
11         return file;
12     }
13 
14 
15     public static byte[] generate(String msg) {
16         ByteArrayOutputStream ous = new ByteArrayOutputStream();
17         generate(msg, ous);
18         return ous.toByteArray();
19     }
20     public static void generate(String msg, OutputStream ous) {
21         if (StringUtils.isEmpty(msg) || ous == null) {
22             return;
23         }
24 
25         Code128Bean bean = new Code128Bean();
26         final int dpi = 512;
27         final double moduleWidth = UnitConv.in2mm(2.0f / dpi);
28         bean.setModuleWidth(moduleWidth);
29         bean.setHeight(30);
30         bean.doQuietZone(false);
31         HumanReadablePlacement none = HumanReadablePlacement.byName("none");
32         bean.setMsgPosition(none);
33 
34         String format = "image/png";
35         try {
36 
37             BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
38                     BufferedImage.TYPE_BYTE_BINARY, false, 0);
39             bean.generateBarcode(canvas, msg);
40 
41             canvas.finish();
42         } catch (IOException e) {
43             throw new RuntimeException(e);
44         }
45     }
46 
47 
48 
49 }

 

posted @ 2023-01-30 15:29  chch213  阅读(324)  评论(0编辑  收藏  举报