Product barcode (JBarcode ),

Source: Internet
Author: User

Product barcode (JBarcode ),

I have never used this before. Now I use JBarcode to generate the product barcode. Preparations before work:

Eclipse:
Eclipse Java ee ide for Web Developers.
Version: Helios Service Release 1
Build id: 20100917-0705

Jar package:
JBarcode-Recognition_Source-0.2.jar
Jbarcode-0.2.8.jar
Commons-lang-2.6.jar

First understand the rules of EAN-13 code:

Then let's take a look at the arrangement of these numbers:

The 13-bit barcode split-point processing shows that these operations need to be processed and intercepted by yourself. You can understand the meaning of each segment expression of the barcode.

Knowing this is enough for us to do a barcode verification and generate our own barcode.

Let's get to know what the verification code is. Let's do it according to our own needs and then handle it as needed, that is, the barcode we want.

The verification code generation rules are as follows:

Note:Here, if the result of C is 0 or 10, the last check code is 0.

Are you more and more interested in JBarcode? The process is very simple.

The daughter-in-law's chocolate will arrive tomorrow. Come on and write code for the daughter-in-law's chocolate .,,,

 

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. wi DeRatioCodedPainter; import org. jbarcode. paint. widthCodedPainter; import org. jbarcode. util. imageUtil; /***** @ ============================================ ===============** @ author: liuyc * @ create: January 26, 2015 14:47:57 * @ update: * @ bolg: http://www.cnblogs.com/yuchuan/ * @ csdn: http://blog.csdn.net/l_lycos * @ E-mail: 763999883@qq.com * @ desc: ** @ ======================================================== ========= */public cl Ass BarCodeImage {/*** image type */public enum ImgType {/*** image format :. gif */GIF (". gif "),/*** image format :. png */PNG (". png "),/*** image format :. jpg */JPG (". jpg "),/*** image format :. jpeg */JPEG (". jpeg "),; ImgType (String value) {this. value = value;} private final String value; public String getValue () {return value ;}} /*** generate product barcode ** @ param filePath * product barcode image storage path :.. /xxx/yyy/* @ param jbarCode * product barcode: 8-digit, 13-digit * @ param format * operator Product barcode image format :. gif /. png /. jpg /. jpeg * @ return image storage path + image name + image file type */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, barCodeNam E);} 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 ;}/ *** generate JPEG image ** @ param paramBufferedImage * @ param paramString */@ SuppressWarnings ("unused ") private void saveToJPEG (BufferedImage paramBufferedImage, String filePath, String fileName) {saveToFile (paramBufferedImage, filePath, fileName, "jpeg ");} /*** generate a PNG Image ** @ param paramBufferedImage * @ param paramString */@ SuppressWarnings ("unused") private vo Id saveToPNG (BufferedImage paramBufferedImage, String filePath, String fileName) {saveToFile (paramBufferedImage, filePath, fileName, "png ");} /*** generate a GIF image ** @ param paramBufferedImage * @ param paramString */private void saveToGIF (BufferedImage failed, String filePath, String fileName) {saveToFile (paramBufferedImage, filePath, fileName, "gif");}/*** Save the image file ** @ param paramBufferedImage * Image stream * @ param filePath * file path * @ param imgName * image parameter * @ param imgFormat * image format */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 (filePat H); 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:" + loca LException); localException. printStackTrace () ;}}/*** return verification code ** @ param code * product barcode * @ return verification code:-1: Incorrect format, the barcode is all numbers-2: the parameter cannot be blank **/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; // sum of even digits and int oddsum = 0; // The sum of odd digits and 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 ){}}

 

 

 

Indicate the source and source address.Http://www.cnblogs.com/yuchuan/p/4250328.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.