java条形码的生成与扫描

1部分代码来自网上,所需jar包:barbecue-1.5-beta1.jar,jbarcode-0.2.8.jar 2 3import java.awt.BorderLayout; 4import java.awt.Component; 5import java.awt.event.KeyAdapter; 6import java.awt.event.KeyEvent; 7import java.awt.event.WindowAdapter; 8import java.awt.event.WindowEvent; 9import java.awt.image.BufferedImage; 10import java.io.FileOutputStream; 11 12import javax.swing.JFrame; 13 14import org.jbarcode.JBarcode; 15import org.jbarcode.encode.Code39Encoder; 16import org.jbarcode.encode.EAN13Encoder; 17import org.jbarcode.paint.BaseLineTextPainter; 18import org.jbarcode.paint.EAN13TextPainter; 19import org.jbarcode.paint.WideRatioCodedPainter; 20import org.jbarcode.paint.WidthCodedPainter; 21import org.jbarcode.util.ImageUtil; 22 23import net.sourceforge.barbecue.Barcode; 24import net.sourceforge.barbecue.BarcodeException; 25import net.sourceforge.barbecue.BarcodeFactory; 26 27/** 28 * @description 针对条形码的帮助类 29 * @使用方法 类名调用 30 * @方法一:getNumStringByBarcode() 扫描条形码得到对应的数字字符串 31 * @方法二:(可以不用,方法三取代)getBarcodeByNumString(String numStr) 通过一串数字字符串得到条形码(swing显示) 32 * @author 33 * @区分键盘输入还是扫描枪输入 思路:扫描输入的间隔比较均匀且时间很短(小于50毫秒), 而手动输入间隔至少在100毫秒以上。 34 * @方法三:createBarcodePictureByString(String numStr,String path) 35 */ 36public class BarcodeUtils { 37 38 static long timeMillis1; 39 static long timeMillis2; 40 41 // 求间隔时间时用到的标志位,每进来一次keyPressed方法,把flag置反,并且记录当前系统的时间毫秒值前后相减,得到间隔时间 42 static boolean IntervalFlag = false; 43 44 static int count = 0;// 进入一次keyPressed()方法加1,第一次进入是不算时间间隔,count=2时开始算,另外可以用来判断是否是连续多次输入 45 static StringBuilder sb = null; 46 static String str = ""; 47 static long timeInterval = 0; 48 static String str2 = ""; 49 50 static boolean scanningGunFlag = false; // 是否是 扫描枪输入 51 static boolean kpressedFlag3 = false;// 是否有键盘输入 52 static boolean keyboardAndScanFlag4 = false;// 判断是不是 先有键盘输入 再有扫描输入 53 54 /** 55 * 56 * @param numStr 条形码字符串 57 * @param path 图片存储路径,例如"f:/" 58 */ 59 public static void createBarcodePictureByString(String numStr,String path) { 60 try { 61 JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(), 62 WidthCodedPainter.getInstance(), 63 EAN13TextPainter.getInstance()); 64 //BufferedImage localBufferedImage = localJBarcode.createBarcode(numStr); 65 //saveToGIF(localBufferedImage, "aaa.gif"); 66 67 localJBarcode.setEncoder(Code39Encoder.getInstance()); 68 localJBarcode.setPainter(WideRatioCodedPainter.getInstance()); 69 localJBarcode.setTextPainter(BaseLineTextPainter.getInstance()); 70 localJBarcode.setShowCheckDigit(false); //xx str = "JBARCODE-39"; 71 BufferedImage localBufferedImage = localJBarcode.createBarcode(numStr); 72 saveToPNG(localBufferedImage, numStr+".png",path); 73 74 } catch (Exception localException) { 75 localException.printStackTrace(); 76 } 77 } 78 79 /** 80 * 此方法的作用是更具条形码字符串生成一个条形码(swing界面显示出来) 81 * 82 * @param numStr 83 * 条形码数字对象的字符串 84 */ 85 @SuppressWarnings("static-access") 86 public static void getBarcodeByNumString(String numStr) { 87 JFrame frame = new JFrame("getBarcodeByNumString"); 88 BarcodeUtils u = new BarcodeUtils(); 89 Component contents = u.usingBarbecueAsSwingComponent(numStr); 90 frame.getContentPane().add(contents, BorderLayout.CENTER); 91 frame.addWindowListener(new WindowAdapter() { 92 public void windowClosing(WindowEvent e) { 93 System.exit(0); 94 } 95 }); 96 frame.pack(); 97 frame.setVisible(true); 98 } 99 100 /** 101 * 扫描条形码的到数字字符串 102 * 103 * @return 条形码对应的数字字符串 104 */ 105 public static String getNumStringByBarcode() { 106 107 JFrame frame = new JFrame("getNumStringByBarcode"); 108 frame.addWindowListener(new WindowAdapter() { 109 public void windowClosing(WindowEvent e) { 110 System.exit(0); 111 } 112 }); 113 114 frame.addKeyListener(new KeyAdapter() { 115 116 public void keyPressed(KeyEvent e) { 117 count++; 118 if (IntervalFlag == false) { 119 timeMillis1 = System.currentTimeMillis(); 120 // System.out.println(timeMillis1+":t1"); 121 IntervalFlag = true; 122 } else { 123 timeMillis2 = System.currentTimeMillis(); 124 // System.out.println(timeMillis2+":t2"); 125 IntervalFlag = false; 126 } 127 if (count > 1) { 128 timeInterval = Math.abs(timeMillis2 - timeMillis1); 129 } 130 // System.out.print(timeInterval+"---"); 131 if (timeInterval < 50) { 132 count++; 133 if (sb == null) 134 sb = new StringBuilder(); 135 if (e.getKeyCode() >= KeyEvent.VK_0 136 && e.getKeyCode() <= KeyEvent.VK_9) { 137 // System.out.println(e.getKeyCode()+":"+e.getKeyChar()); 138 sb.append(e.getKeyChar()); 139 kpressedFlag3 = true; 140 } 141 if (e.getKeyCode() == KeyEvent.VK_ENTER) { 142 if (scanningGunFlag == true 143 || keyboardAndScanFlag4 == true) { 144 str = str2 + sb.toString(); 145 str2 = ""; 146 } else { 147 str = sb.toString(); 148 } 149 sb = null; 150 if (str.length() >= 8) { 151 System.out.println("条形码:" + str); 152 153 } else { 154 str = ""; 155 } 156 if (count >= 8) { 157 scanningGunFlag = true; 158 count = 0; 159 } 160 } 161 // System.out.println("str-:"+str); 162 if (kpressedFlag3 == true && scanningGunFlag == false) { 163 // 首先是键盘输入再有扫描输入 164 keyboardAndScanFlag4 = true; 165 } 166 } else { 167 if (e.getKeyCode() >= KeyEvent.VK_0 168 && e.getKeyCode() <= KeyEvent.VK_9 169 && scanningGunFlag == true) { 170 // 连续扫描 171 str2 = "" + e.getKeyChar(); 172 } 173 if (e.getKeyCode() >= KeyEvent.VK_0 174 && e.getKeyCode() <= KeyEvent.VK_9 175 && keyboardAndScanFlag4 == true) { 176 // 先按下键盘再扫描 177 str2 = "" + e.getKeyChar(); 178 // System.out.println("s--:"+str2); 179 scanningGunFlag = false; 180 } 181 sb = null; 182 } 183 184 } 185 186 }); 187 188 frame.pack(); 189 frame.setVisible(true); 190 return str; 191 192 } 193 194 /** 195 * @param num 196 * @return Component 197 */ 198 private static Component usingBarbecueAsSwingComponent(String num) { 199 Barcode barcode = null; 200 try { 201 barcode = BarcodeFactory.createCode128B(num); 202 203 barcode.setBarHeight(50); 204 barcode.setBarWidth(1); 205 } catch (BarcodeException e) { 206 } 207 return barcode; 208 } 209 210 //////一下方法为createBarcodePictureByString()需要用到的方法--// 211 @SuppressWarnings("unused") 212 private static void saveToJPEG(BufferedImage paramBufferedImage, 213 String paramString,String path) { 214 saveToFile(paramBufferedImage, paramString, "jpeg",path); 215 } 216 217 private static void saveToPNG(BufferedImage paramBufferedImage, 218 String paramString,String path) { 219 saveToFile(paramBufferedImage, paramString, "png",path); 220 } 221 222 @SuppressWarnings("unused") 223 private static void saveToGIF(BufferedImage paramBufferedImage, 224 String paramString,String path) { 225 saveToFile(paramBufferedImage, paramString, "gif",path); 226 } 227 228 private static void saveToFile(BufferedImage paramBufferedImage, 229 String paramString1, String paramString2,String path) { 230 try { 231 FileOutputStream localFileOutputStream = new FileOutputStream("f:/" 232 + paramString1); 233 ImageUtil.encodeAndWrite(paramBufferedImage, paramString2, 234 localFileOutputStream, 96, 96); 235 localFileOutputStream.close(); 236 } catch (Exception localException) { 237 localException.printStackTrace(); 238 } 239 } 240 //------///// 241}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

swap空间的增减方法

(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap

java条形码的生成与扫描 - HelloWorld