大家都晓得每件商品几乎都有属于自己的条形码!最近在公司做一个项目里面需要用到条形码!经研究非常简单!
http://www.oschina.net/p/jbarcode
http://sourceforge.net/projects/jbcode/
本人亲手扫描,成功可用!
/* 2. * To change this template, choose Tools | Templates 3. * and open the template in the editor. 4. */ package com.zhilong.juyuansu.test; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import org.jbarcode.JBarcode; import org.jbarcode.encode.EAN8Encoder; import org.jbarcode.paint.EAN8TextPainter; import org.jbarcode.paint.WidthCodedPainter; import org.jbarcode.util.ImageUtil; /** * 2012-05-28 * @author 郏高阳 * 支持EAN13, EAN8, UPCA, UPCE, Code 3 of 9, Codabar, Code 11, Code 93, Code 128, MSI/Plessey, Interleaved 2 of PostNet等 * */ public class OneBarcodeUtil { public static void main(String[] paramArrayOfString) { try { JBarcode localJBarcode = new JBarcode(EAN8Encoder.getInstance(),WidthCodedPainter.getInstance(),EAN8TextPainter.getInstance()); String str = "2219644"; BufferedImage localBufferedImage = localJBarcode.createBarcode(str); saveToGIF(localBufferedImage, "EAN8.jpg"); } catch (Exception localException) { localException.printStackTrace(); } } static void saveToJPEG(BufferedImage paramBufferedImage, String paramString) { saveToFile(paramBufferedImage, paramString, "jpeg"); } static void saveToFile(BufferedImage paramBufferedImage, String paramString1, String paramString2) { try { FileOutputStream localFileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop/" + paramString1); ImageUtil.encodeAndWrite(paramBufferedImage, paramString2, localFileOutputStream, 96, 96); localFileOutputStream.close(); } catch (Exception localException) { localException.printStackTrace(); } } }
格式为:EAN8条形码!(EAN8格式为7位数组成,最后以为随机编号!拥有60%的市场)
/** * 等比例放大图象 * * @param imgUrl 图像路径 * @param resultImgUrl 放大后的存放路径 * @param scale 放大倍数 * @throws IOException */ static void scale(String imgUrl, String resultImgUrl, int scale) throws IOException { BufferedImage src = ImageIO.read(new File(imgUrl)); int width = src.getWidth(); int height = src.getHeight(); width = width * scale; height = height * scale; Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); ImageIO.write(tag, "JPEG", new File(resultImgUrl)); }
❤犒劳一杯咖啡❤