1、package test;import java.awt.image.BufferedImage;import java.awt.Graphics;import java.awt.Color;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import com.sun.image.codec.jpeg.JPEGImageEncoder;import com.sun.image.codec.jpeg.JPEGCodec;import java.io.File;import java.io.F
2、ileOutputStream;import java.io.OutputStream;import com.sun.image.codec.jpeg.ImageFormatException;import java.io.IOException;import java.util.Random;/* */* 符合 BarCode 39 规范的条码图像生成器* author ChenLiang /条码宽条与窄条宽度之比private int m_nNarrowWidth; /窄条的宽度像素数private int m_nImageHeight; /条码的高度像素数private boolean
3、m_bRotato; /输出的图像是否需要先旋转/* */* 根据 strCodes 传入的字符串,生成符合 BarCode 39 规范的 JPEG 输出流;* param nNarrowWidth * param nImageHeight */* public BarCode39ImageBuilder(int nNarrowWidth, int nImageHeight)m_nNarrowWidth = nNarrowWidth;m_nImageHeight = nImageHeight;m_bRotato = false;*/* */* 构造函数,默认窄条宽为 4 像素,条码高度是 10
4、0 像素;*/*public BarCode39ImageBuilder()this(1, 33);*/* */* 设置是否需要将结果图像在输出之前进行旋转;系统默认是不旋转* param b true:旋转,false:不旋转*/public void setRotato(boolean b)m_bRotato = b;/* */* 生成相应的 Bar Code 图像,格式以 jpeg 格式的输出流;* param strCodes 要生成条码的字符串,注意该字符串需要包含首尾的两个星号* param out 接结果的输出流* throws IOException * throws Imag
5、eFormatException * throws IOException*/public void getImage(String strCodes, String path) throws ImageFormatException, IOExceptionString fileBar= System.getProperty(“file.separator“);File myPNG = new File(path);OutputStream out = new FileOutputStream(myPNG);if (null=strCodes | null=out | 0=strCodes.
6、length()return;int nImageWidth = (strCodes.length() * (3 * rate + 7) * m_nNarrowWidth);BufferedImage bi = new BufferedImage(nImageWidth, m_nImageHeight+13, BufferedImage.TYPE_INT_RGB);Graphics g = bi.getGraphics();g.setColor(Color.WHITE);g.fillRect(0, 0, nImageWidth, m_nImageHeight);g.setColor(Color
7、.BLACK);int startx = 0;for (int i = 0; i i) if (i x += width;return x + m_nNarrowWidth;/* */* 图像翻转,即 X 与 Y 方向互换* param in 源图像* return 翻转后的图像*/private BufferedImage flipX2Y(BufferedImage in)BufferedImage out = new BufferedImage(in.getHeight(), in.getWidth(), in.getType();/请查看 JDKAffineTransform affin
8、eTransform = new AffineTransform(0, 1, 1, 0, 0, 0);AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);return affineTransformOp.filter(in, out);/ 原来旋转的版本,仅供参考/ private BufferedImage rotate(BufferedImage in)/ / int width = in.getWidth()
9、;/ int height = in.getHeight();/ BufferedImage out = new BufferedImage(height, width, in.getType();/ AffineTransform affineTransform = AffineTransform.getRotateInstance(Math.toRadians(90);/ affineTransform.translate(0, -height);/ / AffineTransformOp affineTransformOp = new AffineTransformOp(affineTr
10、ansform,/ AffineTransformOp.TYPE_NEAREST_NEIGHBOR);/ affineTransformOp.filter(in, out);/ return out;/ /Code39 符号表private static final char m_chars = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D,E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X,Y, Z, -, ., , *, $, /, +, % ;/Code39 符号所对应的码表,16 进制
11、编码 n 为 0 w 为 1private static final short m_codes = 0x34, 0x121, 0x61, 0x160, 0x31, 0x130, 0x70, 0x25, 0x124, 0x64,0x109, 0x49, 0x148, 0x19, 0x118, 0x58, 0xd, 0x10c, 0x4c, 0x1c, 0x103, 0x43, 0x142, 0x13, 0x112, 0x52,0x7, 0x106, 0x46, 0x16, 0x181, 0xc1, 0x1c0, 0x91, 0x190, 0xd0, 0x85, 0x184, 0xc4, 0x9
12、4, 0xa8, 0xa2,0x8a, 0x2a ;/* */* 通过码表符号获得码表字符* param ch 码表符号* return 码表值*/private static short getCharCode(char ch)for (int i = 0; i m_chars.length; i+)if (ch = m_charsi)return m_codesi;return 0;/生成条形码的数据-8 位随机字符串public String barcodeData()String dataFinal = null;/String dataTemp = longDateFormat.fo
13、rmat(new java.util.Date(); Random r = new Random();String strRandom = “+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“;dataFinal = strRandom;/dataFinal = dataTemp+strRandom;/*while(hasCus(dataFinal) /判断是否有此客户代码strRandom=“+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“+r.nextInt(9)+“;dataFinal = dataTemp+strRandom; */return dataFinal;