java实现条码打印源码,Graphics2D 在图片上画表格,图片等

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>

/**
* 打印模版
*
* @param imgWidth 图片的宽度
* @param imgHeight 图片的高度
* @param x 起始x轴
* @param y 起始y轴
* @param rowHeight 每行的高度
* @param dataStart 数据缩进
* @param firstWidth 第一列间距x坐标
* @param secondWidth 第二列间距x坐标
* @param thirdWidth 第三列间距x坐标
* @param fourWidth 第四列间距x坐标
* @param tb 实体对象
* @return BufferedImage
*(参数自己定,我的是340, 200, 0, 0, 20, 10, 60, 190, 250, 340, tb)
*/
private BufferedImage createTbGraphics(int imgWidth, int imgHeight, int x, int y, int rowHeight,
int dataStart, int firstWidth, int secondWidth, int thirdWidth, int fourWidth,
Tb tb) {
//空白面板 也可是张图片
BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = null;

try {

g = image.createGraphics();// 得到图形上下文
g.setBackground(Color.WHITE);//设置背景色
g.fillRect(x, y, imgWidth, imgHeight);//填充整个屏幕
g.setColor(Color.BLACK);//设置画笔颜色
// g.setFont(new Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 12));// 设置字体 这种以及“非国产”是乱码 有办法解决的话也可为实线, 我认为乱码是因为条码打印机的缘故,这样就要二次开发打印机,所以我没要这种方法
g.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 12));// 设置字体 还可设置为Font.ITALIC 就这两种条码打印机打印出来的的线是实体线
// g.setFont(new Font("simsun", Font.TYPE1_FONT, 12));// jvm里 想将字体格式simsun存入jvm里直接调用,打印出来也不成

g.drawLine(x, y, imgWidth, y);// 第一条横线

g.drawLine(x, y, x, rowHeight);//竖线
// g.drawString(new String("名称".getBytes("utf-8"),"utf-8"), dataStart, rowHeight - 5);
g.drawString("名称", dataStart, rowHeight - 5);
g.drawLine(firstWidth, y, firstWidth, rowHeight);
g.drawString(tb.getName() == null ? "" : tb.getName() , firstWidth + dataStart, rowHeight - 5);
g.drawLine(imgWidth - 1, y, imgWidth - 1, rowHeight);

g.drawLine(x, rowHeight, imgWidth, rowHeight); //横线
g.drawLine(x, rowHeight, x, rowHeight * 2);
g.drawString("代码", dataStart, rowHeight * 2 - 5); // * 2代表第二行
g.drawLine(firstWidth, rowHeight, firstWidth, rowHeight * 2);
g.drawString(tb.getCode() == null ? "" : tb.getCode() , firstWidth + dataStart, rowHeight * 2 - 5);
g.drawLine(secondWidth, rowHeight, secondWidth, rowHeight * 2);

//其他数据....

//最后一条线
g.drawLine(x, imgHeight - 1, imgWidth, imgHeight - 1);

//在指定坐标(198,61)处 写入二维码或其它图片
g.drawImage(ImageIO.read(getServletContext().getResourceAsStream(t.getPicUrl())), null, 198, 61);
g.dispose();
} catch (IOException e) {
e.printStackTrace();
}

return image;
}

打印效果如下:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值