java條形碼的生成與掃描

部分代碼來自網上,所需jar包: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.WindowEvent;
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 org.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 針對條形碼的幫助類
 * @使用方法 類名調用
 * @方法一:getNumStringByBarcode() 掃描條形碼獲得對應的數字字符串
 * @方法二:(能夠不用,方法三取代)getBarcodeByNumString(String numStr) 經過一串數字字符串獲得條形碼(swing顯示)
 * @author 
 * @區分鍵盤輸入仍是掃描槍輸入 思路:掃描輸入的間隔比較均勻且時間很短(小於50毫秒), 而手動輸入間隔至少在100毫秒以上。
 * @方法三:createBarcodePictureByString(String numStr,String path)
 */
public class BarcodeUtils {

	static long timeMillis1;
	static long timeMillis2;

	// 求間隔時間時用到的標誌位,每進來一次keyPressed方法,把flag置反,而且記錄當前系統的時間毫秒值先後相減,獲得間隔時間
	static boolean IntervalFlag = false;

	static int count = 0;// 進入一次keyPressed()方法加1,第一次進入是不算時間間隔,count=2時開始算,另外能夠用來判斷是不是連續屢次輸入
	static StringBuilder sb = null;
	static String str = "";
	static long timeInterval = 0;
	static String str2 = "";
							
	static boolean scanningGunFlag = false; // 是不是 掃描槍輸入
	static boolean kpressedFlag3 = false;// 是否有鍵盤輸入
	static boolean keyboardAndScanFlag4 = false;// 判斷是否是 先有鍵盤輸入 再有掃描輸入

	/**
	 * 
	 * @param numStr 條形碼字符串
	 * @param path 圖片存儲路徑,例如"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();
		}
	}

	/**
	 * 此方法的做用是更具條形碼字符串生成一個條形碼(swing界面顯示出來)
	 * 
	 * @param numStr
	 *            條形碼數字對象的字符串
	 */
	@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(WindowEvent e) {
				System.exit(0);
			}
		});
		frame.pack();
		frame.setVisible(true);
	}

	/**
	 * 掃描條形碼的到數字字符串
	 * 
	 * @return 條形碼對應的數字字符串
	 */
	public static String getNumStringByBarcode() {

		JFrame frame = new JFrame("getNumStringByBarcode");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent 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("條形碼:" + str);
							
						} else {
							str = "";
						}
						if (count >= 8) {
							scanningGunFlag = true;
							count = 0;
						}
					}
					// System.out.println("str-:"+str);
					if (kpressedFlag3 == true && scanningGunFlag == false) {
						// 首先是鍵盤輸入再有掃描輸入
						keyboardAndScanFlag4 = true;
					}
				} else {
					if (e.getKeyCode() >= KeyEvent.VK_0
							&& e.getKeyCode() <= KeyEvent.VK_9
							&& scanningGunFlag == true) {
						// 連續掃描
						str2 = "" + e.getKeyChar();
					}
					if (e.getKeyCode() >= KeyEvent.VK_0
							&& e.getKeyCode() <= KeyEvent.VK_9
							&& keyboardAndScanFlag4 == true) {
						// 先按下鍵盤再掃描
						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 barcode = null;
		try {
			barcode = BarcodeFactory.createCode128B(num);

			barcode.setBarHeight(50);
			barcode.setBarWidth(1);
		} catch (BarcodeException e) {
		}
		return barcode;
	}

	//////一下方法爲createBarcodePictureByString()須要用到的方法--//
	@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(paramBufferedImage, paramString, "gif",path);
	}

	private static void saveToFile(BufferedImage paramBufferedImage,
			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();
		}
	}
	//------/////
}
相關文章
相關標籤/搜索