国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

商品條形碼(JBarcode)

2019-11-14 22:43:06
字體:
供稿:網(wǎng)友
商品條形碼(JBarcode)

之前沒有使用過這個,現(xiàn)在使用JBarcode生成商品條形碼,工作之前的準(zhǔn)備工作:

Eclipse:Eclipse java EE IDE for Web Developers.Version: Helios Service Release 1Build id: 20100917-0705

jar包:JBarcode-Recognition_Source-0.2.jarjbarcode-0.2.8.jarcommons-lang-2.6.jar

首先了解EAN-13碼的規(guī)則:

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

13位條形碼分位處理就看出來,這些都需要自己加工處理并做截取處理,可以了解條形碼每個段位表達(dá)的意思。

知道這些就已經(jīng)足夠我們?nèi)プ鲆粋€條形碼的校驗(yàn)工作以及生成自己的條形碼。

了解校驗(yàn)碼是怎么回事,我們根據(jù)我們自己的需求去做,然后根據(jù)需求處理一下,就是我們想要的條形碼。

校驗(yàn)碼生成規(guī)則如下:

注意:這里的校驗(yàn)碼,如果減掉后的C的結(jié)果為0或者10,那么最后一位的校驗(yàn)碼就是0

現(xiàn)在是不是對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://m.survivalescaperooms.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.equals(format)) {saveToGIF(bi, filePath, barCodeName);} else if (ImgType.PNG.equals(format)) {saveToPNG(bi, filePath, barCodeName);} else if (ImgType.JPG.equals(format) || ImgType.JPEG.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 *            圖片參數(shù) * @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();}}/** * 返回校驗(yàn)碼 *  * @param code *            商品條形碼 * @return 校驗(yàn)碼: -1:格式不正確,條形碼為全部數(shù)字 -2:參數(shù)不能為空 *  */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; // 偶數(shù)位的和int oddsum = 0; // 奇數(shù)位的和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) {}}

載請標(biāo)明出處,出處地址 http://m.survivalescaperooms.com/yuchuan/p/4250328.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 潞城市| 罗源县| 保山市| 武川县| 盱眙县| 台中市| 柳林县| 诸城市| 屏南县| 武川县| 霍林郭勒市| 浦江县| 南雄市| 金山区| 台南市| 锡林郭勒盟| 和顺县| 垫江县| 玉田县| 宁津县| 准格尔旗| 镇原县| 平遥县| 同德县| 杂多县| 巴中市| 青河县| 五台县| 江陵县| 芦溪县| 牡丹江市| 绥滨县| 称多县| 通化县| 荣昌县| 体育| 武义县| 同江市| 武义县| 永和县| 镇宁|