博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]用iText导出条形码图片(包含条形码)
阅读量:4172 次
发布时间:2019-05-26

本文共 2391 字,大约阅读时间需要 7 分钟。

private static final int HEIGHT_SPACE = 20;// 图片之间的间隔
public static void main(String[] args) throws Exception {
List<String> codeList = new ArrayList<String>();
codeList.add("ABCD124645765");
codeList.add("ABCD12-4645-765");
codeList.add("ABCD12464-5765");
codeList.add("AB-CD1-246457-65");
createBarcodeImage(200, 100, codeList);
System.out.println("The image is created.");
}
/**
* Creates the barcode image.
*
* @param barCodeWidth
* 生成条形码的宽度
* @param barCodeHeight
* 生成条形码的高度
* @param codeList
* 要生成条形码的字符集合
*
* @throws Exception
* the exception
*/
public static void createBarcodeImage(int barCodeWidth, int barCodeHeight,
List<String> codeList) throws Exception {
// list不能为空
Assert.assertTrue("The list can not empty.", codeList.size() > 0);
// 图片宽度
int imageWidth = (barCodeWidth + barCodeWidth / 2) * codeList.size() + barCodeWidth / 2;
// 图片高度
int imageHeight = barCodeHeight + HEIGHT_SPACE * 2;
BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) img.getGraphics();
g.fillRect(0, 0, imageWidth, imageHeight);
Font font = new java.awt.Font("", java.awt.Font.PLAIN, 12);
Barcode128 barcode128 = new Barcode128();
FontRenderContext fontRenderContext = g.getFontRenderContext();
// 条形码(文字)的高度
int stringHeight = (int) font.getStringBounds("", fontRenderContext).getHeight();
// 图片横坐标开始位置
int startX = barCodeWidth / 2;
// 图片纵坐标开始位置
int imageStartY = (imageHeight - barCodeHeight - stringHeight) / 2;
int stringStartY = imageStartY * 2 + barCodeHeight;// 条形码(文字)开始位置
for (String code : codeList) {
int codeWidth = (int) font.getStringBounds(code, fontRenderContext).getWidth();
barcode128.setCode(code);
Image codeImg = barcode128.createAwtImage(Color.black, Color.white);
g.drawImage(codeImg, startX, imageStartY, barCodeWidth, barCodeHeight, Color.white, null);
// 为图片添加条形码(文字),位置为条形码图片的下部居中
AttributedString ats = new AttributedString(code);
ats.addAttribute(TextAttribute.FONT, font, 0, code.length());
AttributedCharacterIterator iter = ats.getIterator();
// 设置条形码(文字)的颜色为蓝色
g.setColor(Color.BLUE);
// 绘制条形码(文字)
g.drawString(iter, startX + (barCodeWidth - codeWidth) / 2, stringStartY);
// 更改图片横坐标开始位置,图片之间的空隙为条形码的宽度的一半
startX = startX + barCodeWidth / 2 + barCodeWidth;
}
g.dispose();
OutputStream os = new BufferedOutputStream(new FileOutputStream("/home/admin/codeList.png"));
ImageIO.write(img, "PNG", os);
}

转载地址:http://gebai.baihongyu.com/

你可能感兴趣的文章
mysql中用命令行复制表结构的方法
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>
让代码变得更优雅-Lombok
查看>>
解决Rhythmbox乱码
查看>>
豆瓣爱问共享资料插件发布啦
查看>>
Ubuntu10.10 CAJView安装 读取nh\kdh\caj文件 成功
查看>>
kermit的安装和配置
查看>>
vim 配置
查看>>
openocd zylin
查看>>
进程创建时文件系统处理
查看>>
内核线程创建
查看>>
linux中cat命令使用详解
查看>>
java中的异常机制
查看>>
java SE面向对象思维导图
查看>>
三维分析之视频投放
查看>>
SuperMap iDesktop之栅格值怎么查
查看>>
SuperMap iClient3D for WebGL教程-orientation
查看>>
SuperMap iClient3D for WebGL教程-description描述属性
查看>>
SuperMap iClient3D for WebGL教程-CallbackProperty
查看>>
如何修改leaflet聚合图的层级和样式
查看>>