求助:java编写的39条形码生成器,为什么无法运行?
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
import
import
import com.sun.image.codec.jpeg.ImageFormatException;
import
import java.util.Random;
/** *//**
* 符合BarCode 39规范的条码图像生成器
* @author ChenLiang & LiGuang
*/
public class BarCodeImage
{
private static final int rate = 3; //条码宽条与窄条宽度之比
private int m_nNarrowWidth; //窄条的宽度像素数
private int m_nImageHeight; //条码的高度像素数
// private boolean m_bRotato; //输出的图像是否需要先旋转
/** *//**
* 根据strCodes传入的字符串,生成符合BarCode 39规范的JPEG输出流;
* @param nNarrowWidth
* @param nImageHeight
*/
/** *//**
* 生成相应的Bar Code图像,格式以jpeg格式的输出流;
* @param strCodes 要生成条码的字符串,注意该字符串需要包含首尾的两个星号
* @param out 接结果的输出流
* @throws IOException
* @throws ImageFormatException
* @throws IOException
*/
public void getImage(String strCodes, String path) throws ImageFormatException, IOException
{
String fileBar= System.getProperty("file.separator");
File myPNG = new File(path);
OutputStream out = new FileOutputStream(myPNG);
if (null==strCodes || null==out || 0==strCodes.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.BLACK);
int startx = 0;
for (int i = 0; i < strCodes.length(); i++)
startx = drawOneChar(g, startx, strCodes.charAt(i));
g.setColor(Color.WHITE);
g.fillRect(0, 33, nImageWidth, 13);
g.setColor(Color.BLACK);
g.drawString(strCodes, nImageWidth/4, 45);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
while(true) //图片生成完毕后,向下运行
{
if(myPNG.exists())
{
//System.out.println("---------over");
break;
}
}
}
/** *//**
* 辅助方法,绘制一个字符
* @param g 画布
* @param x 起始位置
* @param ch 要绘制的字符
* @return 下一个要绘制字符的位置
*/
private int drawOneChar(Graphics g, int x, char ch)
{
short sCode = getCharCode(ch);
for (int i = 0; i < 9; i++)
{
int width = m_nNarrowWidth;
if (((0x100 >>> i) & sCode) != 0)
width *= rate;
if ((i & 0x1) == 0)
g.fillRect(x, 0, width, m_nImageHeight);
x += width;
}
return x + m_nNarrowWidth;
}
//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进制编码 n为0 w为1
private 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, 0x94, 0xa8, 0xa2,
0x8a, 0x2a };
/** *//**
* 通过码表符号获得码表字符
* @param ch 码表符号
* @return 码表值
*/
private static short getCharCode(char insert)
{
for (int i = 0; i < m_chars.length; i++)
{
if (ch == m_chars[i])
return m_codes[i];
}
return 0;
}
public char insert(String barcodeData){
char ch;
for(i=0;i<dataFinal.length;i++)
ch=barcodeData[i];
return ch;
}
//生成条形码的数据----8位随机字符串
public String barcodeData()
{
String dataFinal = null;
//String dataTemp = longDateFormat.format(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;
}
}
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
为什么总是说这两个包是sun的专用包,难道是这个原因吗?