Part of the Code comes from the Internet, the jar package required: barbecue-1.5-beta1.jar, jbarcode-0.2.8.jar
Import Java. AWT. borderlayout; import Java. AWT. component; import Java. AWT. event. keyadapter; import Java. AWT. event. keyevent; import Java. AWT. event. windowadapter; import Java. AWT. event. using wevent; import Java. AWT. image. bufferedimage; import Java. io. fileoutputstream; import javax. swing. jframe; import Org. jbarcode. jbarcode; import Org. jbarcode. encode. code39encoder; import Org. jbarcode. encode. ean13encoder; import o RG. jbarcode. paint. baselinetextpainter; import Org. jbarcode. paint. ean13textpainter; import Org. jbarcode. paint. wideratiocodedpainter; import Org. jbarcode. paint. widthcodedpainter; import Org. jbarcode. util. imageutil; import net. sourceForge. barbecue. barcode; import net. sourceForge. barbecue. barcodeexception; import net. sourceForge. barbecue. barcodefactory;/*** @ description help class for bar code * @ call using the method class name * @ Method 1: getnums Tringbybarcode () scan the barcode to obtain the corresponding numeric string * @ Method 2: (No, method 3) getbarcodebynumstring (string numstr) Get the barcode through a string of numeric strings (swing display) * @ author * @ differentiate between keyboard input and scanner input: The Scanning Input interval is relatively uniform and the time is short (less than 50 milliseconds), while the manual input interval is at least 100 milliseconds. * @ Method 3: createbarcodepicturebystring (string numstr, string path) */public class barcodeutils {static long timemillis1; static long timemillis2; // enter keypressed () the system time in milliseconds of the method // The identifier used to evaluate the interval. Each time the keypressed method is used, the flag is reversed and the time before and after the current system time is recorded, get the interval static Boolean intervalflag = false; static int COUNT = 0; // enter the keypressed () method once and Add 1. The first entry is not counted as the interval, and count = 2, in addition, it can be used to determine whether the input is static stringbuilder sb = NULL multiple times in a row; // It is placed into the input number static string STR = ""; // store the static long timeinterval = 0; static string str2 = ""; // use static Boolean scanninggunflag = false to distinguish between keyboard input-scan input, scan input-keyboard input, and scan input-scan input; // whether the scanner inputs static Boolean kpressedflag3 = false; // whether the keyboard inputs static Boolean keyboardandscanflag4 = false; // determine if there is a keyboard input first and then scan the input/***** @ Param numstr barcode string * @ Param path image storage path, for example, "F: /"*/public static void createbarcodepicturebystring (string numstr, string path) {try {jbarcode localjbarcode = new jbarcode (ean13encoder. getinstance (), widthcodedpainter. getinstance (), ean13textpainter. getinstance (); // bufferedimage localbufferedimage = localjbarcode. createbarcode (numstr); // savetogif (localbufferedimage, "aaa.gif"); localjbarcode. setencoder (code39encoder. getinstance (); localjbarcode. setpainter (wideratiocodedpainter. getinstance (); localjbarcode. settextpainter (baselinetextpainter. getinstance (); localjbarcode. setshowcheckdigit (false); // xx str = "JBARCODE-39"; bufferedimage localbufferedimage = localjbarcode. createbarcode (numstr); savetopng (localbufferedimage, numstr + ". PNG ", PATH);} catch (exception localexception) {localexception. printstacktrace () ;}}/*** this method is used to generate a bar code (displayed on the swing Interface) with a higher bar code string) ** @ Param numstr * string of the bar code numeric object */@ suppresswarnings ("static-access") public static void getbarcodebynumstring (string numstr) {jframe frame = new jframe ("getbarcodebynumstring"); barcodeutils u = new barcodeutils (); component contents = u. usingbarbecueasswingcomponent (numstr); frame. getcontentpane (). add (contents, borderlayout. center); frame. addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {system. exit (0) ;}}); frame. pack (); frame. setvisible (true);}/*** scan the barcode to the numeric string ** @ return the numeric string corresponding to the barcode */public static string getnumstringbybarcode () {jframe frame = new jframe ("getnumstringbybarcode"); frame. addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {system. exit (0) ;}}); frame. addkeylistener (New keyadapter () {public void keypressed (keyevent e) {count ++; If (intervalflag = false) {timemillis1 = system. currenttimemillis (); // system. out. println (timemillis1 + ": T1"); intervalflag = true;} else {timemillis2 = system. currenttimemillis (); // system. out. println (timemillis2 + ": t2"); intervalflag = false;} If (count> 1) {timeinterval = math. ABS (timemillis2-timemillis1);} // system. out. print (timeinterval + "---"); If (timeinterval <50) {count ++; If (SB = NULL) Sb = new stringbuilder (); If (E. getkeycode ()> = keyevent. vk_0 & E. getkeycode () <= keyevent. vk_9) {// system. out. println (E. getkeycode () + ":" + E. getkeychar (); sb. append (E. getkeychar (); kpressedflag3 = true;} If (E. getkeycode () = keyevent. vk_enter) {If (scanninggunflag = true | keyboardandscanflag4 = true) {STR = str2 + sb. tostring (); str2 = "";} else {STR = sb. tostring () ;}sb = NULL; If (Str. length ()> = 8) {system. out. println ("barcode:" + Str); // write the barcode to the XML string Path = dom4jxml. class. getresource (""). tostring () + "barcodeinfo. XML "; Path = path. substring (6, path. length (); dom4jxml. createbarcodexml (path, STR, system. currenttimemillis () + "");} else {STR = "";} If (count> = 8) {// continuously input more than once and after entering, scan the input scanninggunflag = true; Count = 0 ;}} // system. out. println ("str-:" + Str); If (kpressedflag3 = true & scanninggunflag = false) {// enter the keyboard and then scan the input keyboardandscanflag4 = true ;}} else {If (E. getkeycode ()> = keyevent. vk_0 & E. getkeycode () <= keyevent. vk_9 & scanninggunflag = true) {// scan str2 = "" + E. getkeychar ();} If (E. getkeycode ()> = keyevent. vk_0 & E. getkeycode () <= keyevent. vk_9 & keyboardandscanflag4 = true) {// press the keyboard and scan str2 = "" + E. getkeychar (); // system. out. println ("s --:" + str2); scanninggunflag = false;} sb = NULL ;}}); frame. pack (); frame. setvisible (true); Return STR;}/*** @ Param num * @ return component */Private Static component usingbarbecueasswingcomponent (string num) {barcode = NULL; try {barcode = barcodefactory. createcode128b (Num); barcode. setbarheight (50); barcode. setbarwidth (1);} catch (barcodeexception e) {} return barcode;} // The following method is createbarcodepicturebystring () required methods -- // @ suppresswarnings ("UNUSED") Private Static void savetojpeg (bufferedimage parambufferedimage, string paramstring, string path) {savetofile (parambufferedimage, paramstring, "Jpeg ", path);} Private Static void savetopng (bufferedimage parambufferedimage, string paramstring, string path) {savetofile (parambufferedimage, paramstring, "PNG", PATH) ;}@ suppresswarnings ("UNUSED ") private Static void savetogif (bufferedimage parambufferedimage, string paramstring, string path) {savetofile (Region, paramstring, "GIF", PATH);} Private Static void savetofile (bufferedimage region, string paramstring1, string paramstring2, string path) {try {fileoutputstream localfileoutputstream = new fileoutputstream ("F:/" + paramstring1); imageutil. encodeandwrite (parambufferedimage, paramstring2, localfileoutputstream, 96, 96); localfileoutputstream. close ();} catch (exception localexception) {localexception. printstacktrace ();}}//------/////}