使用java Graphics 绘图工具生成快递快递电子面单

作者: adm 分类: java 发布时间: 2021-05-12

最近公司需要开发一个公司内部使用的快递下单系统,给我的开发任务中有一个生成电子面单功能,为了下单时更方便,利用此功能使用快递公司给我们的打印机直接打印出电子面单,刚接到这个任务时我想这应该很简单,不就是做一个表格打印出来吗,原本以为使用excel或者word等工具直接生成一个文档,后来经理说不用excel和word工具,让用Java直接生成电子面单,刚开始有点懵,因为不知道Java还有绘图功能,因此在网上学习了一下Java怎样绘图,索性直接开干。

废话不多说直接上代码。

一、    首先是生成条码工具类,此类是生成快递单号条码。

SFBarCodeGenerateUtil.java

package testNetty.wu;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* 顺丰速运条码生成工具<br/>
* 采用 code128c编码规则<br/>
* <pre>
* CODE128C:[00]-[99]的数字对集合,共100个
* 即只能表示偶数位长度的数字
* </pre>
* @author wu
* @version 1.0
* @Time 2017/03/29
* */
public class SFBarCodeGenerateUtil {

private static final Logger logger = Logger.getLogger(SFBarCodeGenerateUtil.class.getSimpleName());

/**图片格式 jpg 格式*/
public static final String PICTURE_JPG = “JPG”;
/**图片格式 png 格式*/
public static final String PICTURE_PNG = “PNG”;
/**图片格式 gif 格式*/
public static final String PICTURE_GIF = “GIF”;

/** code128编码字符集 二维数组*/
private static String[][] code128 = {
{ ” “, ” “, “00”, “212222”, “11011001100” },
{ “!”, “!”, “01”, “222122”, “11001101100” },
{ “\””, “\””, “02”, “222221”, “11001100110” },
{ “#”, “#”, “03”, “121223”, “10010011000” },
{ “$”, “$”, “04”, “121322”, “10010001100” },
{ “%”, “%”, “05”, “131222”, “10001001100” },
{ “&”, “&”, “06”, “122213”, “10011001000” },
{ “‘”, “‘”, “07”, “122312”, “10011000100” },
{ “(“, “(“, “08”, “132212”, “10001100100” },
{ “)”, “)”, “09”, “221213”, “11001001000” },
{ “*”, “*”, “10”, “221312”, “11001000100” },
{ “+”, “+”, “11”, “231212”, “11000100100” },
{ “,”, “,”, “12”, “112232”, “10110011100” },
{ “-“, “-“, “13”, “122132”, “10011011100” },
{ “.”, “.”, “14”, “122231”, “10011001110” },
{ “/”, “/”, “15”, “113222”, “10111001100” },
{ “0”, “0”, “16”, “123122”, “10011101100” },
{ “1”, “1”, “17”, “123221”, “10011100110” },
{ “2”, “2”, “18”, “223211”, “11001110010” },
{ “3”, “3”, “19”, “221132”, “11001011100” },
{ “4”, “4”, “20”, “221231”, “11001001110” },
{ “5”, “5”, “21”, “213212”, “11011100100” },
{ “6”, “6”, “22”, “223112”, “11001110100” },
{ “7”, “7”, “23”, “312131”, “11101101110” },
{ “8”, “8”, “24”, “311222”, “11101001100” },
{ “9”, “9”, “25”, “321122”, “11100101100” },
{ “:”, “:”, “26”, “321221”, “11100100110” },
{ “;”, “;”, “27”, “312212”, “11101100100” },
{ “<“, “<“, “28”, “322112”, “11100110100” },
{ “=”, “=”, “29”, “322211”, “11100110010” },
{ “>”, “>”, “30”, “212123”, “11011011000” },
{ “?”, “?”, “31”, “212321”, “11011000110” },
{ “@”, “@”, “32”, “232121”, “11000110110” },
{ “A”, “A”, “33”, “111323”, “10100011000” },
{ “B”, “B”, “34”, “131123”, “10001011000” },
{ “C”, “C”, “35”, “131321”, “10001000110” },
{ “D”, “D”, “36”, “112313”, “10110001000” },
{ “E”, “E”, “37”, “132113”, “10001101000” },
{ “F”, “F”, “38”, “132311”, “10001100010” },
{ “G”, “G”, “39”, “211313”, “11010001000” },
{ “H”, “H”, “40”, “231113”, “11000101000” },
{ “I”, “I”, “41”, “231311”, “11000100010” },
{ “J”, “J”, “42”, “112133”, “10110111000” },
{ “K”, “K”, “43”, “112331”, “10110001110” },
{ “L”, “L”, “44”, “132131”, “10001101110” },
{ “M”, “M”, “45”, “113123”, “10111011000” },
{ “N”, “N”, “46”, “113321”, “10111000110” },
{ “O”, “O”, “47”, “133121”, “10001110110” },
{ “P”, “P”, “48”, “313121”, “11101110110” },
{ “Q”, “Q”, “49”, “211331”, “11010001110” },
{ “R”, “R”, “50”, “231131”, “11000101110” },
{ “S”, “S”, “51”, “213113”, “11011101000” },
{ “T”, “T”, “52”, “213311”, “11011100010” },
{ “U”, “U”, “53”, “213131”, “11011101110” },
{ “V”, “V”, “54”, “311123”, “11101011000” },
{ “W”, “W”, “55”, “311321”, “11101000110” },
{ “X”, “X”, “56”, “331121”, “11100010110” },
{ “Y”, “Y”, “57”, “312113”, “11101101000” },
{ “Z”, “Z”, “58”, “312311”, “11101100010” },
{ “[“, “[“, “59”, “332111”, “11100011010” },
{ “\\”, “\\”, “60”, “314111”, “11101111010” },
{ “]”, “]”, “61”, “221411”, “11001000010” },
{ “^”, “^”, “62”, “431111”, “11110001010” },
{ “_”, “_”, “63”, “111224”, “10100110000” },
{ “NUL”, “`”, “64”, “111422”, “10100001100” },
{ “SOH”, “a”, “65”, “121124”, “10010110000” },
{ “STX”, “b”, “66”, “121421”, “10010000110” },
{ “ETX”, “c”, “67”, “141122”, “10000101100” },
{ “EOT”, “d”, “68”, “141221”, “10000100110” },
{ “ENQ”, “e”, “69”, “112214”, “10110010000” },
{ “ACK”, “f”, “70”, “112412”, “10110000100” },
{ “BEL”, “g”, “71”, “122114”, “10011010000” },
{ “BS”, “h”, “72”, “122411”, “10011000010” },
{ “HT”, “i”, “73”, “142112”, “10000110100” },
{ “LF”, “j”, “74”, “142211”, “10000110010” },
{ “VT”, “k”, “75”, “241211”, “11000010010” },
{ “FF”, “I”, “76”, “221114”, “11001010000” },
{ “CR”, “m”, “77”, “413111”, “11110111010” },
{ “SO”, “n”, “78”, “241112”, “11000010100” },
{ “SI”, “o”, “79”, “134111”, “10001111010” },
{ “DLE”, “p”, “80”, “111242”, “10100111100” },
{ “DC1”, “q”, “81”, “121142”, “10010111100” },
{ “DC2”, “r”, “82”, “121241”, “10010011110” },
{ “DC3”, “s”, “83”, “114212”, “10111100100” },
{ “DC4”, “t”, “84”, “124112”, “10011110100” },
{ “NAK”, “u”, “85”, “124211”, “10011110010” },
{ “SYN”, “v”, “86”, “411212”, “11110100100” },
{ “ETB”, “w”, “87”, “421112”, “11110010100” },
{ “CAN”, “x”, “88”, “421211”, “11110010010” },
{ “EM”, “y”, “89”, “212141”, “11011011110” },
{ “SUB”, “z”, “90”, “214121”, “11011110110” },
{ “ESC”, “{“, “91”, “412121”, “11110110110” },
{ “FS”, “|”, “92”, “111143”, “10101111000” },
{ “GS”, “},”, “93”, “111341”, “10100011110” },
{ “RS”, “~”, “94”, “131141”, “10001011110” },
{ “US”, “DEL”, “95”, “114113”, “10111101000” },
{ “FNC3”, “FNC3”, “96”, “114311”, “10111100010” },
{ “FNC2”, “FNC2”, “97”, “411113”, “11110101000” },
{ “SHIFT”, “SHIFT”, “98”, “411311”, “11110100010” },
{ “CODEC”, “CODEC”, “99”, “113141”, “10111011110” },
{ “CODEB”, “FNC4”, “CODEB”, “114131”, “10111101110” },
{ “FNC4”, “CODEA”, “CODEA”, “311141”, “11101011110” },
{ “FNC1”, “FNC1”, “FNC1”, “411131”, “11110101110” },
{ “StartA”, “StartA”, “StartA”, “211412”, “11010000100” },
{ “StartB”, “StartB”, “StartB”, “211214”, “11010010000” },
{ “StartC”, “StartC”, “StartC”, “211232”, “11010011100” },
{ “Stop”, “Stop”, “Stop”, “2331112”, “1100011101011” },
};

/**
* 生产Code128的条形码的code
* @param barCode 生成条码的数字号码
* @return
*/
private static String getCode(String barCode) {
String rtnCode = “”;// 返回的参数
List<Integer> rtnCodeNumb = new ArrayList<Integer>();// 2截取位的组合
int examine = 105; // 首位
// 编码不能是奇数
if (!((barCode.length() & 1) == 0))
return “”;
while (barCode.length() != 0) {
int temp = 0;
try {
// Code128 编码必须为数字
temp = (Integer) Integer.valueOf(barCode.substring(0, 2));
} catch (Exception e) {
e.printStackTrace();
return “”;
}
// 获得条纹
rtnCode += getValue(barCode, barCode.substring(0, 2), temp);
rtnCodeNumb.add(temp);
// 条码截取2个就需要去掉用过的前二位
barCode = barCode.substring(2);
}
if (rtnCodeNumb.size() == 0) {
return “”;
}
rtnCode = getValue(examine) + rtnCode; // 获取开始位
for (int i = 0; i != rtnCodeNumb.size(); i++) {
examine += rtnCodeNumb.get(i) * (i + 1);
}
examine = examine % 103; // 获得校验位
rtnCode += getValue(examine); // 获取校验位
rtnCode += “1100011101011”; // 结束位
return rtnCode;
}

/**
* 根据编号获得条纹
*
* @param encode
* @param p_Value
* @param p_SetID
* @return
*/
private static String getValue(String encode, String p_Value, int p_SetID) {
return code128[p_SetID][4];
}

/**
* 根据编号获得条纹
* @param p_CodeId
* @return
*/
private static String getValue(int p_CodeId) {
return code128[p_CodeId][4];
}

// 条码的高度像素数
private static int m_nImageHeight = 40;

/**
* 生成条码
* @param barString 条码模式字符串
* @param path 生成条码图片的路径
*/
private static boolean kiCode128C(String barString, String path) {
OutputStream out = null;
try {
File myPNG = new File(path);
out = new FileOutputStream(myPNG);
int nImageWidth = 0;
char[] cs = barString.toCharArray();
for (int i = 0; i != cs.length; i++) {
nImageWidth = cs.length;
}
BufferedImage bi = new BufferedImage(nImageWidth, m_nImageHeight,
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
for (int i = 0; i < cs.length; i++) {
if (“1”.equals(cs[i] + “”)) {
g.setColor(Color.BLACK);
g.fillRect(i, 0, 1, m_nImageHeight);
} else {
g.setColor(Color.WHITE);
g.fillRect(i, 0, 1, m_nImageHeight);
}
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
return true;
} catch (FileNotFoundException fe) {
logger.error(“系统找不到指定路径!!” + path, fe);
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return false;
}

/**
* 生成条形码<br/>
* 条码是图片格式(JPG、PNG、GIF)
* @param wayBillNo
* 运单号
* @param generatePathName
* 生成条形码路径及名称
* @param width
* 条码图片宽度
* @param height
* 条码图片高度
* @param picTyp
* 图片格式<br/>
* JPG、PNG、GIF三种图片类型选择
* @return boolean类型值
* true 生成成功,false 生成失败
* */
public static boolean generateBarCode(String wayBillNo, String generatePathName, int width,
int height, String picTyp){
//只能是数字
if (!Pattern.matches(“[0-9]+”, wayBillNo)) {
logger.error(“生成CODE128C条码只能是0~9的数字不能有其他字符!!”);
//运单号长度只能是偶数
} else if (wayBillNo.length() % 2 != 0) {
logger.error(“生成CODE128C条码的长度只能是偶数!!”);
//生成条码
} else if (kiCode128C(getCode(wayBillNo), generatePathName)){
/**
* 设置条码图片的尺寸
* */
BufferedInputStream bis = null;
BufferedOutputStream out = null;
try {
File sfFile = new File(generatePathName);
if (sfFile.isFile() && sfFile.exists()) {
//读取图片
bis = new BufferedInputStream(new FileInputStream(generatePathName));
//转换成图片对象
Image bi = ImageIO.read(bis);
//构建图片流 设置图片宽和高
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//绘制改变尺寸后的图
tag.getGraphics().drawImage(bi, 0, 0,width, height, null);
//保存图片
out = new BufferedOutputStream(new FileOutputStream(generatePathName));
ImageIO.write(tag, picTyp,out);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
if (bis != null)
bis.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
logger.info(“条码生成成功. ” + generatePathName + ” 像素:” + width + “*” + height);
return true;
}
logger.error(“条码生成失败!”);
return false;
}

}

 

二、其次是生成电子面单表格类,包含条码、单号、寄件人和收件人信息。

SFOrderGenerateUtil.java

package testNetty.wu;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* 生成电子面单图片工具
* @author wu
* @version 1.0
* @time 2017/04/25
* */
public class SFOrderGenerateUtil {

private static final Logger logger = Logger.getLogger(SFOrderGenerateUtil.class.getSimpleName());

//图片的宽度
public static final int IMG_WIDTH = 1198;
//图片的宽度
public static final int IMG_HEIGHT = 1800;
//LOGO的宽度
public static final int LOGO_WIDTH = 240;
//LOGO高度
public static final int LOGO_HEIGHT = 100;
//LOGO客服电话的宽度
public static final int LOGO_TEL_WIDTH = 220;
//LOGO客服电话高度
public static final int LOGO_TEL_HEIGHT = 84;

//Logo路径
public static final String LOGO_PATH = “C:\\Users\\Administrator\\Desktop\\expressLogo\\logoSC.png”;
//Logo客服电话
public static final String LOGO_TEL_PATH = “C:\\Users\\Administrator\\Desktop\\expressLogo\\sf_Tel.png”;;

public static BufferedImage image;
public static void createImage(String fileLocation) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(fileLocation);
bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bos != null) bos.close();
if (fos != null) fos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

/**
* 生成订单图片
* @param orderPath
* 生成订单存放路径
* @param printTyp
* 订单打印类型 1:A4纸打印 2:热敏纸打印
* @param orderParam
* 生成订单所需的参数对象
* @param isCompress
* 是否需要根据输入宽高压缩图片
* <pre>
* isCompress 为ture时 所输入的宽高才起作用
* </pre>
* @param imgWidth
* 图片宽度
* @param imgHeidht
* 图片高度
* @author Administrator
* @return
*
* */
public static boolean generateOrders(String orderPath, SfPrintOrderParam orderParam, String printTyp, boolean isCompress, int imgWidth, int imgHeidht){
if (null == orderParam)
return false;
int startHeight = 0; //表格的起始高度
int startWidth = 0; //表格的起始宽度
try {
if (orderParam.getSubMailNos().size() == 0 || orderParam.getSubMailNos().isEmpty()) {
generateParentOrder(orderPath, orderParam, printTyp, isCompress, imgWidth, imgHeidht);
return true;
} else {
String picPath = orderPath;
File mk = new File(picPath + orderParam.getMailNo());
if (mk.exists()){
FileUtils.deleteDirectory(mk);
}
for (int k = 0; k < orderParam.getSubMailNos().size(); k++) {
image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
//以运单号为名称创建存放订单的目录
FileUtils.forceMkdir(mk);
picPath += orderParam.getMailNo() + “/”;

//设置背景色为白色
g.setColor(Color.WHITE);
//设置颜色区域大小
g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
//提高导入Img的清晰度

/*
* 绘制表格 填充内容
* */
//表格线条的颜色
g.setColor(Color.BLACK);

//消除文本出现锯齿现象
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);

//表格的四个边框
g.drawLine(startWidth, startHeight, startWidth + 374, startHeight); //上边框
g.drawLine(startWidth, startHeight, startWidth, startHeight + 562); //左边框
g.drawLine(startWidth, startHeight + 562, startWidth + 375, startHeight + 562); //下边框
g.drawLine(startWidth + 374, startHeight, startWidth + 374, startHeight + 563); //右边框

//绘制表格内容 第一行
g.drawLine(startWidth, startHeight + 48, startWidth + 375, startHeight + 48);

//插入顺丰速运Logo
Image logoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
g.drawImage(logoImg, startWidth + 4, startHeight + 8, null);
//插入第二个logo (客服电话)
Image logoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
g.drawImage(logoTelImg, startWidth + 280, startHeight + 15, null);

//填写产品类型
Font fontSfTyp = new Font(“黑体”, Font.BOLD, 36);
g.setFont(fontSfTyp);
//产品类型字符串
String sfProTypStr = orderParam.getEarthCarryFlag();
g.drawString(sfProTypStr, startWidth + 150, startHeight + 41);

//绘制第二行
g.drawLine(startWidth, startHeight + 124, startWidth + 375, startHeight + 124);
g.drawLine(startWidth + 276, startHeight + 48, startWidth + 276, startHeight + 124);

//生成code128c 条码
SFBarCodeGenerateUtil.generateBarCode(orderParam.getMailNo(), //运单号
picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg”, //图片名称
262, //图片宽度
45, //图片高度
SFBarCodeGenerateUtil.PICTURE_JPG);
//导入条码图片
Image sfBarImg = insertImage(picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg”, 0, 0, false);
g.drawImage(sfBarImg, startWidth + 7, startHeight + 53, null);
sfBarImg.flush();

//设置字体
Font fontSfBarCode = new Font(“黑体”,Font.BOLD,10);
g.setFont(fontSfBarCode);

String pageNum = (k + 1) + “/” + orderParam.getSubMailNos().size();
g.drawString(pageNum, startWidth + 7, startHeight + 108);

//子单号
String subBarCodeStr = orderParam.getSubMailNos().get(k);
subBarCodeStr = subBarCodeStr.replaceAll(“(.{3})”, “$1 “);
g.drawString(“子单号 ” + subBarCodeStr, startWidth + 80, startHeight + 108);

//条码字符串(条码母单号)
String sfBarCodeStr = orderParam.getMailNo();
sfBarCodeStr = sfBarCodeStr.replaceAll(“(.{3})”, “$1 “);
g.drawString(“母单号 ” + sfBarCodeStr, startWidth + 80, startHeight + 120);

//绘制产品类型框
g.drawLine(startWidth + 276, startHeight + 70, startWidth + 375, startHeight + 70);
Font fontSfProtypSub = new Font(“黑体”, Font.BOLD, 14);
g.setFont(fontSfProtypSub);

//产品类型字符串
String subSfProTypStr = orderParam.getExpressTyp();
g.drawString(subSfProTypStr, startWidth + 295, startHeight + 65);

//目的地栏的绘制
g.drawLine(startWidth, startHeight + 176, startWidth + 375, startHeight + 176);
g.drawLine(startWidth + 21, startHeight + 124, startWidth + 21, startHeight + 176);

//目的地填写
Font fontDest = new Font(“黑体”, Font.BOLD, 10);
g.setFont(fontDest);
//目的地标题
String destTitleStr = “目的地”;
char[] destTitleArray = destTitleStr.toCharArray();
int destTitleWidth = startWidth + 6;
int destTitleHeight = startHeight + 140;
for (int i = 0; i < destTitleStr.length(); i++) {
g.drawString(String.valueOf(destTitleArray[i]), destTitleWidth, destTitleHeight);
destTitleHeight += 12;
}

//目的地代码
Font fontDestCode = new Font(“Arial”, Font.BOLD, 36);
g.setFont(fontDestCode);
//目的地代码字符串
String destCode = orderParam.getDestCode();
g.drawString(destCode, startWidth + 24, startHeight + 165);

//收件人表格栏
g.drawLine(startWidth, startHeight + 225, startWidth + 1198, startHeight + 225);
g.drawLine(startWidth + 21, startHeight + 176, startWidth + 21, startHeight + 225);

//设置收件人标题字体
Font fontRevicer = new Font(“黑体”, Font.BOLD, 10);
g.setFont(fontRevicer);
//收件人标题字符串
String revicerTitleStr = “收件人”;
char[] revicerTitleArray = revicerTitleStr.toCharArray();
int revicerTitleWidth = startWidth + 6;
int revicerTitleHeight = startHeight + 192;
for (int i = 0; i < revicerTitleStr.length(); i++) {
g.drawString(String.valueOf(revicerTitleArray[i]), revicerTitleWidth, revicerTitleHeight);
revicerTitleHeight += 11;
}

/*
* 收件人详细信息
* */
String dContact = orderParam.getdContact(); //收件人姓名
String dTel = orderParam.getdTel(); //联系电话
String dMoblie = orderParam.getdMobile(); //联系人手机号
String dCompany = orderParam.getdCompany(); //公司名称
String dProvince = orderParam.getdProvince();
String dCity = orderParam.getdCity();
String dCounty = orderParam.getdCounty();
String dAddress = orderParam.getdAddress(); //详细地址

String revicerInfo = dContact + ” ” + dTel + ” ” + dMoblie + “” + dCompany;
dAddress = dProvince + dCity + dCounty + dAddress;

//设置收件人信息字体
Font fontRevicerInfo = new Font(“黑体”, Font.BOLD, 14);
g.setFont(fontRevicerInfo);
g.drawString(revicerInfo, startWidth + 24, startHeight + 190);
//设置收件人详细地址字体
Font fontReviceAddress = new Font(“黑体”, Font.BOLD, 14);
g.setFont(fontReviceAddress);
if (dAddress.length() > 23) {
g.drawString(dAddress.substring(0, 23), startWidth + 24, startHeight + 205);
g.drawString(dAddress.substring(23, dAddress.length()), startWidth + 24, startHeight + 220);
} else {
g.drawString(dAddress, startWidth + 24, startHeight + 205);
}

//绘制寄件人表格
g.drawLine(startWidth, startHeight + 259, startWidth + 375, startHeight +259);
g.drawLine(startWidth + 21, startHeight + 225, startWidth + 21, startHeight + 259);

//设置寄件人标题字体
Font fontSender = new Font(“黑体”, Font.BOLD, 10);
g.setFont(fontSender);
//寄件人标题字符串
String senderTitleStr = “寄件人”;
char[] senderTitleArray = senderTitleStr.toCharArray();
int senderTitleWidth = startWidth + 6;
int senderTitleHeight = startHeight + 235;
for (int i = 0; i < senderTitleStr.length(); i++) {
g.drawString(String.valueOf(senderTitleArray[i]), senderTitleWidth, senderTitleHeight);
senderTitleHeight += 11;
}

/*
* 寄件人信息
* **/
String jContact = orderParam.getjContact(); //寄件人姓名
String jTel = orderParam.getjTel(); //寄件人联系电话
String jMobile = orderParam.getjMobile();
String jCompany = orderParam.getjCompany();
String jProvince = orderParam.getjProvince();
String jCity = orderParam.getjCity();
String jCounty = orderParam.getjCounty();
String jAddress = orderParam.getjAddress();

String senderInfo = jContact + ” ” + jTel + ” ” + jMobile + ” ” + jCompany;
jAddress = jProvince + jCity + jCounty + jAddress;

//设置寄件人信息字体
Font fontSenderInfo = new Font(“黑体”, Font.PLAIN, 8);
g.setFont(fontSenderInfo);
g.drawString(senderInfo, startWidth + 24, startHeight + 240);

//设置寄件人详细地址字体
Font fontSenderAddress = new Font(“黑体”, Font.PLAIN, 8);
g.setFont(fontSenderAddress);
if (jAddress.length() > 27) {
g.drawString(jAddress.substring(0, 27), startWidth + 24, startHeight + 250);
g.drawString(jAddress.substring(27, jAddress.length()), startWidth + 24, startHeight + 265);
} else {
g.drawString(jAddress, startWidth + 24, startHeight + 250);
}

//绘制派送方式表格
g.drawLine(startWidth + 310, startHeight + 225, startWidth + 310, startHeight + 338);
//设置派送类型字体
Font fontSenTyp = new Font(“黑体”, Font.BOLD, 55);
g.setFont(fontSenTyp);

//快递详细信息表格
g.drawLine(startWidth, startHeight + 304, startWidth + 310, startHeight + 304);

//快递详细信息
Font cellFont = new Font(“黑体”, Font.PLAIN, 9);
g.setFont(cellFont);
String[][] cellValue1 = {
{“付款方式:”,orderParam.getPayMethod()},
{“月结账号:”,orderParam.getMonthSettleNo()},
{“第三方地区:”,orderParam.getThridArea()},
{“实际重量:”,orderParam.getRealWeight()}
};
int cellLineHeight = startHeight + 268;
for (int i = 0; i < cellValue1.length; i++) {
g.drawString(cellValue1[i][0], startWidth + 3, cellLineHeight);
g.drawString(cellValue1[i][1], startWidth + 52, cellLineHeight);
cellLineHeight += 10;
}
String[][] cellValue2 = {
{“计费重量:”,orderParam.getChargWeight()},
{“声明价值:”,orderParam.getDeclarPrice()},
{“保价费用:”,orderParam.getSupportFee()},
{“定时派送:”,orderParam.getSendTime()}
};
cellLineHeight = startHeight + 268;
for (int i = 0; i < cellValue2.length; i++) {
g.drawString(cellValue2[i][0], startWidth + 100, cellLineHeight);
g.drawString(cellValue2[i][1], startWidth + 150, cellLineHeight);
cellLineHeight += 10;
}
String[][] cellValue3 = {
{“包装费用:”,orderParam.getPackFee()},
{“运费:”,orderParam.getFreight()},
{“费用合计:”,orderParam.getSumFee()}
};
cellLineHeight = startHeight + 268;
for (int i = 0; i < cellValue3.length; i++) {
g.drawString(cellValue3[i][0], startWidth + 220, cellLineHeight);
g.drawString(cellValue3[i][1], startWidth + 270, cellLineHeight);
cellLineHeight += 10;
}

//转寄协议客户
Font fowardFont = new Font(“黑体”, Font.BOLD, 9);
g.setFont(fowardFont);
String fowardStr = “转寄协议客户”;
g.drawString(fowardStr, startWidth + 250, startHeight + 302);

//托寄物
g.drawLine(startWidth + 21, startHeight + 304, startWidth + 21, startHeight + 338);
//托寄物标题字符串
String articleTitleStr = “托寄物”;
char[] articleTitleArray = articleTitleStr.toCharArray();
int articleTitleWidth = startWidth + 6;
int articleTitleHeight = startHeight + 315;
for (int i = 0; i < articleTitleStr.length(); i++) {
g.drawString(String.valueOf(articleTitleArray[i]), articleTitleWidth, articleTitleHeight);
articleTitleHeight += 10;
}

Font cargoFont = new Font(“黑体”, Font.PLAIN, 8);
g.setFont(cargoFont);
String cargoContent = orderParam.getCargoContent();
g.drawString(cargoContent, startWidth + 24, 315);

//收件员信息
g.drawLine(startWidth + 220, startHeight + 304, startWidth + 220, startHeight + 338);
Font receDriverFont = new Font(“黑体”, Font.PLAIN, 8);
g.setFont(receDriverFont);
String[][] receDriverStrs = {
{“收件员:”,orderParam.getReviceDriver()},
{“寄件日期:”,orderParam.getSendDate()},
{“派件员:”,orderParam.getSendDriver()}
};
cellLineHeight = startHeight + 315;
for (int i = 0; i < receDriverStrs.length; i++) {
g.drawString(receDriverStrs[i][0], startWidth + 225, cellLineHeight);
g.drawString(receDriverStrs[i][1], startWidth + 265, cellLineHeight);
cellLineHeight += 10;
}
//签名
String signStr = “签名:”;
g.drawString(signStr, startWidth + 312, startHeight + 268);
//月日
String monthDay = “月 日”;
g.drawString(monthDay, startWidth + 345, startHeight + 336);

//母单与子单分割线
g.drawLine(startWidth, startHeight + 338, startWidth + 375, startHeight + 338);

//————————— 如果有子单号 生成子单信息 ———————————-//
g.drawLine(startWidth, startHeight + 394, startWidth + 375, startHeight + 394);
g.drawLine(startWidth + 100, startHeight + 338, startWidth + 100, startHeight + 394);
//插入Logo
Image sublogoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
g.drawImage(sublogoImg, startWidth + 10, startHeight + 338, null);
//插入第二个logo (客服电话)
Image sublogoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
g.drawImage(sublogoTelImg, startWidth + 15, startHeight + 368, null);

//导入子单条码
//生成code128c 条码
SFBarCodeGenerateUtil.generateBarCode(orderParam.getSubMailNos().get(k), //运单号
picPath + “SFBarCoding_sub_” + orderParam.getSubMailNos().get(k) + “.jpg”, //图片名称
169, //图片宽度
37, //图片高度
SFBarCodeGenerateUtil.PICTURE_JPG);
Image sfSubBarImg = insertImage(picPath + “SFBarCoding_sub_” + orderParam.getSubMailNos().get(k) + “.jpg”, 0, 0, false);
g.drawImage(sfSubBarImg, startWidth + 150, startHeight + 342, null);
sfSubBarImg.flush();

//子单号
Font subOrderFont = new Font(“黑体”, Font.BOLD, 10);
g.setFont(subOrderFont);
g.drawString(“子单号: ” + subBarCodeStr, startWidth + 160, startHeight + 390);

//绘制寄件人表格 子单
g.drawLine(startWidth, startHeight + 431, startWidth + 375, startHeight + 431);
g.drawLine(startWidth + 21, startHeight + 394, startWidth + 21, startHeight + 431);
//设置寄件人标题字体
Font fontsubSender = new Font(“黑体”, Font.BOLD, 10);
g.setFont(fontsubSender);
//寄件人标题字符串
String senderSubTitleStr = “寄件人”;
char[] senderSubTitleArray = senderSubTitleStr.toCharArray();
int senderSubTitleWidth = startWidth + 6;
int senderSubTitleHeight = startHeight + 408;
for (int i = 0; i < senderSubTitleStr.length(); i++) {
g.drawString(String.valueOf(senderSubTitleArray[i]), senderSubTitleWidth, senderSubTitleHeight);
senderSubTitleHeight += 10;
}
Font subJInfoFont = new Font(“黑体”, Font.PLAIN, 8);
g.setFont(subJInfoFont);
g.drawString(senderInfo, startWidth + 24, startHeight + 405);

if (jAddress.length() > 30) {
g.drawString(jAddress.substring(0, 30), startWidth + 24, startHeight + 415);
g.drawString(jAddress.substring(30, jAddress.length()), startWidth + 24, startHeight + 425);
} else {
g.drawString(jAddress, startWidth + 24, startHeight + 415);
}

//绘制收件人表格 子单
g.drawLine(startWidth, startHeight + 469, startWidth + 375, startHeight + 469);
g.drawLine(startWidth + 21, startHeight + 394, startWidth + 21, startHeight + 469);
//收件人标题字符串
Font fontsubReveicer = new Font(“黑体”, Font.BOLD, 10);
g.setFont(fontsubReveicer);
String revicerSubTitleStr = “收件人”;
char[] revicerSubTitleArray = revicerSubTitleStr.toCharArray();
int revicerSubTitleWidth = startWidth + 6;
int revicerSubTitleHeight = startHeight + 445;
for (int i = 0; i < revicerSubTitleStr.length(); i++) {
g.drawString(String.valueOf(revicerSubTitleArray[i]), revicerSubTitleWidth, revicerSubTitleHeight);
revicerSubTitleHeight += 10;
}

Font subDInfoFont = new Font(“黑体”, Font.PLAIN, 8);
g.setFont(subDInfoFont);
g.drawString(revicerInfo, startWidth + 24, startHeight + 442);
if (dAddress.length() > 35) {
g.drawString(dAddress.substring(0, 35), startWidth + 24, startHeight + 452);
g.drawString(dAddress.substring(35, dAddress.length()), startWidth + 24, startHeight + 462);
} else {
g.drawString(dAddress, startWidth + 24, startHeight + 452);
}
createImage(picPath + “SfOrderImage_” + orderParam.getMailNo() + “_” + (k + 1) + “.jpg”);
//如果需要需要根据自定义尺寸压缩图片
if (isCompress) {
//压缩图片
compressImg(picPath + “SfOrderImage_” + orderParam.getMailNo() + “_” + (k + 1) + “.jpg”, imgWidth, imgHeidht);
}
g.dispose();
File sfSubFile = new File(picPath + “SFBarCoding_sub_” + orderParam.getSubMailNos().get(k) + “.jpg”);
if (sfSubFile.exists() && sfSubFile.isFile()) {
if (!sfSubFile.delete()){
logger.error(“删除” + picPath + “SFBarCoding_sub_” + orderParam.getSubMailNos().get(k) + “.jpg 失败!”);
}
}
File sfbarFile = new File(picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg”);
if (sfbarFile.exists() && sfbarFile.isFile()) {
if (!sfbarFile.delete()) {
logger.error(“删除” + picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg 失败!”);
}
}
logger.error(“订单生成成功. ” + picPath + “SfOrderImage_” + orderParam.getMailNo() + “_” + (k + 1) + “.jpg”);
picPath = orderPath;
}
}
return true;

} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* 插入图片 自定义图片的宽高
* @param imgPath
* 插入图片的路径
* @param imgWidth
* 设置图片的宽度
* @param imgHeight
* 设置图片的高度
* @param isCompress
* 是否按输入的宽高定义图片的尺寸,只有为true时 输入的宽度和高度才起作用<br/>
* 为false时输入的宽高不起作用,按输入图片的默认尺寸
* @return
* @throws Exception
* */
private static Image insertImage(String imgPath, int imgWidth, int imgHeight, boolean isCompress) throws Exception {
File fileimage = new File(imgPath);
Image src = ImageIO.read(fileimage);
if (isCompress) {
Image image = src.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH);
return image;
}
return src;
}

/**
* 生成母订单
* @param orderParam 生成订单所需的参数对象
* @return boolean
* */
private static boolean generateParentOrder(String orderPath, SfPrintOrderParam orderParam, String printTyp, boolean isCompress, int imgWidth, int imgHeidht){
if (null == orderParam)
return false;
String picPath = orderPath;
int startHeight = 0; //表格的起始高度
int startWidth = 0; //表格的起始宽度
try {
if (orderParam.getSubMailNos().size() == 0 || orderParam.getSubMailNos().isEmpty()) {
image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();

//以运单号为名称创建存放订单的目录
File mk = new File(picPath + orderParam.getMailNo());
if (mk.exists()) {
FileUtils.deleteDirectory(mk);
}
FileUtils.forceMkdir(mk);

picPath += orderParam.getMailNo() + “/”;

//设置背景色为白色
g.setColor(Color.WHITE);
//设置颜色区域大小
g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);

/*
* 绘制表格 填充内容
* */
//表格线条的颜色
g.setColor(Color.BLACK);
//边框加粗
g.setStroke(new BasicStroke(2.0f));
//消除文本出现锯齿现象
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//表格的四个边框
g.drawLine(startWidth, startHeight, startWidth + 1198, startHeight); //上边框

g.drawLine(startWidth, startHeight, startWidth, startHeight + 1800); //左边框
g.drawLine(startWidth, startHeight + 1799, startWidth + 1198, startHeight + 1799); //下边框
g.drawLine(startWidth + 1197, startHeight, startWidth + 1197, startHeight + 1800); //右边框

//绘制表格内容 第一行
g.drawLine(startWidth, startHeight + 155, startWidth + 1198, startHeight + 155);

//A4纸打印是才有Logo
if (Integer.valueOf(printTyp) == ExpressConstant.ORDER_PRINT_TYP_A4) {
//插入Logo
Image logoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
g.drawImage(logoImg, startWidth + 30, startHeight + 30, null);
//插入第二个logo (客服电话)
Image logoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
g.drawImage(logoTelImg, startWidth + 920, startHeight + 30, null);
}

//E路标识
Font fontSfTyp = new Font(“微软雅黑”, Font.BOLD, 145);
g.setFont(fontSfTyp);
//E路标识
String sfProTypStr = orderParam.getEarthCarryFlag();
g.drawString(sfProTypStr, startWidth + 445, startHeight + 130);

//绘制第二行
g.drawLine(startWidth, startHeight + 396, startWidth + 1198, startHeight + 396);
g.drawLine(startWidth + 750, startHeight + 155, startWidth + 750, startHeight + 396);

//生成code128c 条码
SFBarCodeGenerateUtil.generateBarCode(orderParam.getMailNo(), //运单号
picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg”, //图片名称
600, //图片宽度
120, //图片高度
SFBarCodeGenerateUtil.PICTURE_JPG);
//导入条码图片
Image sfBarImg = insertImage(picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg”, 0, 0, false);
g.drawImage(sfBarImg, startWidth + 80, startHeight + 191, null);

//设置字体
Font fontSfBarCode = new Font(“黑体”,Font.BOLD,35);
g.setFont(fontSfBarCode);

String sfBarCodeStr = orderParam.getMailNo();
sfBarCodeStr = sfBarCodeStr.replaceAll(“(.{3})”, “$1 “);
g.drawString(“母单号 ” + sfBarCodeStr, startWidth + 150, startHeight + 355);

//绘制产品类型框
g.drawLine(startWidth + 750, startHeight + 240, startWidth + 1198, startHeight + 240);
Font fontSfProtypSub = new Font(“黑体”, Font.BOLD, 50);
g.setFont(fontSfProtypSub);

//产品类型字符串
String subSfProTypStr = orderParam.getExpressTyp();
g.drawString(subSfProTypStr, startWidth + 850, startHeight + 220);

//目的地栏的绘制
g.drawLine(startWidth, startHeight + 563, startWidth + 1198, startHeight + 563);
g.drawLine(startWidth + 80, startHeight + 396, startWidth + 80, startHeight + 563);

//目的地填写
Font fontDest = new Font(“黑体”, Font.BOLD, 30);
g.setFont(fontDest);
//目的地标题
String destTitleStr = “目的地”;
char[] destTitleArray = destTitleStr.toCharArray();
int destTitleWidth = startWidth + 40;
int destTitleHeight = startHeight + 460;
for (int i = 0; i < destTitleStr.length(); i++) {
g.drawString(String.valueOf(destTitleArray[i]), destTitleWidth, destTitleHeight);
destTitleHeight += 33;
}

//目的地代码
Font fontDestCode = new Font(“Arial”, Font.BOLD, 214);
g.setFont(fontDestCode);
//目的地代码字符串
String destCode = orderParam.getDestCode() == null ? “” : orderParam.getDestCode();
g.drawString(destCode, startWidth + 90, startHeight + 555);

//收件人表格栏
g.drawLine(startWidth, startHeight + 720, startWidth + 1198, startHeight + 720);
g.drawLine(startWidth + 80, startHeight + 563, startWidth + 80, startHeight + 720);

//设置收件人标题字体
Font fontRevicer = new Font(“黑体”, Font.BOLD, 25);
g.setFont(fontRevicer);
//收件人标题字符串
String revicerTitleStr = “收件人”;
char[] revicerTitleArray = revicerTitleStr.toCharArray();
int revicerTitleWidth = startWidth + 40;
int revicerTitleHeight = startHeight + 620;
for (int i = 0; i < revicerTitleStr.length(); i++) {
g.drawString(String.valueOf(revicerTitleArray[i]), revicerTitleWidth, revicerTitleHeight);
revicerTitleHeight += 31;
}

/*
* 收件人详细信息
* */
String dContact = orderParam.getdContact(); //收件人姓名
String dTel = orderParam.getdTel(); //联系电话
String dMoblie = orderParam.getdMobile(); //联系人手机号
String dCompany = orderParam.getdCompany(); //公司名称
String dProvince = orderParam.getdProvince();
String dCity = orderParam.getdCity();
String dCounty = orderParam.getdCounty();
String dAddress = orderParam.getdAddress(); //详细地址

String revicerInfo = dContact + ” ” + dTel + ” ” + dMoblie + ” ” + dCompany;
dAddress = dProvince + dCity + dCounty + dAddress;

//设置收件人信息字体
Font fontRevicerInfo = new Font(“黑体”, Font.BOLD, 35);
g.setFont(fontRevicerInfo);
g.drawString(revicerInfo, startWidth + 90, startHeight + 610);
//设置收件人详细地址字体
Font fontReviceAddress = new Font(“黑体”, Font.BOLD, 40);
g.setFont(fontReviceAddress);
if (dAddress.length() > 30) {
g.drawString(dAddress.substring(0, 30), startWidth + 90, startHeight + 655);
g.drawString(dAddress.substring(30, dAddress.length()), startWidth + 90, startHeight + 700);
} else {
g.drawString(dAddress, startWidth + 90, startHeight + 655);
}

//绘制寄件人表格
g.drawLine(startWidth, startHeight + 828, startWidth + 1198, startHeight +828);
g.drawLine(startWidth + 80, startHeight + 720, startWidth + 80, startHeight + 828);

//设置寄件人标题字体
Font fontSender = new Font(“黑体”, Font.BOLD, 25);
g.setFont(fontSender);
//寄件人标题字符串
String senderTitleStr = “寄件人”;
char[] senderTitleArray = senderTitleStr.toCharArray();
int senderTitleWidth = startWidth + 40;
int senderTitleHeight = startHeight + 752;
for (int i = 0; i < senderTitleStr.length(); i++) {
g.drawString(String.valueOf(senderTitleArray[i]), senderTitleWidth, senderTitleHeight);
senderTitleHeight += 27;
}

/*
* 寄件人信息
* **/
String jContact = orderParam.getjContact(); //寄件人姓名
String jTel = orderParam.getjTel(); //寄件人联系电话
String jMobile = orderParam.getjMobile();
String jCompany = orderParam.getjCompany();
String jProvince = orderParam.getjProvince();
String jCity = orderParam.getjCity();
String jCounty = orderParam.getjCounty();
String jAddress = orderParam.getjAddress();

String senderInfo = jContact + ” ” + jTel + ” ” + jMobile + ” ” + jCompany;
jAddress = jProvince + jCity + jCounty + jAddress;

//设置寄件人信息字体
Font fontSenderInfo = new Font(“黑体”, Font.PLAIN, 30);
g.setFont(fontSenderInfo);
g.drawString(senderInfo, startWidth + 90, startHeight + 752);

//设置寄件人详细地址字体
Font fontSenderAddress = new Font(“黑体”, Font.PLAIN, 30);
g.setFont(fontSenderAddress);
if (jAddress.length() > 27) {
g.drawString(jAddress.substring(0, 27), startWidth + 90, startHeight + 785);
g.drawString(jAddress.substring(27, jAddress.length()), startWidth + 90, startHeight + 817);
} else {
g.drawString(jAddress, startWidth + 90, startHeight + 785);
}

//绘制派送方式表格
g.drawLine(startWidth + 850, startHeight + 720, startWidth + 850, startHeight + 1080);
//设置派送类型字体
Font fontSenTyp = new Font(“黑体”, Font.BOLD, 55);
g.setFont(fontSenTyp);

//快递详细信息表格
g.drawLine(startWidth, startHeight + 972, startWidth + 850, startHeight + 972);

//快递详细信息
Font cellFont = new Font(“黑体”, Font.PLAIN, 22);
g.setFont(cellFont);
String[][] cellValue1 = {
{“付款方式:”,orderParam.getPayMethod()},
{“月结账号:”,orderParam.getMonthSettleNo()},
{“第三方地区:”,orderParam.getThridArea()},
{“实际重量:”,orderParam.getRealWeight()}
};
int cellLineHeight = startHeight + 855;
for (int i = 0; i < cellValue1.length; i++) {
g.drawString(cellValue1[i][0], startWidth + 30, cellLineHeight);
g.drawString(cellValue1[i][1], startWidth + 155, cellLineHeight);
cellLineHeight += 30;
}
String[][] cellValue2 = {
{“计费重量:”,orderParam.getChargWeight()},
{“声明价值:”,orderParam.getDeclarPrice()},
{“保价费用:”,orderParam.getSupportFee()},
{“定时派送:”,orderParam.getSendTime()}
};
cellLineHeight = startHeight + 855;
for (int i = 0; i < cellValue2.length; i++) {
g.drawString(cellValue2[i][0], startWidth + 355, cellLineHeight);
g.drawString(cellValue2[i][1], startWidth + 470, cellLineHeight);
cellLineHeight += 30;
}
String[][] cellValue3 = {
{“包装费用:”,orderParam.getPackFee()},
{“运费:”,orderParam.getFreight()},
{“费用合计:”,orderParam.getSumFee()}
};
cellLineHeight = startHeight + 855;
for (int i = 0; i < cellValue3.length; i++) {
g.drawString(cellValue3[i][0], startWidth + 655, cellLineHeight);
g.drawString(cellValue3[i][1], startWidth + 770, cellLineHeight);
cellLineHeight += 30;
}

//转寄协议客户
Font fowardFont = new Font(“黑体”, Font.BOLD, 25);
g.setFont(fowardFont);
String fowardStr = “转寄协议客户”;
g.drawString(fowardStr, startWidth + 693, startHeight + 965);

//托寄物
g.drawLine(startWidth + 80, startHeight + 972, startWidth + 80, startHeight + 1080);
//托寄物标题字符串
String articleTitleStr = “托寄物”;
char[] articleTitleArray = articleTitleStr.toCharArray();
int articleTitleWidth = startWidth + 40;
int articleTitleHeight = startHeight + 1005;
for (int i = 0; i < articleTitleStr.length(); i++) {
g.drawString(String.valueOf(articleTitleArray[i]), articleTitleWidth, articleTitleHeight);
articleTitleHeight += 31;
}

//收件员信息
g.drawLine(startWidth + 600, startHeight + 972, startWidth + 600, startHeight + 1080);
Font receDriverFont = new Font(“黑体”, Font.PLAIN, 25);
g.setFont(receDriverFont);
String[][] receDriverStrs = {
{“收件员:”,orderParam.getReviceDriver()},
{“寄件日期:”,orderParam.getSendDate()},
{“派件员:”,orderParam.getSendDriver()}
};
cellLineHeight = startHeight + 1005;
for (int i = 0; i < receDriverStrs.length; i++) {
g.drawString(receDriverStrs[i][0], startWidth + 610, cellLineHeight);
g.drawString(receDriverStrs[i][1], startWidth + 730, cellLineHeight);
cellLineHeight += 31;
}
//签名
String signStr = “签名:”;
g.drawString(signStr, startWidth + 860, startHeight + 860);
//月日
String monthDay = “月 日”;
g.drawString(monthDay, startWidth + 1100, startHeight + 1070);

//母单与子单分割线
g.drawLine(startWidth, startHeight + 1080, startWidth + 1198, startHeight + 1080);
//————————— 第二联信息 ———————————-//
g.drawLine(startWidth, startHeight + 1260, startWidth + 1198, startHeight + 1260);
g.drawLine(startWidth + 300, startHeight + 1080, startWidth + 300, startHeight + 1260);
//插入Logo
Image sublogoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
g.drawImage(sublogoImg, startWidth + 40, startHeight + 1085, null);
//插入第二个logo (客服电话)
Image sublogoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
g.drawImage(sublogoTelImg, startWidth + 50, startHeight + 1170, null);

//导入子单条码
Image sfSubBarImg = insertImage(picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg”, 0, 0, false);
g.drawImage(sfSubBarImg, startWidth + 400, startHeight + 1095, null);

//子单号
Font subOrderFont = new Font(“黑体”, Font.BOLD, 30);
g.setFont(subOrderFont);
g.drawString(“母单号 ” + sfBarCodeStr, startWidth + 460, startHeight + 1250);

//绘制寄件人表格 子单
g.drawLine(startWidth, startHeight + 1379, startWidth + 1198, startHeight + 1379);
g.drawLine(startWidth + 80, startHeight + 1260, startWidth + 80, startHeight + 1379);
//设置寄件人标题字体
Font fontsubSender = new Font(“黑体”, Font.BOLD, 30);
g.setFont(fontsubSender);
//寄件人标题字符串
String senderSubTitleStr = “寄件人”;
char[] senderSubTitleArray = senderSubTitleStr.toCharArray();
int senderSubTitleWidth = startWidth + 40;
int senderSubTitleHeight = startHeight + 1295;
for (int i = 0; i < senderSubTitleStr.length(); i++) {
g.drawString(String.valueOf(senderSubTitleArray[i]), senderSubTitleWidth, senderSubTitleHeight);
senderSubTitleHeight += 33;
}
Font subJInfoFont = new Font(“黑体”, Font.PLAIN, 35);
g.setFont(subJInfoFont);
g.drawString(senderInfo, startWidth + 110, startHeight + 1295);

if (jAddress.length() > 30) {
g.drawString(jAddress.substring(0, 30), startWidth + 110, startHeight + 1330);
g.drawString(jAddress.substring(30, jAddress.length()), startWidth + 110, startHeight + 1365);
} else {
g.drawString(jAddress, startWidth + 110, startHeight + 1330);
}

//绘制收件人表格 子单
g.drawLine(startWidth, startHeight + 1499, startWidth + 1198, startHeight + 1499);
g.drawLine(startWidth + 80, startHeight + 1379, startWidth + 80, startHeight + 1499);
//收件人标题字符串
Font fontsubReveicer = new Font(“黑体”, Font.BOLD, 30);
g.setFont(fontsubReveicer);
String revicerSubTitleStr = “收件人”;
char[] revicerSubTitleArray = revicerSubTitleStr.toCharArray();
int revicerSubTitleWidth = startWidth + 40;
int revicerSubTitleHeight = startHeight + 1415;
for (int i = 0; i < revicerSubTitleStr.length(); i++) {
g.drawString(String.valueOf(revicerSubTitleArray[i]), revicerSubTitleWidth, revicerSubTitleHeight);
revicerSubTitleHeight += 33;
}

Font subDInfoFont = new Font(“黑体”, Font.PLAIN, 35);
g.setFont(subDInfoFont);
g.drawString(revicerInfo, startWidth + 110, startHeight + 1415);
if (dAddress.length() > 30) {
g.drawString(dAddress.substring(0, 30), startWidth + 110, startHeight + 1450);
g.drawString(dAddress.substring(30, dAddress.length()), startWidth + 110, startHeight + 1486);
} else {
g.drawString(dAddress, startWidth + 110, startHeight + 1450);
}
g.dispose();
File sfbarFile = new File(picPath + “SFBarCoding_” + orderParam.getMailNo() + “.jpg”);
if (sfbarFile.exists() && sfbarFile.isFile()) {
sfbarFile.delete();
}
//生成订单图片
createImage(picPath + “SfOrderImage_” + orderParam.getMailNo() + “.jpg”);
if (isCompress) {
compressImg(picPath + “SfOrderImage_” + orderParam.getMailNo() + “.jpg”, imgWidth, imgHeidht);
}
logger.info(“订单生成成功. ” + picPath + “SfOrderImage_” + orderParam.getMailNo() + “.jpg”);
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* 水平翻转图像 顺时针旋转90度、左右翻转
* @param bufferedimage 目标图像
* @return
*/
private static BufferedImage rotate90DX(BufferedImage bi)
{
int width = bi.getWidth();
int height = bi.getHeight();

BufferedImage biFlip = new BufferedImage(height, width, bi.getType());

for(int i=0; i<width; i++)
for(int j=0; j<height; j++)
biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));

return biFlip;
}

/**
* 水平翻转图像 逆时针旋转90度、左右翻转
* @param bufferedimage 目标图像
* @return
*/
private static BufferedImage rotate90SX(BufferedImage bi)
{
int width = bi.getWidth();
int height = bi.getHeight();

BufferedImage biFlip = new BufferedImage(height, width, bi.getType());

for(int i=0; i<width; i++)
for(int j=0; j<height; j++)
biFlip.setRGB(j, i, bi.getRGB(i, j));

return biFlip;
}

/**
* 根据规定尺寸压缩图片
* @param imgPath 图片路径
* @param width 图片宽度
* @param height 图片高度
* */
private static void compressImg(String imgPath, int width, int height){
/**
* 设置条码图片的尺寸
* */
BufferedInputStream bis = null;
BufferedOutputStream out = null;
FileOutputStream fis = null;
try {
File sfFile = new File(imgPath);
if (sfFile.isFile() && sfFile.exists()) {
//读取图片
bis = new BufferedInputStream(new FileInputStream(imgPath));
//转换成图片对象
Image bi = ImageIO.read(bis).getScaledInstance(width, height, Image.SCALE_SMOOTH);
//构建图片流 设置图片宽和高
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//绘制改变尺寸后的图
tag.getGraphics().drawImage(bi, 0, 0,width, height, null);
//保存图片
fis = new FileOutputStream(imgPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fis);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
//设置压缩图片质量
jep.setQuality(3f, true);
encoder.encode(tag, jep);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) fis.close();
if (out != null) out.close();
if (bis != null) bis.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

//创建目录
private static boolean createDir(String destDirName){
try {
File file = new File(destDirName);
if (destDirName.endsWith(File.separator))
destDirName += File.separator;
if (file.mkdir())
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

//删除目录
public static boolean removeDir(File dir){
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
return removeDir(file);
} else {
return file.delete();
}
}
return dir.delete();
}

//删除目录
public static boolean deleteMk(String dirPath){
File dirFile = new File(dirPath);
if (dirFile.isDirectory()) {
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
}
return dirFile.delete();
}

/**
* 把文件转换为byte[]
* @param filePath 文件路径
* */
public static byte[] getFileBytes(String filePath){
byte[] buffer = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
File file = new File(filePath);
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return buffer;
}
}

 

三、生成电子面单所需参数类。

SfPrintOrderParam.java

package testNetty.wu;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* 打印订单所需要的参数
* */
public class SfPrintOrderParam implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

//陆运标识
private String earthCarryFlag = “”;
//母运单号
private String mailNo = “”;
//子运单号
private List<String> subMailNos = new ArrayList<String>();
//目的地代码
private String destCode = “”;
//产品类型
private String expressTyp = “”;
//收件人姓名
private String dContact = “”;
//收件人公司名称
private String dCompany = “”;
//收件人联系方式
private String dTel = “”;
//收件人手机号
private String dMobile = “”;
//收件人省份
private String dProvince = “”;
//收件人城市
private String dCity = “”;
//收件人区、县
private String dCounty = “”;
//收件人详细地址
private String dAddress = “”;
//寄件人姓名
private String jContact = “”;
//寄件人公司名称
private String jCompany = “”;
//寄件人联系方式
private String jTel = “”;
//寄件人手机号
private String jMobile = “”;
//寄件人省份
private String jProvince = “”;
//寄件人城市
private String jCity = “”;
//寄件人区、县
private String jCounty = “”;
//寄件人详细地址
private String jAddress = “”;
//付款方式
private String payMethod = “”;
//月结账号
private String monthSettleNo = “”;
//第三方地区
private String thridArea = “”;
//实际重量
private String realWeight = “”;
//计费重量
private String chargWeight = “”;
//声明价值
private String declarPrice = “”;
//保价费用
private String supportFee = “”;
//定时派送时间
private String sendTime = “”;
//包装费用
private String packFee = “”;
//运费
private String freight = “”;
//费用合计
private String sumFee = “”;
//收件员编号
private String reviceDriver = “”;
//寄件日期
private String sendDate = “”;
//派件员
private String sendDriver = “”;
//拖寄物信息
private String cargoContent = “”;

public SfPrintOrderParam() {
super();
// TODO Auto-generated constructor stub
}

public String getEarthCarryFlag() {
return earthCarryFlag;
}
public void setEarthCarryFlag(String earthCarryFlag) {
this.earthCarryFlag = earthCarryFlag;
}
public String getMailNo() {
return mailNo;
}
public void setMailNo(String mailNo) {
this.mailNo = mailNo;
}
public List<String> getSubMailNos() {
return subMailNos;
}
public String getExpressTyp() {
return expressTyp;
}

public void setExpressTyp(String expressTyp) {
this.expressTyp = expressTyp;
}

public void setSubMailNos(List<String> subMailNos) {
this.subMailNos = subMailNos;
}
public String getDestCode() {
return destCode;
}
public void setDestCode(String destCode) {
this.destCode = destCode;
}
public String getdContact() {
return dContact;
}
public void setdContact(String dContact) {
this.dContact = dContact;
}
public String getdCompany() {
return dCompany;
}
public void setdCompany(String dCompany) {
this.dCompany = dCompany;
}
public String getdTel() {
return dTel;
}
public void setdTel(String dTel) {
this.dTel = dTel;
}
public String getdMobile() {
return dMobile;
}
public void setdMobile(String dMobile) {
this.dMobile = dMobile;
}
public String getdProvince() {
return dProvince;
}
public void setdProvince(String dProvince) {
this.dProvince = dProvince;
}
public String getdCity() {
return dCity;
}
public void setdCity(String dCity) {
this.dCity = dCity;
}
public String getdCounty() {
return dCounty;
}
public void setdCounty(String dCounty) {
this.dCounty = dCounty;
}
public String getdAddress() {
return dAddress;
}
public void setdAddress(String dAddress) {
this.dAddress = dAddress;
}
public String getjContact() {
return jContact;
}
public void setjContact(String jContact) {
this.jContact = jContact;
}
public String getjCompany() {
return jCompany;
}
public void setjCompany(String jCompany) {
this.jCompany = jCompany;
}
public String getjTel() {
return jTel;
}
public void setjTel(String jTel) {
this.jTel = jTel;
}
public String getjMobile() {
return jMobile;
}
public void setjMobile(String jMobile) {
this.jMobile = jMobile;
}
public String getjProvince() {
return jProvince;
}
public void setjProvince(String jProvince) {
this.jProvince = jProvince;
}
public String getjCity() {
return jCity;
}
public void setjCity(String jCity) {
this.jCity = jCity;
}
public String getjCounty() {
return jCounty;
}
public void setjCounty(String jCounty) {
this.jCounty = jCounty;
}
public String getjAddress() {
return jAddress;
}
public void setjAddress(String jAddress) {
this.jAddress = jAddress;
}
public String getPayMethod() {
return payMethod;
}
public void setPayMethod(String payMethod) {
this.payMethod = payMethod;
}
public String getMonthSettleNo() {
return monthSettleNo;
}
public void setMonthSettleNo(String monthSettleNo) {
this.monthSettleNo = monthSettleNo;
}
public String getThridArea() {
return thridArea;
}
public void setThridArea(String thridArea) {
this.thridArea = thridArea;
}
public String getRealWeight() {
return realWeight;
}
public void setRealWeight(String realWeight) {
this.realWeight = realWeight;
}
public String getChargWeight() {
return chargWeight;
}
public void setChargWeight(String chargWeight) {
this.chargWeight = chargWeight;
}
public String getDeclarPrice() {
return declarPrice;
}
public void setDeclarPrice(String declarPrice) {
this.declarPrice = declarPrice;
}
public String getSupportFee() {
return supportFee;
}
public void setSupportFee(String supportFee) {
this.supportFee = supportFee;
}
public String getSendTime() {
return sendTime;
}
public void setSendTime(String sendTime) {
this.sendTime = sendTime;
}
public String getPackFee() {
return packFee;
}
public void setPackFee(String packFee) {
this.packFee = packFee;
}
public String getFreight() {
return freight;
}
public void setFreight(String freight) {
this.freight = freight;
}
public String getSumFee() {
return sumFee;
}
public void setSumFee(String sumFee) {
this.sumFee = sumFee;
}
public String getReviceDriver() {
return reviceDriver;
}
public void setReviceDriver(String reviceDriver) {
this.reviceDriver = reviceDriver;
}
public String getSendDate() {
return sendDate;
}
public void setSendDate(String sendDate) {
this.sendDate = sendDate;
}
public String getSendDriver() {
return sendDriver;
}
public void setSendDriver(String sendDriver) {
this.sendDriver = sendDriver;
}

public String getCargoContent() {
return cargoContent;
}
public void setCargoContent(String cargoContent) {
this.cargoContent = cargoContent;
}

}

 

四、测试运行此程序

package testNetty.wu;

/**
*
*/
public class App
{
public static void main( String[] args )
{
SfPrintOrderParam param = new SfPrintOrderParam();

param.setMailNo(“883987638272”);
param.setEarthCarryFlag(“E”);
param.setDestCode(“0311”);
param.setdContact(“李XX”);
param.setdCompany(“河北北国商城”);
param.setdMobile(“13588911223”);
param.setdProvince(“河北省”);
param.setdCity(“石家庄市”);
param.setdCounty(“桥西区”);
param.setdAddress(“华润万象城”);
param.setjContact(“吴XX”);
param.setjMobile(“13716533121”);
param.setjCompany(“北京XXX科技公司”);
param.setjProvince(“北京”);
param.setjCity(“北京市”);
param.setjCounty(“朝阳区”);
param.setjCounty(“东三环24号XXX大厦”);

SFOrderGenerateUtil.generateOrders(
“C:\\Users\\Administrator\\Desktop\\”,
param,
“1”,
false,
600,
400);
}
}

五、生成电子面单图片效果 如图:

JAVA JDK低版本解决方案http://www.yinzhongnet.com/242.html

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!