之前沒有使用過這個,現在使用JBarcode生成商品條形碼,工作之前的准備工作:

Eclipse:
Eclipse Java EE IDE for Web Developers.
Version: Helios Service Release 1
Build id: 20100917-0705
jar包:
JBarcode-Recognition_Source-0.2.jar
jbarcode-0.2.8.jar
commons-lang-2.6.jar
首先了解EAN-13碼的規則:

然后大家去了解一下這些數字的排列:

13位條形碼分位處理就看出來,這些都需要自己加工處理並做截取處理,可以了解條形碼每個段位表達的意思。
知道這些就已經足夠我們去做一個條形碼的校驗工作以及生成自己的條形碼。

了解校驗碼是怎么回事,我們根據我們自己的需求去做,然后根據需求處理一下,就是我們想要的條形碼。
校驗碼生成規則如下:

注意:這里的校驗碼,如果減掉后的C的結果為0或者10,那么最后一位的校驗碼就是0
現在是不是對JBarcode越來越感興趣了呢,流程是很簡單的。
明天小媳婦的巧克力就到了,加油寫代碼為了小媳婦的巧克力。,,,
package com.liuyc.test.demo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;
/**
*
* @=============================================
*
* @author : Liuyc
* @create : 2015年1月26日 14:47:57
* @update :
* @bolg : http://www.cnblogs.com/yuchuan/
* @csdn : http://blog.csdn.net/l_lycos
* @E-mail : 763999883@qq.com
* @desc :
*
* @=============================================
*/
public class BarCodeImage {
/**
* 圖片類型
*/
public enum ImgType {
/**
* 圖片格式:.gif
*/
GIF(".gif"),
/**
* 圖片格式:.png
*/
PNG(".png"),
/**
* 圖片格式:.jpg
*/
JPG(".jpg"),
/**
* 圖片格式:.jpeg
*/
JPEG(".jpeg"), ;
ImgType(String value) {
this.value = value;
}
private final String value;
public String getValue() {
return value;
}
}
/**
* 生成商品條形碼
*
* @param filePath
* 商品條形碼圖片存放路徑:../xxx/yyy/
* @param jbarCode
* 商品條形碼:8位、13位
* @param format
* 商品條形碼圖片格式:.gif/.png/.jpg/.jpeg
* @return 圖片存放路徑+圖片名稱+圖片文件類型
*/
public String createBarCode(String filePath, String jbarCode, String format) {
String barCodeName = jbarCode + format;
try {
BufferedImage bi = null;
int len = jbarCode.length();
String barCode = jbarCode;
if (len == 12) {
} else if (len == 13) {
int backCode = checkCode(jbarCode);
int oldCode = Integer
.parseInt(jbarCode.substring(len - 1, len));
if (oldCode != backCode) {
return null;
}
barCode = jbarCode.substring(0, jbarCode.length() - 1);
}
JBarcode localJBarcode13 = new JBarcode(EAN13Encoder.getInstance(),
WidthCodedPainter.getInstance(),
EAN13TextPainter.getInstance());
bi = localJBarcode13.createBarcode(barCode);
if (ImgType.GIF.value.equals(format)) {
saveToGIF(bi, filePath, barCodeName);
} else if (ImgType.PNG.value.equals(format)) {
saveToPNG(bi, filePath, barCodeName);
} else if (ImgType.JPG.value.equals(format) || ImgType.JPEG.value.equals(format)) {
saveToJPEG(bi, filePath, barCodeName);
}
localJBarcode13.setEncoder(EAN13Encoder.getInstance());
localJBarcode13.setPainter(WideRatioCodedPainter.getInstance());
localJBarcode13.setTextPainter(EAN13TextPainter.getInstance());
localJBarcode13.setShowCheckDigit(false);
return filePath + barCodeName;
} catch (Exception localException) {
localException.printStackTrace();
return null;
}
}
/**
* 生成JPEG圖片
*
* @param paramBufferedImage
* @param paramString
*/
@SuppressWarnings("unused")
private void saveToJPEG(BufferedImage paramBufferedImage, String filePath,
String fileName) {
saveToFile(paramBufferedImage, filePath, fileName, "jpeg");
}
/**
* 生成PNG圖片
*
* @param paramBufferedImage
* @param paramString
*/
@SuppressWarnings("unused")
private void saveToPNG(BufferedImage paramBufferedImage, String filePath,
String fileName) {
saveToFile(paramBufferedImage, filePath, fileName, "png");
}
/**
* 生成GIF圖片
*
* @param paramBufferedImage
* @param paramString
*/
private void saveToGIF(BufferedImage paramBufferedImage, String filePath,
String fileName) {
saveToFile(paramBufferedImage, filePath, fileName, "gif");
}
/**
* 保存圖片文件
*
* @param paramBufferedImage
* 圖片流
* @param filePath
* 文件路徑
* @param imgName
* 圖片參數
* @param imgFormat
* 圖片格式
*/
private void saveToFile(BufferedImage paramBufferedImage, String filePath,
String imgName, String imgFormat) {
try {
FileOutputStream fileOutputStream = null;
try {
// 如果是本地測試,請自行修改為一個圖片存放地址
String rootPath = this.getClass().getClassLoader() .getResource("/").getPath();
String imgDir = StringUtils
.substringBefore(rootPath, "WEB-INF").concat(filePath);
String dirPath = "";
try {
dirPath = URLDecoder.decode(imgDir, "UTF-8");
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
String imgPath = dirPath + "/" + imgName;
fileOutputStream = new FileOutputStream(imgPath);
} catch (Exception e) {
System.out.println("Create Img File Error:" + e.toString());
}
ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat,
fileOutputStream, 96, 96);
fileOutputStream.close();
} catch (Exception localException) {
System.out.println("Save Img File Error:" + localException);
localException.printStackTrace();
}
}
/**
* 返回校驗碼
*
* @param code
* 商品條形碼
* @return 校驗碼: -1:格式不正確,條形碼為全部數字 -2:參數不能為空
*
*/
private int checkCode(String code) {
int checkCode = -1;
if (code == null || "".equals(code)) {
return -2;
} else if (!Pattern.compile("^[0-9]*$").matcher(code).matches()) {
checkCode = -1;
} else {
try {
int evensum = 0; // 偶數位的和
int oddsum = 0; // 奇數位的和
evensum += Integer.parseInt(code.substring(11, 12));
evensum += Integer.parseInt(code.substring(9, 10));
evensum += Integer.parseInt(code.substring(7, 8));
evensum += Integer.parseInt(code.substring(5, 6));
evensum += Integer.parseInt(code.substring(3, 4));
evensum += Integer.parseInt(code.substring(1, 2));
evensum *= 3;
oddsum += Integer.parseInt(code.substring(10, 11));
oddsum += Integer.parseInt(code.substring(8, 9));
oddsum += Integer.parseInt(code.substring(6, 7));
oddsum += Integer.parseInt(code.substring(4, 5));
oddsum += Integer.parseInt(code.substring(2, 3));
oddsum += Integer.parseInt(code.substring(0, 1));
int sum = evensum + oddsum;
int ck = 0;
if (sum % 10 == 0) {
ck = sum;
} else {
ck = (sum / 10 + 1) * 10;
}
checkCode = ck - sum;
} catch (NumberFormatException e) {
System.out.println("BarCode Format Error:" + e.toString());
} catch (Exception e) {
System.out.println("Get Check Code Error:" + e.toString());
}
}
return checkCode;
}
/**
* @param args
*/
public static void main(String[] args) {
}
}



載請標明出處,出處地址
http://www.cnblogs.com/yuchuan/p/4250328.html
