public static String getMyTag(String type, String grade, String color, String rolls, String thickness, String grossWeight, String width, String length, String rollNO, String date) throws IOException {
// 创建图片
BufferedImage image = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_BGR);
Graphics2D g2d = (Graphics2D) image.getGraphics();
// 抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, 1000, 800);
//设置画笔颜色
g2d.setColor(Color.BLACK);
//按顺序的横线
myDrawLine(g2d, 3,10, 10, 760, 10);
myDrawLine(g2d, 3,10, 160, 760, 160);
myDrawLine(g2d, 3,10, 240, 760, 240);
myDrawLine(g2d, 3,10, 320, 760, 320);
myDrawLine(g2d, 3,10, 400, 760, 400);
myDrawLine(g2d, 3,10, 480, 760, 480);
myDrawLine(g2d, 3,10, 650, 760, 650);
//按顺序的竖线
myDrawLine(g2d, 3,10, 10, 10, 650);
myDrawLine(g2d, 3,197, 160, 197, 480);
myDrawLine(g2d, 3,384, 160, 384, 480);
myDrawLine(g2d, 3,571, 160, 571, 480);
myDrawLine(g2d, 3,760, 10, 760, 650);
//按顺序的字
//行3
myDrawString(g2d, 20, new Font("宋体", Font.BOLD, 53), type, 210,230, 1);
myDrawString(g2d, 20, new Font("宋体", Font.PLAIN, 53), grade, 640,230, 1);
//行4
myDrawString(g2d, 20, new Font("宋体", Font.PLAIN, 53), color, 210,310, 1);
myDrawString(g2d, 20, new Font("宋体", Font.PLAIN, 53), rolls + " ROLL", 579,310, 0.89);
//行5
myDrawString(g2d, 20, new Font("宋体", Font.PLAIN, 53), thickness + "mic", 210,390, 0.89);
myDrawString(g2d, 20, new Font("宋体", Font.PLAIN, 53), grossWeight + " KG", 579,390, 0.89);
//行6
myDrawString(g2d, 20, new Font("宋体", Font.PLAIN, 53), width + "mm", 210,470, 1);
myDrawString(g2d, 20, new Font("宋体", Font.BOLD, 53), length + "M", 579,470, 0.89);
g2d.dispose();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outputStream);// 输出png图片
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
String code =
"^XA\n" +
"^MMT\n" +
"^PW767\n" +
"^LL0687\n" +
"^LS0\n" +
"^FO0,0" + ZPLConveter.getFont2ImgZPL(byteArrayInputStream, true , 50) + "\n" +
"^BY4,3,84^FT124,595^BCN,,Y,N\n" + //条码
"^FD>;123456123456123456^FS\n" +
"^PQ1,0,1,Y" +
"^XZ\n";
return code;
}
/**
* 画一条线
* @param g2d Graph2D对象
* @param fontSize 线条宽度
* @param sWidth 线条起点X坐标
* @param sHeight 线条起点Y坐标
* @param eWidth 线条终点X坐标
* @param eHeight 线条终点Y坐标
*/
public static void myDrawLine(Graphics2D g2d, Integer fontSize, Integer sWidth, Integer sHeight, Integer eWidth, Integer eHeight) {
//设置线条的宽度
BasicStroke bs1 = new BasicStroke(fontSize);
g2d.setStroke(bs1);
g2d.drawLine(sWidth, sHeight, eWidth, eHeight);
}
/**
* 画一句字
* 使用rate可以控制字体的间距,但是并不会拉伸字体,可能导致字体重叠
* @param g2d Graph2D对象
* @param fontSize 画笔宽度
* @param font 字体格式
* @param str *输入的字
* @param x 字的X轴
* @param y 字的Y轴
* @param rate 字的间距(正常为1)
*/
public static void myDrawString(Graphics2D g2d, Integer fontSize, Font font, String str, Integer x, Integer y, double rate) {
g2d.setFont(font);
String tempStr=new String();
int orgStringWight=g2d.getFontMetrics().stringWidth(str);
int orgStringLength=str.length();
int tempx=x;
int tempy=y;
while(str.length()>0)
{
tempStr=str.substring(0, 1);
str=str.substring(1, str.length());
g2d.drawString(tempStr, tempx, tempy);
tempx=(int)(tempx+(double)orgStringWight/(double)orgStringLength*rate);
}
}