最近公司需要开发一个公司内部使用的快递下单系统,给我的开发任务中有一个生成电子面单功能,为了下单时更方便,利用此功能使用快递公司给我们的打印机直接打印出电子面单,刚接到这个任务时我想这应该很简单,不就是做一个表格打印出来吗,原本以为使用excel或者word等工具直接生成一个文档,后来经理说不用excel和word工具,让用Java直接生成电子面单,刚开始有点懵,因为不知道Java还有绘图功能,因此在网上学习了一下Java怎样绘图,索性直接开干。
废话不多说直接上代码。
一、 首先是生成条码工具类,此类是生成快递单号条码。
SFBarCodeGenerateUtil.java
- 1 package testNetty.wu;
- 2
- 3 import java.awt.Color;
- 4 import java.awt.Graphics;
- 5 import java.awt.Image;
- 6 import java.awt.image.BufferedImage;
- 7 import java.io.BufferedInputStream;
- 8 import java.io.BufferedOutputStream;
- 9 import java.io.File;
- 10 import java.io.FileInputStream;
- 11 import java.io.FileNotFoundException;
- 12 import java.io.FileOutputStream;
- 13 import java.io.OutputStream;
- 14 import java.util.ArrayList;
- 15 import java.util.List;
- 16 import java.util.regex.Pattern;
- 17
- 18 import javax.imageio.ImageIO;
- 19
- 20 import org.apache.log4j.Logger;
- 21
- 22
- 23 import com.sun.image.codec.jpeg.JPEGCodec;
- 24 import com.sun.image.codec.jpeg.JPEGImageEncoder;
- 25
- 26 /**
- 27 * 顺丰速运条码生成工具<br/>
- 28 * 采用 code128c编码规则<br/>
- 29 * <pre>
- 30 * CODE128C:[00]-[99]的数字对集合,共100个
- 31 * 即只能表示偶数位长度的数字
- 32 * </pre>
- 33 * @author wu
- 34 * @version 1.0
- 35 * @Time 2017/03/29
- 36 * */
- 37 public class SFBarCodeGenerateUtil {
- 38
- 39 private static final Logger logger = Logger.getLogger(SFBarCodeGenerateUtil.class.getSimpleName());
- 40
- 41 /**图片格式 jpg 格式*/
- 42 public static final String PICTURE_JPG = "JPG";
- 43 /**图片格式 png 格式*/
- 44 public static final String PICTURE_PNG = "PNG";
- 45 /**图片格式 gif 格式*/
- 46 public static final String PICTURE_GIF = "GIF";
- 47
- 48 /** code128编码字符集 二维数组*/
- 49 private static String[][] code128 = {
- 50 { " ", " ", "00", "212222", "11011001100" },
- 51 { "!", "!", "01", "222122", "11001101100" },
- 52 { "\"", "\"", "02", "222221", "11001100110" },
- 53 { "#", "#", "03", "121223", "10010011000" },
- 54 { "$", "$", "04", "121322", "10010001100" },
- 55 { "%", "%", "05", "131222", "10001001100" },
- 56 { "&", "&", "06", "122213", "10011001000" },
- 57 { "'", "'", "07", "122312", "10011000100" },
- 58 { "(", "(", "08", "132212", "10001100100" },
- 59 { ")", ")", "09", "221213", "11001001000" },
- 60 { "*", "*", "10", "221312", "11001000100" },
- 61 { "+", "+", "11", "231212", "11000100100" },
- 62 { ",", ",", "12", "112232", "10110011100" },
- 63 { "-", "-", "13", "122132", "10011011100" },
- 64 { ".", ".", "14", "122231", "10011001110" },
- 65 { "/", "/", "15", "113222", "10111001100" },
- 66 { "0", "0", "16", "123122", "10011101100" },
- 67 { "1", "1", "17", "123221", "10011100110" },
- 68 { "2", "2", "18", "223211", "11001110010" },
- 69 { "3", "3", "19", "221132", "11001011100" },
- 70 { "4", "4", "20", "221231", "11001001110" },
- 71 { "5", "5", "21", "213212", "11011100100" },
- 72 { "6", "6", "22", "223112", "11001110100" },
- 73 { "7", "7", "23", "312131", "11101101110" },
- 74 { "8", "8", "24", "311222", "11101001100" },
- 75 { "9", "9", "25", "321122", "11100101100" },
- 76 { ":", ":", "26", "321221", "11100100110" },
- 77 { ";", ";", "27", "312212", "11101100100" },
- 78 { "<", "<", "28", "322112", "11100110100" },
- 79 { "=", "=", "29", "322211", "11100110010" },
- 80 { ">", ">", "30", "212123", "11011011000" },
- 81 { "?", "?", "31", "212321", "11011000110" },
- 82 { "@", "@", "32", "232121", "11000110110" },
- 83 { "A", "A", "33", "111323", "10100011000" },
- 84 { "B", "B", "34", "131123", "10001011000" },
- 85 { "C", "C", "35", "131321", "10001000110" },
- 86 { "D", "D", "36", "112313", "10110001000" },
- 87 { "E", "E", "37", "132113", "10001101000" },
- 88 { "F", "F", "38", "132311", "10001100010" },
- 89 { "G", "G", "39", "211313", "11010001000" },
- 90 { "H", "H", "40", "231113", "11000101000" },
- 91 { "I", "I", "41", "231311", "11000100010" },
- 92 { "J", "J", "42", "112133", "10110111000" },
- 93 { "K", "K", "43", "112331", "10110001110" },
- 94 { "L", "L", "44", "132131", "10001101110" },
- 95 { "M", "M", "45", "113123", "10111011000" },
- 96 { "N", "N", "46", "113321", "10111000110" },
- 97 { "O", "O", "47", "133121", "10001110110" },
- 98 { "P", "P", "48", "313121", "11101110110" },
- 99 { "Q", "Q", "49", "211331", "11010001110" },
- 100 { "R", "R", "50", "231131", "11000101110" },
- 101 { "S", "S", "51", "213113", "11011101000" },
- 102 { "T", "T", "52", "213311", "11011100010" },
- 103 { "U", "U", "53", "213131", "11011101110" },
- 104 { "V", "V", "54", "311123", "11101011000" },
- 105 { "W", "W", "55", "311321", "11101000110" },
- 106 { "X", "X", "56", "331121", "11100010110" },
- 107 { "Y", "Y", "57", "312113", "11101101000" },
- 108 { "Z", "Z", "58", "312311", "11101100010" },
- 109 { "[", "[", "59", "332111", "11100011010" },
- 110 { "\\", "\\", "60", "314111", "11101111010" },
- 111 { "]", "]", "61", "221411", "11001000010" },
- 112 { "^", "^", "62", "431111", "11110001010" },
- 113 { "_", "_", "63", "111224", "10100110000" },
- 114 { "NUL", "`", "64", "111422", "10100001100" },
- 115 { "SOH", "a", "65", "121124", "10010110000" },
- 116 { "STX", "b", "66", "121421", "10010000110" },
- 117 { "ETX", "c", "67", "141122", "10000101100" },
- 118 { "EOT", "d", "68", "141221", "10000100110" },
- 119 { "ENQ", "e", "69", "112214", "10110010000" },
- 120 { "ACK", "f", "70", "112412", "10110000100" },
- 121 { "BEL", "g", "71", "122114", "10011010000" },
- 122 { "BS", "h", "72", "122411", "10011000010" },
- 123 { "HT", "i", "73", "142112", "10000110100" },
- 124 { "LF", "j", "74", "142211", "10000110010" },
- 125 { "VT", "k", "75", "241211", "11000010010" },
- 126 { "FF", "I", "76", "221114", "11001010000" },
- 127 { "CR", "m", "77", "413111", "11110111010" },
- 128 { "SO", "n", "78", "241112", "11000010100" },
- 129 { "SI", "o", "79", "134111", "10001111010" },
- 130 { "DLE", "p", "80", "111242", "10100111100" },
- 131 { "DC1", "q", "81", "121142", "10010111100" },
- 132 { "DC2", "r", "82", "121241", "10010011110" },
- 133 { "DC3", "s", "83", "114212", "10111100100" },
- 134 { "DC4", "t", "84", "124112", "10011110100" },
- 135 { "NAK", "u", "85", "124211", "10011110010" },
- 136 { "SYN", "v", "86", "411212", "11110100100" },
- 137 { "ETB", "w", "87", "421112", "11110010100" },
- 138 { "CAN", "x", "88", "421211", "11110010010" },
- 139 { "EM", "y", "89", "212141", "11011011110" },
- 140 { "SUB", "z", "90", "214121", "11011110110" },
- 141 { "ESC", "{", "91", "412121", "11110110110" },
- 142 { "FS", "|", "92", "111143", "10101111000" },
- 143 { "GS", "},", "93", "111341", "10100011110" },
- 144 { "RS", "~", "94", "131141", "10001011110" },
- 145 { "US", "DEL", "95", "114113", "10111101000" },
- 146 { "FNC3", "FNC3", "96", "114311", "10111100010" },
- 147 { "FNC2", "FNC2", "97", "411113", "11110101000" },
- 148 { "SHIFT", "SHIFT", "98", "411311", "11110100010" },
- 149 { "CODEC", "CODEC", "99", "113141", "10111011110" },
- 150 { "CODEB", "FNC4", "CODEB", "114131", "10111101110" },
- 151 { "FNC4", "CODEA", "CODEA", "311141", "11101011110" },
- 152 { "FNC1", "FNC1", "FNC1", "411131", "11110101110" },
- 153 { "StartA", "StartA", "StartA", "211412", "11010000100" },
- 154 { "StartB", "StartB", "StartB", "211214", "11010010000" },
- 155 { "StartC", "StartC", "StartC", "211232", "11010011100" },
- 156 { "Stop", "Stop", "Stop", "2331112", "1100011101011" },
- 157 };
- 158
- 159 /**
- 160 * 生产Code128的条形码的code
- 161 * @param barCode 生成条码的数字号码
- 162 * @return
- 163 */
- 164 private static String getCode(String barCode) {
- 165 String rtnCode = "";// 返回的参数
- 166 List<Integer> rtnCodeNumb = new ArrayList<Integer>();// 2截取位的组合
- 167 int examine = 105; // 首位
- 168 // 编码不能是奇数
- 169 if (!((barCode.length() & 1) == 0))
- 170 return "";
- 171 while (barCode.length() != 0) {
- 172 int temp = 0;
- 173 try {
- 174 // Code128 编码必须为数字
- 175 temp = (Integer) Integer.valueOf(barCode.substring(0, 2));
- 176 } catch (Exception e) {
- 177 e.printStackTrace();
- 178 return "";
- 179 }
- 180 // 获得条纹
- 181 rtnCode += getValue(barCode, barCode.substring(0, 2), temp);
- 182 rtnCodeNumb.add(temp);
- 183 // 条码截取2个就需要去掉用过的前二位
- 184 barCode = barCode.substring(2);
- 185 }
- 186 if (rtnCodeNumb.size() == 0) {
- 187 return "";
- 188 }
- 189 rtnCode = getValue(examine) + rtnCode; // 获取开始位
- 190 for (int i = 0; i != rtnCodeNumb.size(); i++) {
- 191 examine += rtnCodeNumb.get(i) * (i + 1);
- 192 }
- 193 examine = examine % 103; // 获得校验位
- 194 rtnCode += getValue(examine); // 获取校验位
- 195 rtnCode += "1100011101011"; // 结束位
- 196 return rtnCode;
- 197 }
- 198
- 199 /**
- 200 * 根据编号获得条纹
- 201 *
- 202 * @param encode
- 203 * @param p_Value
- 204 * @param p_SetID
- 205 * @return
- 206 */
- 207 private static String getValue(String encode, String p_Value, int p_SetID) {
- 208 return code128[p_SetID][4];
- 209 }
- 210
- 211 /**
- 212 * 根据编号获得条纹
- 213 * @param p_CodeId
- 214 * @return
- 215 */
- 216 private static String getValue(int p_CodeId) {
- 217 return code128[p_CodeId][4];
- 218 }
- 219
- 220 // 条码的高度像素数
- 221 private static int m_nImageHeight = 40;
- 222
- 223 /**
- 224 * 生成条码
- 225 * @param barString 条码模式字符串
- 226 * @param path 生成条码图片的路径
- 227 */
- 228 private static boolean kiCode128C(String barString, String path) {
- 229 OutputStream out = null;
- 230 try {
- 231 File myPNG = new File(path);
- 232 out = new FileOutputStream(myPNG);
- 233 int nImageWidth = 0;
- 234 char[] cs = barString.toCharArray();
- 235 for (int i = 0; i != cs.length; i++) {
- 236 nImageWidth = cs.length;
- 237 }
- 238 BufferedImage bi = new BufferedImage(nImageWidth, m_nImageHeight,
- 239 BufferedImage.TYPE_INT_RGB);
- 240 Graphics g = bi.getGraphics();
- 241 for (int i = 0; i < cs.length; i++) {
- 242 if ("1".equals(cs[i] + "")) {
- 243 g.setColor(Color.BLACK);
- 244 g.fillRect(i, 0, 1, m_nImageHeight);
- 245 } else {
- 246 g.setColor(Color.WHITE);
- 247 g.fillRect(i, 0, 1, m_nImageHeight);
- 248 }
- 249 }
- 250 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- 251 encoder.encode(bi);
- 252 return true;
- 253 } catch (FileNotFoundException fe) {
- 254 logger.error("系统找不到指定路径!!" + path, fe);
- 255 return false;
- 256 } catch (Exception e) {
- 257 e.printStackTrace();
- 258 } finally {
- 259 try {
- 260 if (out != null)
- 261 out.close();
- 262 } catch (Exception e2) {
- 263 e2.printStackTrace();
- 264 }
- 265 }
- 266 return false;
- 267 }
- 268
- 269 /**
- 270 * 生成条形码<br/>
- 271 * 条码是图片格式(JPG、PNG、GIF)
- 272 * @param wayBillNo
- 273 * 运单号
- 274 * @param generatePathName
- 275 * 生成条形码路径及名称
- 276 * @param width
- 277 * 条码图片宽度
- 278 * @param height
- 279 * 条码图片高度
- 280 * @param picTyp
- 281 * 图片格式<br/>
- 282 * JPG、PNG、GIF三种图片类型选择
- 283 * @return boolean类型值
- 284 * true 生成成功,false 生成失败
- 285 * */
- 286 public static boolean generateBarCode(String wayBillNo, String generatePathName, int width,
- 287 int height, String picTyp){
- 288 //只能是数字
- 289 if (!Pattern.matches("[0-9]+", wayBillNo)) {
- 290 logger.error("生成CODE128C条码只能是0~9的数字不能有其他字符!!");
- 291 //运单号长度只能是偶数
- 292 } else if (wayBillNo.length() % 2 != 0) {
- 293 logger.error("生成CODE128C条码的长度只能是偶数!!");
- 294 //生成条码
- 295 } else if (kiCode128C(getCode(wayBillNo), generatePathName)){
- 296 /**
- 297 * 设置条码图片的尺寸
- 298 * */
- 299 BufferedInputStream bis = null;
- 300 BufferedOutputStream out = null;
- 301 try {
- 302 File sfFile = new File(generatePathName);
- 303 if (sfFile.isFile() && sfFile.exists()) {
- 304 //读取图片
- 305 bis = new BufferedInputStream(new FileInputStream(generatePathName));
- 306 //转换成图片对象
- 307 Image bi = ImageIO.read(bis);
- 308 //构建图片流 设置图片宽和高
- 309 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- 310 //绘制改变尺寸后的图
- 311 tag.getGraphics().drawImage(bi, 0, 0,width, height, null);
- 312 //保存图片
- 313 out = new BufferedOutputStream(new FileOutputStream(generatePathName));
- 314 ImageIO.write(tag, picTyp,out);
- 315 out.flush();
- 316 }
- 317 } catch (Exception e) {
- 318 e.printStackTrace();
- 319 } finally {
- 320 try {
- 321 if (out != null)
- 322 out.close();
- 323 if (bis != null)
- 324 bis.close();
- 325 } catch (Exception e2) {
- 326 e2.printStackTrace();
- 327 }
- 328 }
- 329 logger.info("条码生成成功. " + generatePathName + " 像素:" + width + "*" + height);
- 330 return true;
- 331 }
- 332 logger.error("条码生成失败!");
- 333 return false;
- 334 }
- 335
- 336 }
二、其次是生成电子面单表格类,包含条码、单号、寄件人和收件人信息。
SFOrderGenerateUtil.java
- 1 package testNetty.wu;
- 2
- 3 import java.awt.BasicStroke;
- 4 import java.awt.Color;
- 5 import java.awt.Font;
- 6 import java.awt.Graphics2D;
- 7 import java.awt.Image;
- 8 import java.awt.RenderingHints;
- 9 import java.awt.image.BufferedImage;
- 10 import java.io.BufferedInputStream;
- 11 import java.io.BufferedOutputStream;
- 12 import java.io.ByteArrayOutputStream;
- 13 import java.io.File;
- 14 import java.io.FileInputStream;
- 15 import java.io.FileNotFoundException;
- 16 import java.io.FileOutputStream;
- 17 import java.io.IOException;
- 18
- 19 import javax.imageio.ImageIO;
- 20
- 21 import org.apache.commons.io.FileUtils;
- 22 import org.apache.log4j.Logger;
- 23
- 24 import com.sun.image.codec.jpeg.JPEGCodec;
- 25 import com.sun.image.codec.jpeg.JPEGEncodeParam;
- 26 import com.sun.image.codec.jpeg.JPEGImageEncoder;
- 27
- 28 /**
- 29 * 生成电子面单图片工具
- 30 * @author wu
- 31 * @version 1.0
- 32 * @time 2017/04/25
- 33 * */
- 34 public class SFOrderGenerateUtil {
- 35
- 36 private static final Logger logger = Logger.getLogger(SFOrderGenerateUtil.class.getSimpleName());
- 37
- 38 //图片的宽度
- 39 public static final int IMG_WIDTH = 1198;
- 40 //图片的宽度
- 41 public static final int IMG_HEIGHT = 1800;
- 42 //LOGO的宽度
- 43 public static final int LOGO_WIDTH = 240;
- 44 //LOGO高度
- 45 public static final int LOGO_HEIGHT = 100;
- 46 //LOGO客服电话的宽度
- 47 public static final int LOGO_TEL_WIDTH = 220;
- 48 //LOGO客服电话高度
- 49 public static final int LOGO_TEL_HEIGHT = 84;
- 50
- 51 //Logo路径
- 52 public static final String LOGO_PATH = "C:\\Users\\Administrator\\Desktop\\expressLogo\\logoSC.png";
- 53 //Logo客服电话
- 54 public static final String LOGO_TEL_PATH = "C:\\Users\\Administrator\\Desktop\\expressLogo\\sf_Tel.png";;
- 55
- 56
- 57 public static BufferedImage image;
- 58 public static void createImage(String fileLocation) {
- 59 FileOutputStream fos = null;
- 60 BufferedOutputStream bos = null;
- 61 try {
- 62 fos = new FileOutputStream(fileLocation);
- 63 bos = new BufferedOutputStream(fos);
- 64 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
- 65 encoder.encode(image);
- 66 } catch (Exception e) {
- 67 e.printStackTrace();
- 68 } finally {
- 69 try {
- 70 if (bos != null) bos.close();
- 71 if (fos != null) fos.close();
- 72 } catch (Exception e2) {
- 73 e2.printStackTrace();
- 74 }
- 75 }
- 76 }
- 77
- 78 /**
- 79 * 生成订单图片
- 80 * @param orderPath
- 81 * 生成订单存放路径
- 82 * @param printTyp
- 83 * 订单打印类型 1:A4纸打印 2:热敏纸打印
- 84 * @param orderParam
- 85 * 生成订单所需的参数对象
- 86 * @param isCompress
- 87 * 是否需要根据输入宽高压缩图片
- 88 * <pre>
- 89 * isCompress 为ture时 所输入的宽高才起作用
- 90 * </pre>
- 91 * @param imgWidth
- 92 * 图片宽度
- 93 * @param imgHeidht
- 94 * 图片高度
- 95 * @author Administrator
- 96 * @return
- 97 *
- 98 * */
- 99 public static boolean generateOrders(String orderPath, SfPrintOrderParam orderParam, String printTyp, boolean isCompress, int imgWidth, int imgHeidht){
- 100 if (null == orderParam)
- 101 return false;
- 102 int startHeight = 0; //表格的起始高度
- 103 int startWidth = 0; //表格的起始宽度
- 104 try {
- 105 if (orderParam.getSubMailNos().size() == 0 || orderParam.getSubMailNos().isEmpty()) {
- 106 generateParentOrder(orderPath, orderParam, printTyp, isCompress, imgWidth, imgHeidht);
- 107 return true;
- 108 } else {
- 109 String picPath = orderPath;
- 110 File mk = new File(picPath + orderParam.getMailNo());
- 111 if (mk.exists()){
- 112 FileUtils.deleteDirectory(mk);
- 113 }
- 114 for (int k = 0; k < orderParam.getSubMailNos().size(); k++) {
- 115 image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
- 116 Graphics2D g = image.createGraphics();
- 117 //以运单号为名称创建存放订单的目录
- 118 FileUtils.forceMkdir(mk);
- 119 picPath += orderParam.getMailNo() + "/";
- 120
- 121 //设置背景色为白色
- 122 g.setColor(Color.WHITE);
- 123 //设置颜色区域大小
- 124 g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
- 125 //提高导入Img的清晰度
- 126
- 127
- 128 /*
- 129 * 绘制表格 填充内容
- 130 * */
- 131 //表格线条的颜色
- 132 g.setColor(Color.BLACK);
- 133
- 134 //消除文本出现锯齿现象
- 135 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- 136 //g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
- 137
- 138 //表格的四个边框
- 139 g.drawLine(startWidth, startHeight, startWidth + 374, startHeight); //上边框
- 140 g.drawLine(startWidth, startHeight, startWidth, startHeight + 562); //左边框
- 141 g.drawLine(startWidth, startHeight + 562, startWidth + 375, startHeight + 562); //下边框
- 142 g.drawLine(startWidth + 374, startHeight, startWidth + 374, startHeight + 563); //右边框
- 143
- 144 //绘制表格内容 第一行
- 145 g.drawLine(startWidth, startHeight + 48, startWidth + 375, startHeight + 48);
- 146
- 147 //插入顺丰速运Logo
- 148 Image logoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
- 149 g.drawImage(logoImg, startWidth + 4, startHeight + 8, null);
- 150 //插入第二个logo (客服电话)
- 151 Image logoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
- 152 g.drawImage(logoTelImg, startWidth + 280, startHeight + 15, null);
- 153
- 154 //填写产品类型
- 155 Font fontSfTyp = new Font("黑体", Font.BOLD, 36);
- 156 g.setFont(fontSfTyp);
- 157 //产品类型字符串
- 158 String sfProTypStr = orderParam.getEarthCarryFlag();
- 159 g.drawString(sfProTypStr, startWidth + 150, startHeight + 41);
- 160
- 161 //绘制第二行
- 162 g.drawLine(startWidth, startHeight + 124, startWidth + 375, startHeight + 124);
- 163 g.drawLine(startWidth + 276, startHeight + 48, startWidth + 276, startHeight + 124);
- 164
- 165 //生成code128c 条码
- 166 SFBarCodeGenerateUtil.generateBarCode(orderParam.getMailNo(), //运单号
- 167 picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", //图片名称
- 168 262, //图片宽度
- 169 45, //图片高度
- 170 SFBarCodeGenerateUtil.PICTURE_JPG);
- 171 //导入条码图片
- 172 Image sfBarImg = insertImage(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", 0, 0, false);
- 173 g.drawImage(sfBarImg, startWidth + 7, startHeight + 53, null);
- 174 sfBarImg.flush();
- 175
- 176 //设置字体
- 177 Font fontSfBarCode = new Font("黑体",Font.BOLD,10);
- 178 g.setFont(fontSfBarCode);
- 179
- 180 String pageNum = (k + 1) + "/" + orderParam.getSubMailNos().size();
- 181 g.drawString(pageNum, startWidth + 7, startHeight + 108);
- 182
- 183 //子单号
- 184 String subBarCodeStr = orderParam.getSubMailNos().get(k);
- 185 subBarCodeStr = subBarCodeStr.replaceAll("(.{3})", "$1 ");
- 186 g.drawString("子单号 " + subBarCodeStr, startWidth + 80, startHeight + 108);
- 187
- 188 //条码字符串(条码母单号)
- 189 String sfBarCodeStr = orderParam.getMailNo();
- 190 sfBarCodeStr = sfBarCodeStr.replaceAll("(.{3})", "$1 ");
- 191 g.drawString("母单号 " + sfBarCodeStr, startWidth + 80, startHeight + 120);
- 192
- 193 //绘制产品类型框
- 194 g.drawLine(startWidth + 276, startHeight + 70, startWidth + 375, startHeight + 70);
- 195 Font fontSfProtypSub = new Font("黑体", Font.BOLD, 14);
- 196 g.setFont(fontSfProtypSub);
- 197
- 198 //产品类型字符串
- 199 String subSfProTypStr = orderParam.getExpressTyp();
- 200 g.drawString(subSfProTypStr, startWidth + 295, startHeight + 65);
- 201
- 202 //目的地栏的绘制
- 203 g.drawLine(startWidth, startHeight + 176, startWidth + 375, startHeight + 176);
- 204 g.drawLine(startWidth + 21, startHeight + 124, startWidth + 21, startHeight + 176);
- 205
- 206 //目的地填写
- 207 Font fontDest = new Font("黑体", Font.BOLD, 10);
- 208 g.setFont(fontDest);
- 209 //目的地标题
- 210 String destTitleStr = "目的地";
- 211 char[] destTitleArray = destTitleStr.toCharArray();
- 212 int destTitleWidth = startWidth + 6;
- 213 int destTitleHeight = startHeight + 140;
- 214 for (int i = 0; i < destTitleStr.length(); i++) {
- 215 g.drawString(String.valueOf(destTitleArray[i]), destTitleWidth, destTitleHeight);
- 216 destTitleHeight += 12;
- 217 }
- 218
- 219 //目的地代码
- 220 Font fontDestCode = new Font("Arial", Font.BOLD, 36);
- 221 g.setFont(fontDestCode);
- 222 //目的地代码字符串
- 223 String destCode = orderParam.getDestCode();
- 224 g.drawString(destCode, startWidth + 24, startHeight + 165);
- 225
- 226 //收件人表格栏
- 227 g.drawLine(startWidth, startHeight + 225, startWidth + 1198, startHeight + 225);
- 228 g.drawLine(startWidth + 21, startHeight + 176, startWidth + 21, startHeight + 225);
- 229
- 230 //设置收件人标题字体
- 231 Font fontRevicer = new Font("黑体", Font.BOLD, 10);
- 232 g.setFont(fontRevicer);
- 233 //收件人标题字符串
- 234 String revicerTitleStr = "收件人";
- 235 char[] revicerTitleArray = revicerTitleStr.toCharArray();
- 236 int revicerTitleWidth = startWidth + 6;
- 237 int revicerTitleHeight = startHeight + 192;
- 238 for (int i = 0; i < revicerTitleStr.length(); i++) {
- 239 g.drawString(String.valueOf(revicerTitleArray[i]), revicerTitleWidth, revicerTitleHeight);
- 240 revicerTitleHeight += 11;
- 241 }
- 242
- 243 /*
- 244 * 收件人详细信息
- 245 * */
- 246 String dContact = orderParam.getdContact(); //收件人姓名
- 247 String dTel = orderParam.getdTel(); //联系电话
- 248 String dMoblie = orderParam.getdMobile(); //联系人手机号
- 249 String dCompany = orderParam.getdCompany(); //公司名称
- 250 String dProvince = orderParam.getdProvince();
- 251 String dCity = orderParam.getdCity();
- 252 String dCounty = orderParam.getdCounty();
- 253 String dAddress = orderParam.getdAddress(); //详细地址
- 254
- 255 String revicerInfo = dContact + " " + dTel + " " + dMoblie + "" + dCompany;
- 256 dAddress = dProvince + dCity + dCounty + dAddress;
- 257
- 258 //设置收件人信息字体
- 259 Font fontRevicerInfo = new Font("黑体", Font.BOLD, 14);
- 260 g.setFont(fontRevicerInfo);
- 261 g.drawString(revicerInfo, startWidth + 24, startHeight + 190);
- 262 //设置收件人详细地址字体
- 263 Font fontReviceAddress = new Font("黑体", Font.BOLD, 14);
- 264 g.setFont(fontReviceAddress);
- 265 if (dAddress.length() > 23) {
- 266 g.drawString(dAddress.substring(0, 23), startWidth + 24, startHeight + 205);
- 267 g.drawString(dAddress.substring(23, dAddress.length()), startWidth + 24, startHeight + 220);
- 268 } else {
- 269 g.drawString(dAddress, startWidth + 24, startHeight + 205);
- 270 }
- 271
- 272 //绘制寄件人表格
- 273 g.drawLine(startWidth, startHeight + 259, startWidth + 375, startHeight +259);
- 274 g.drawLine(startWidth + 21, startHeight + 225, startWidth + 21, startHeight + 259);
- 275
- 276 //设置寄件人标题字体
- 277 Font fontSender = new Font("黑体", Font.BOLD, 10);
- 278 g.setFont(fontSender);
- 279 //寄件人标题字符串
- 280 String senderTitleStr = "寄件人";
- 281 char[] senderTitleArray = senderTitleStr.toCharArray();
- 282 int senderTitleWidth = startWidth + 6;
- 283 int senderTitleHeight = startHeight + 235;
- 284 for (int i = 0; i < senderTitleStr.length(); i++) {
- 285 g.drawString(String.valueOf(senderTitleArray[i]), senderTitleWidth, senderTitleHeight);
- 286 senderTitleHeight += 11;
- 287 }
- 288
- 289
- 290 /*
- 291 * 寄件人信息
- 292 * **/
- 293 String jContact = orderParam.getjContact(); //寄件人姓名
- 294 String jTel = orderParam.getjTel(); //寄件人联系电话
- 295 String jMobile = orderParam.getjMobile();
- 296 String jCompany = orderParam.getjCompany();
- 297 String jProvince = orderParam.getjProvince();
- 298 String jCity = orderParam.getjCity();
- 299 String jCounty = orderParam.getjCounty();
- 300 String jAddress = orderParam.getjAddress();
- 301
- 302 String senderInfo = jContact + " " + jTel + " " + jMobile + " " + jCompany;
- 303 jAddress = jProvince + jCity + jCounty + jAddress;
- 304
- 305 //设置寄件人信息字体
- 306 Font fontSenderInfo = new Font("黑体", Font.PLAIN, 8);
- 307 g.setFont(fontSenderInfo);
- 308 g.drawString(senderInfo, startWidth + 24, startHeight + 240);
- 309
- 310 //设置寄件人详细地址字体
- 311 Font fontSenderAddress = new Font("黑体", Font.PLAIN, 8);
- 312 g.setFont(fontSenderAddress);
- 313 if (jAddress.length() > 27) {
- 314 g.drawString(jAddress.substring(0, 27), startWidth + 24, startHeight + 250);
- 315 g.drawString(jAddress.substring(27, jAddress.length()), startWidth + 24, startHeight + 265);
- 316 } else {
- 317 g.drawString(jAddress, startWidth + 24, startHeight + 250);
- 318 }
- 319
- 320 //绘制派送方式表格
- 321 g.drawLine(startWidth + 310, startHeight + 225, startWidth + 310, startHeight + 338);
- 322 //设置派送类型字体
- 323 Font fontSenTyp = new Font("黑体", Font.BOLD, 55);
- 324 g.setFont(fontSenTyp);
- 325
- 326 //快递详细信息表格
- 327 g.drawLine(startWidth, startHeight + 304, startWidth + 310, startHeight + 304);
- 328
- 329 //快递详细信息
- 330 Font cellFont = new Font("黑体", Font.PLAIN, 9);
- 331 g.setFont(cellFont);
- 332 String[][] cellValue1 = {
- 333 {"付款方式:",orderParam.getPayMethod()},
- 334 {"月结账号:",orderParam.getMonthSettleNo()},
- 335 {"第三方地区:",orderParam.getThridArea()},
- 336 {"实际重量:",orderParam.getRealWeight()}
- 337 };
- 338 int cellLineHeight = startHeight + 268;
- 339 for (int i = 0; i < cellValue1.length; i++) {
- 340 g.drawString(cellValue1[i][0], startWidth + 3, cellLineHeight);
- 341 g.drawString(cellValue1[i][1], startWidth + 52, cellLineHeight);
- 342 cellLineHeight += 10;
- 343 }
- 344 String[][] cellValue2 = {
- 345 {"计费重量:",orderParam.getChargWeight()},
- 346 {"声明价值:",orderParam.getDeclarPrice()},
- 347 {"保价费用:",orderParam.getSupportFee()},
- 348 {"定时派送:",orderParam.getSendTime()}
- 349 };
- 350 cellLineHeight = startHeight + 268;
- 351 for (int i = 0; i < cellValue2.length; i++) {
- 352 g.drawString(cellValue2[i][0], startWidth + 100, cellLineHeight);
- 353 g.drawString(cellValue2[i][1], startWidth + 150, cellLineHeight);
- 354 cellLineHeight += 10;
- 355 }
- 356 String[][] cellValue3 = {
- 357 {"包装费用:",orderParam.getPackFee()},
- 358 {"运费:",orderParam.getFreight()},
- 359 {"费用合计:",orderParam.getSumFee()}
- 360 };
- 361 cellLineHeight = startHeight + 268;
- 362 for (int i = 0; i < cellValue3.length; i++) {
- 363 g.drawString(cellValue3[i][0], startWidth + 220, cellLineHeight);
- 364 g.drawString(cellValue3[i][1], startWidth + 270, cellLineHeight);
- 365 cellLineHeight += 10;
- 366 }
- 367
- 368 //转寄协议客户
- 369 Font fowardFont = new Font("黑体", Font.BOLD, 9);
- 370 g.setFont(fowardFont);
- 371 String fowardStr = "转寄协议客户";
- 372 g.drawString(fowardStr, startWidth + 250, startHeight + 302);
- 373
- 374 //托寄物
- 375 g.drawLine(startWidth + 21, startHeight + 304, startWidth + 21, startHeight + 338);
- 376 //托寄物标题字符串
- 377 String articleTitleStr = "托寄物";
- 378 char[] articleTitleArray = articleTitleStr.toCharArray();
- 379 int articleTitleWidth = startWidth + 6;
- 380 int articleTitleHeight = startHeight + 315;
- 381 for (int i = 0; i < articleTitleStr.length(); i++) {
- 382 g.drawString(String.valueOf(articleTitleArray[i]), articleTitleWidth, articleTitleHeight);
- 383 articleTitleHeight += 10;
- 384 }
- 385
- 386 Font cargoFont = new Font("黑体", Font.PLAIN, 8);
- 387 g.setFont(cargoFont);
- 388 String cargoContent = orderParam.getCargoContent();
- 389 g.drawString(cargoContent, startWidth + 24, 315);
- 390
- 391 //收件员信息
- 392 g.drawLine(startWidth + 220, startHeight + 304, startWidth + 220, startHeight + 338);
- 393 Font receDriverFont = new Font("黑体", Font.PLAIN, 8);
- 394 g.setFont(receDriverFont);
- 395 String[][] receDriverStrs = {
- 396 {"收件员:",orderParam.getReviceDriver()},
- 397 {"寄件日期:",orderParam.getSendDate()},
- 398 {"派件员:",orderParam.getSendDriver()}
- 399 };
- 400 cellLineHeight = startHeight + 315;
- 401 for (int i = 0; i < receDriverStrs.length; i++) {
- 402 g.drawString(receDriverStrs[i][0], startWidth + 225, cellLineHeight);
- 403 g.drawString(receDriverStrs[i][1], startWidth + 265, cellLineHeight);
- 404 cellLineHeight += 10;
- 405 }
- 406 //签名
- 407 String signStr = "签名:";
- 408 g.drawString(signStr, startWidth + 312, startHeight + 268);
- 409 //月日
- 410 String monthDay = "月 日";
- 411 g.drawString(monthDay, startWidth + 345, startHeight + 336);
- 412
- 413 //母单与子单分割线
- 414 g.drawLine(startWidth, startHeight + 338, startWidth + 375, startHeight + 338);
- 415
- 416 //--------------------------- 如果有子单号 生成子单信息 ----------------------------------//
- 417 g.drawLine(startWidth, startHeight + 394, startWidth + 375, startHeight + 394);
- 418 g.drawLine(startWidth + 100, startHeight + 338, startWidth + 100, startHeight + 394);
- 419 //插入Logo
- 420 Image sublogoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
- 421 g.drawImage(sublogoImg, startWidth + 10, startHeight + 338, null);
- 422 //插入第二个logo (客服电话)
- 423 Image sublogoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
- 424 g.drawImage(sublogoTelImg, startWidth + 15, startHeight + 368, null);
- 425
- 426 //导入子单条码
- 427 //生成code128c 条码
- 428 SFBarCodeGenerateUtil.generateBarCode(orderParam.getSubMailNos().get(k), //运单号
- 429 picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg", //图片名称
- 430 169, //图片宽度
- 431 37, //图片高度
- 432 SFBarCodeGenerateUtil.PICTURE_JPG);
- 433 Image sfSubBarImg = insertImage(picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg", 0, 0, false);
- 434 g.drawImage(sfSubBarImg, startWidth + 150, startHeight + 342, null);
- 435 sfSubBarImg.flush();
- 436
- 437 //子单号
- 438 Font subOrderFont = new Font("黑体", Font.BOLD, 10);
- 439 g.setFont(subOrderFont);
- 440 g.drawString("子单号: " + subBarCodeStr, startWidth + 160, startHeight + 390);
- 441
- 442 //绘制寄件人表格 子单
- 443 g.drawLine(startWidth, startHeight + 431, startWidth + 375, startHeight + 431);
- 444 g.drawLine(startWidth + 21, startHeight + 394, startWidth + 21, startHeight + 431);
- 445 //设置寄件人标题字体
- 446 Font fontsubSender = new Font("黑体", Font.BOLD, 10);
- 447 g.setFont(fontsubSender);
- 448 //寄件人标题字符串
- 449 String senderSubTitleStr = "寄件人";
- 450 char[] senderSubTitleArray = senderSubTitleStr.toCharArray();
- 451 int senderSubTitleWidth = startWidth + 6;
- 452 int senderSubTitleHeight = startHeight + 408;
- 453 for (int i = 0; i < senderSubTitleStr.length(); i++) {
- 454 g.drawString(String.valueOf(senderSubTitleArray[i]), senderSubTitleWidth, senderSubTitleHeight);
- 455 senderSubTitleHeight += 10;
- 456 }
- 457 Font subJInfoFont = new Font("黑体", Font.PLAIN, 8);
- 458 g.setFont(subJInfoFont);
- 459 g.drawString(senderInfo, startWidth + 24, startHeight + 405);
- 460
- 461 if (jAddress.length() > 30) {
- 462 g.drawString(jAddress.substring(0, 30), startWidth + 24, startHeight + 415);
- 463 g.drawString(jAddress.substring(30, jAddress.length()), startWidth + 24, startHeight + 425);
- 464 } else {
- 465 g.drawString(jAddress, startWidth + 24, startHeight + 415);
- 466 }
- 467
- 468 //绘制收件人表格 子单
- 469 g.drawLine(startWidth, startHeight + 469, startWidth + 375, startHeight + 469);
- 470 g.drawLine(startWidth + 21, startHeight + 394, startWidth + 21, startHeight + 469);
- 471 //收件人标题字符串
- 472 Font fontsubReveicer = new Font("黑体", Font.BOLD, 10);
- 473 g.setFont(fontsubReveicer);
- 474 String revicerSubTitleStr = "收件人";
- 475 char[] revicerSubTitleArray = revicerSubTitleStr.toCharArray();
- 476 int revicerSubTitleWidth = startWidth + 6;
- 477 int revicerSubTitleHeight = startHeight + 445;
- 478 for (int i = 0; i < revicerSubTitleStr.length(); i++) {
- 479 g.drawString(String.valueOf(revicerSubTitleArray[i]), revicerSubTitleWidth, revicerSubTitleHeight);
- 480 revicerSubTitleHeight += 10;
- 481 }
- 482
- 483 Font subDInfoFont = new Font("黑体", Font.PLAIN, 8);
- 484 g.setFont(subDInfoFont);
- 485 g.drawString(revicerInfo, startWidth + 24, startHeight + 442);
- 486 if (dAddress.length() > 35) {
- 487 g.drawString(dAddress.substring(0, 35), startWidth + 24, startHeight + 452);
- 488 g.drawString(dAddress.substring(35, dAddress.length()), startWidth + 24, startHeight + 462);
- 489 } else {
- 490 g.drawString(dAddress, startWidth + 24, startHeight + 452);
- 491 }
- 492 createImage(picPath + "SfOrderImage_" + orderParam.getMailNo() + "_" + (k + 1) + ".jpg");
- 493 //如果需要需要根据自定义尺寸压缩图片
- 494 if (isCompress) {
- 495 //压缩图片
- 496 compressImg(picPath + "SfOrderImage_" + orderParam.getMailNo() + "_" + (k + 1) + ".jpg", imgWidth, imgHeidht);
- 497 }
- 498 g.dispose();
- 499 File sfSubFile = new File(picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg");
- 500 if (sfSubFile.exists() && sfSubFile.isFile()) {
- 501 if (!sfSubFile.delete()){
- 502 logger.error("删除" + picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg 失败!");
- 503 }
- 504 }
- 505 File sfbarFile = new File(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg");
- 506 if (sfbarFile.exists() && sfbarFile.isFile()) {
- 507 if (!sfbarFile.delete()) {
- 508 logger.error("删除" + picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg 失败!");
- 509 }
- 510 }
- 511 logger.error("订单生成成功. " + picPath + "SfOrderImage_" + orderParam.getMailNo() + "_" + (k + 1) + ".jpg");
- 512 picPath = orderPath;
- 513 }
- 514 }
- 515 return true;
- 516
- 517 } catch (IOException e) {
- 518 e.printStackTrace();
- 519 } catch (Exception e) {
- 520 e.printStackTrace();
- 521 }
- 522 return false;
- 523 }
- 524
- 525 /**
- 526 * 插入图片 自定义图片的宽高
- 527 * @param imgPath
- 528 * 插入图片的路径
- 529 * @param imgWidth
- 530 * 设置图片的宽度
- 531 * @param imgHeight
- 532 * 设置图片的高度
- 533 * @param isCompress
- 534 * 是否按输入的宽高定义图片的尺寸,只有为true时 输入的宽度和高度才起作用<br/>
- 535 * 为false时输入的宽高不起作用,按输入图片的默认尺寸
- 536 * @return
- 537 * @throws Exception
- 538 * */
- 539 private static Image insertImage(String imgPath, int imgWidth, int imgHeight, boolean isCompress) throws Exception {
- 540 File fileimage = new File(imgPath);
- 541 Image src = ImageIO.read(fileimage);
- 542 if (isCompress) {
- 543 Image image = src.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH);
- 544 return image;
- 545 }
- 546 return src;
- 547 }
- 548
- 549 /**
- 550 * 生成母订单
- 551 * @param orderParam 生成订单所需的参数对象
- 552 * @return boolean
- 553 * */
- 554 private static boolean generateParentOrder(String orderPath, SfPrintOrderParam orderParam, String printTyp, boolean isCompress, int imgWidth, int imgHeidht){
- 555 if (null == orderParam)
- 556 return false;
- 557 String picPath = orderPath;
- 558 int startHeight = 0; //表格的起始高度
- 559 int startWidth = 0; //表格的起始宽度
- 560 try {
- 561 if (orderParam.getSubMailNos().size() == 0 || orderParam.getSubMailNos().isEmpty()) {
- 562 image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
- 563 Graphics2D g = image.createGraphics();
- 564
- 565 //以运单号为名称创建存放订单的目录
- 566 File mk = new File(picPath + orderParam.getMailNo());
- 567 if (mk.exists()) {
- 568 FileUtils.deleteDirectory(mk);
- 569 }
- 570 FileUtils.forceMkdir(mk);
- 571
- 572 picPath += orderParam.getMailNo() + "/";
- 573
- 574 //设置背景色为白色
- 575 g.setColor(Color.WHITE);
- 576 //设置颜色区域大小
- 577 g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
- 578
- 579 /*
- 580 * 绘制表格 填充内容
- 581 * */
- 582 //表格线条的颜色
- 583 g.setColor(Color.BLACK);
- 584 //边框加粗
- 585 g.setStroke(new BasicStroke(2.0f));
- 586 //消除文本出现锯齿现象
- 587 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- 588 //表格的四个边框
- 589 g.drawLine(startWidth, startHeight, startWidth + 1198, startHeight); //上边框
- 590
- 591 g.drawLine(startWidth, startHeight, startWidth, startHeight + 1800); //左边框
- 592 g.drawLine(startWidth, startHeight + 1799, startWidth + 1198, startHeight + 1799); //下边框
- 593 g.drawLine(startWidth + 1197, startHeight, startWidth + 1197, startHeight + 1800); //右边框
- 594
- 595 //绘制表格内容 第一行
- 596 g.drawLine(startWidth, startHeight + 155, startWidth + 1198, startHeight + 155);
- 597
- 598 //A4纸打印是才有Logo
- 599 if (Integer.valueOf(printTyp) == ExpressConstant.ORDER_PRINT_TYP_A4) {
- 600 //插入Logo
- 601 Image logoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
- 602 g.drawImage(logoImg, startWidth + 30, startHeight + 30, null);
- 603 //插入第二个logo (客服电话)
- 604 Image logoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
- 605 g.drawImage(logoTelImg, startWidth + 920, startHeight + 30, null);
- 606 }
- 607
- 608 //E路标识
- 609 Font fontSfTyp = new Font("微软雅黑", Font.BOLD, 145);
- 610 g.setFont(fontSfTyp);
- 611 //E路标识
- 612 String sfProTypStr = orderParam.getEarthCarryFlag();
- 613 g.drawString(sfProTypStr, startWidth + 445, startHeight + 130);
- 614
- 615 //绘制第二行
- 616 g.drawLine(startWidth, startHeight + 396, startWidth + 1198, startHeight + 396);
- 617 g.drawLine(startWidth + 750, startHeight + 155, startWidth + 750, startHeight + 396);
- 618
- 619 //生成code128c 条码
- 620 SFBarCodeGenerateUtil.generateBarCode(orderParam.getMailNo(), //运单号
- 621 picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", //图片名称
- 622 600, //图片宽度
- 623 120, //图片高度
- 624 SFBarCodeGenerateUtil.PICTURE_JPG);
- 625 //导入条码图片
- 626 Image sfBarImg = insertImage(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", 0, 0, false);
- 627 g.drawImage(sfBarImg, startWidth + 80, startHeight + 191, null);
- 628
- 629 //设置字体
- 630 Font fontSfBarCode = new Font("黑体",Font.BOLD,35);
- 631 g.setFont(fontSfBarCode);
- 632
- 633 String sfBarCodeStr = orderParam.getMailNo();
- 634 sfBarCodeStr = sfBarCodeStr.replaceAll("(.{3})", "$1 ");
- 635 g.drawString("母单号 " + sfBarCodeStr, startWidth + 150, startHeight + 355);
- 636
- 637 //绘制产品类型框
- 638 g.drawLine(startWidth + 750, startHeight + 240, startWidth + 1198, startHeight + 240);
- 639 Font fontSfProtypSub = new Font("黑体", Font.BOLD, 50);
- 640 g.setFont(fontSfProtypSub);
- 641
- 642 //产品类型字符串
- 643 String subSfProTypStr = orderParam.getExpressTyp();
- 644 g.drawString(subSfProTypStr, startWidth + 850, startHeight + 220);
- 645
- 646 //目的地栏的绘制
- 647 g.drawLine(startWidth, startHeight + 563, startWidth + 1198, startHeight + 563);
- 648 g.drawLine(startWidth + 80, startHeight + 396, startWidth + 80, startHeight + 563);
- 649
- 650 //目的地填写
- 651 Font fontDest = new Font("黑体", Font.BOLD, 30);
- 652 g.setFont(fontDest);
- 653 //目的地标题
- 654 String destTitleStr = "目的地";
- 655 char[] destTitleArray = destTitleStr.toCharArray();
- 656 int destTitleWidth = startWidth + 40;
- 657 int destTitleHeight = startHeight + 460;
- 658 for (int i = 0; i < destTitleStr.length(); i++) {
- 659 g.drawString(String.valueOf(destTitleArray[i]), destTitleWidth, destTitleHeight);
- 660 destTitleHeight += 33;
- 661 }
- 662
- 663 //目的地代码
- 664 Font fontDestCode = new Font("Arial", Font.BOLD, 214);
- 665 g.setFont(fontDestCode);
- 666 //目的地代码字符串
- 667 String destCode = orderParam.getDestCode() == null ? "" : orderParam.getDestCode();
- 668 g.drawString(destCode, startWidth + 90, startHeight + 555);
- 669
- 670 //收件人表格栏
- 671 g.drawLine(startWidth, startHeight + 720, startWidth + 1198, startHeight + 720);
- 672 g.drawLine(startWidth + 80, startHeight + 563, startWidth + 80, startHeight + 720);
- 673
- 674 //设置收件人标题字体
- 675 Font fontRevicer = new Font("黑体", Font.BOLD, 25);
- 676 g.setFont(fontRevicer);
- 677 //收件人标题字符串
- 678 String revicerTitleStr = "收件人";
- 679 char[] revicerTitleArray = revicerTitleStr.toCharArray();
- 680 int revicerTitleWidth = startWidth + 40;
- 681 int revicerTitleHeight = startHeight + 620;
- 682 for (int i = 0; i < revicerTitleStr.length(); i++) {
- 683 g.drawString(String.valueOf(revicerTitleArray[i]), revicerTitleWidth, revicerTitleHeight);
- 684 revicerTitleHeight += 31;
- 685 }
- 686
- 687 /*
- 688 * 收件人详细信息
- 689 * */
- 690 String dContact = orderParam.getdContact(); //收件人姓名
- 691 String dTel = orderParam.getdTel(); //联系电话
- 692 String dMoblie = orderParam.getdMobile(); //联系人手机号
- 693 String dCompany = orderParam.getdCompany(); //公司名称
- 694 String dProvince = orderParam.getdProvince();
- 695 String dCity = orderParam.getdCity();
- 696 String dCounty = orderParam.getdCounty();
- 697 String dAddress = orderParam.getdAddress(); //详细地址
- 698
- 699 String revicerInfo = dContact + " " + dTel + " " + dMoblie + " " + dCompany;
- 700 dAddress = dProvince + dCity + dCounty + dAddress;
- 701
- 702 //设置收件人信息字体
- 703 Font fontRevicerInfo = new Font("黑体", Font.BOLD, 35);
- 704 g.setFont(fontRevicerInfo);
- 705 g.drawString(revicerInfo, startWidth + 90, startHeight + 610);
- 706 //设置收件人详细地址字体
- 707 Font fontReviceAddress = new Font("黑体", Font.BOLD, 40);
- 708 g.setFont(fontReviceAddress);
- 709 if (dAddress.length() > 30) {
- 710 g.drawString(dAddress.substring(0, 30), startWidth + 90, startHeight + 655);
- 711 g.drawString(dAddress.substring(30, dAddress.length()), startWidth + 90, startHeight + 700);
- 712 } else {
- 713 g.drawString(dAddress, startWidth + 90, startHeight + 655);
- 714 }
- 715
- 716 //绘制寄件人表格
- 717 g.drawLine(startWidth, startHeight + 828, startWidth + 1198, startHeight +828);
- 718 g.drawLine(startWidth + 80, startHeight + 720, startWidth + 80, startHeight + 828);
- 719
- 720 //设置寄件人标题字体
- 721 Font fontSender = new Font("黑体", Font.BOLD, 25);
- 722 g.setFont(fontSender);
- 723 //寄件人标题字符串
- 724 String senderTitleStr = "寄件人";
- 725 char[] senderTitleArray = senderTitleStr.toCharArray();
- 726 int senderTitleWidth = startWidth + 40;
- 727 int senderTitleHeight = startHeight + 752;
- 728 for (int i = 0; i < senderTitleStr.length(); i++) {
- 729 g.drawString(String.valueOf(senderTitleArray[i]), senderTitleWidth, senderTitleHeight);
- 730 senderTitleHeight += 27;
- 731 }
- 732
- 733
- 734 /*
- 735 * 寄件人信息
- 736 * **/
- 737 String jContact = orderParam.getjContact(); //寄件人姓名
- 738 String jTel = orderParam.getjTel(); //寄件人联系电话
- 739 String jMobile = orderParam.getjMobile();
- 740 String jCompany = orderParam.getjCompany();
- 741 String jProvince = orderParam.getjProvince();
- 742 String jCity = orderParam.getjCity();
- 743 String jCounty = orderParam.getjCounty();
- 744 String jAddress = orderParam.getjAddress();
- 745
- 746 String senderInfo = jContact + " " + jTel + " " + jMobile + " " + jCompany;
- 747 jAddress = jProvince + jCity + jCounty + jAddress;
- 748
- 749 //设置寄件人信息字体
- 750 Font fontSenderInfo = new Font("黑体", Font.PLAIN, 30);
- 751 g.setFont(fontSenderInfo);
- 752 g.drawString(senderInfo, startWidth + 90, startHeight + 752);
- 753
- 754 //设置寄件人详细地址字体
- 755 Font fontSenderAddress = new Font("黑体", Font.PLAIN, 30);
- 756 g.setFont(fontSenderAddress);
- 757 if (jAddress.length() > 27) {
- 758 g.drawString(jAddress.substring(0, 27), startWidth + 90, startHeight + 785);
- 759 g.drawString(jAddress.substring(27, jAddress.length()), startWidth + 90, startHeight + 817);
- 760 } else {
- 761 g.drawString(jAddress, startWidth + 90, startHeight + 785);
- 762 }
- 763
- 764 //绘制派送方式表格
- 765 g.drawLine(startWidth + 850, startHeight + 720, startWidth + 850, startHeight + 1080);
- 766 //设置派送类型字体
- 767 Font fontSenTyp = new Font("黑体", Font.BOLD, 55);
- 768 g.setFont(fontSenTyp);
- 769
- 770 //快递详细信息表格
- 771 g.drawLine(startWidth, startHeight + 972, startWidth + 850, startHeight + 972);
- 772
- 773 //快递详细信息
- 774 Font cellFont = new Font("黑体", Font.PLAIN, 22);
- 775 g.setFont(cellFont);
- 776 String[][] cellValue1 = {
- 777 {"付款方式:",orderParam.getPayMethod()},
- 778 {"月结账号:",orderParam.getMonthSettleNo()},
- 779 {"第三方地区:",orderParam.getThridArea()},
- 780 {"实际重量:",orderParam.getRealWeight()}
- 781 };
- 782 int cellLineHeight = startHeight + 855;
- 783 for (int i = 0; i < cellValue1.length; i++) {
- 784 g.drawString(cellValue1[i][0], startWidth + 30, cellLineHeight);
- 785 g.drawString(cellValue1[i][1], startWidth + 155, cellLineHeight);
- 786 cellLineHeight += 30;
- 787 }
- 788 String[][] cellValue2 = {
- 789 {"计费重量:",orderParam.getChargWeight()},
- 790 {"声明价值:",orderParam.getDeclarPrice()},
- 791 {"保价费用:",orderParam.getSupportFee()},
- 792 {"定时派送:",orderParam.getSendTime()}
- 793 };
- 794 cellLineHeight = startHeight + 855;
- 795 for (int i = 0; i < cellValue2.length; i++) {
- 796 g.drawString(cellValue2[i][0], startWidth + 355, cellLineHeight);
- 797 g.drawString(cellValue2[i][1], startWidth + 470, cellLineHeight);
- 798 cellLineHeight += 30;
- 799 }
- 800 String[][] cellValue3 = {
- 801 {"包装费用:",orderParam.getPackFee()},
- 802 {"运费:",orderParam.getFreight()},
- 803 {"费用合计:",orderParam.getSumFee()}
- 804 };
- 805 cellLineHeight = startHeight + 855;
- 806 for (int i = 0; i < cellValue3.length; i++) {
- 807 g.drawString(cellValue3[i][0], startWidth + 655, cellLineHeight);
- 808 g.drawString(cellValue3[i][1], startWidth + 770, cellLineHeight);
- 809 cellLineHeight += 30;
- 810 }
- 811
- 812 //转寄协议客户
- 813 Font fowardFont = new Font("黑体", Font.BOLD, 25);
- 814 g.setFont(fowardFont);
- 815 String fowardStr = "转寄协议客户";
- 816 g.drawString(fowardStr, startWidth + 693, startHeight + 965);
- 817
- 818 //托寄物
- 819 g.drawLine(startWidth + 80, startHeight + 972, startWidth + 80, startHeight + 1080);
- 820 //托寄物标题字符串
- 821 String articleTitleStr = "托寄物";
- 822 char[] articleTitleArray = articleTitleStr.toCharArray();
- 823 int articleTitleWidth = startWidth + 40;
- 824 int articleTitleHeight = startHeight + 1005;
- 825 for (int i = 0; i < articleTitleStr.length(); i++) {
- 826 g.drawString(String.valueOf(articleTitleArray[i]), articleTitleWidth, articleTitleHeight);
- 827 articleTitleHeight += 31;
- 828 }
- 829
- 830 //收件员信息
- 831 g.drawLine(startWidth + 600, startHeight + 972, startWidth + 600, startHeight + 1080);
- 832 Font receDriverFont = new Font("黑体", Font.PLAIN, 25);
- 833 g.setFont(receDriverFont);
- 834 String[][] receDriverStrs = {
- 835 {"收件员:",orderParam.getReviceDriver()},
- 836 {"寄件日期:",orderParam.getSendDate()},
- 837 {"派件员:",orderParam.getSendDriver()}
- 838 };
- 839 cellLineHeight = startHeight + 1005;
- 840 for (int i = 0; i < receDriverStrs.length; i++) {
- 841 g.drawString(receDriverStrs[i][0], startWidth + 610, cellLineHeight);
- 842 g.drawString(receDriverStrs[i][1], startWidth + 730, cellLineHeight);
- 843 cellLineHeight += 31;
- 844 }
- 845 //签名
- 846 String signStr = "签名:";
- 847 g.drawString(signStr, startWidth + 860, startHeight + 860);
- 848 //月日
- 849 String monthDay = "月 日";
- 850 g.drawString(monthDay, startWidth + 1100, startHeight + 1070);
- 851
- 852 //母单与子单分割线
- 853 g.drawLine(startWidth, startHeight + 1080, startWidth + 1198, startHeight + 1080);
- 854 //--------------------------- 第二联信息 ----------------------------------//
- 855 g.drawLine(startWidth, startHeight + 1260, startWidth + 1198, startHeight + 1260);
- 856 g.drawLine(startWidth + 300, startHeight + 1080, startWidth + 300, startHeight + 1260);
- 857 //插入Logo
- 858 Image sublogoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
- 859 g.drawImage(sublogoImg, startWidth + 40, startHeight + 1085, null);
- 860 //插入第二个logo (客服电话)
- 861 Image sublogoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
- 862 g.drawImage(sublogoTelImg, startWidth + 50, startHeight + 1170, null);
- 863
- 864 //导入子单条码
- 865 Image sfSubBarImg = insertImage(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", 0, 0, false);
- 866 g.drawImage(sfSubBarImg, startWidth + 400, startHeight + 1095, null);
- 867
- 868 //子单号
- 869 Font subOrderFont = new Font("黑体", Font.BOLD, 30);
- 870 g.setFont(subOrderFont);
- 871 g.drawString("母单号 " + sfBarCodeStr, startWidth + 460, startHeight + 1250);
- 872
- 873 //绘制寄件人表格 子单
- 874 g.drawLine(startWidth, startHeight + 1379, startWidth + 1198, startHeight + 1379);
- 875 g.drawLine(startWidth + 80, startHeight + 1260, startWidth + 80, startHeight + 1379);
- 876 //设置寄件人标题字体
- 877 Font fontsubSender = new Font("黑体", Font.BOLD, 30);
- 878 g.setFont(fontsubSender);
- 879 //寄件人标题字符串
- 880 String senderSubTitleStr = "寄件人";
- 881 char[] senderSubTitleArray = senderSubTitleStr.toCharArray();
- 882 int senderSubTitleWidth = startWidth + 40;
- 883 int senderSubTitleHeight = startHeight + 1295;
- 884 for (int i = 0; i < senderSubTitleStr.length(); i++) {
- 885 g.drawString(String.valueOf(senderSubTitleArray[i]), senderSubTitleWidth, senderSubTitleHeight);
- 886 senderSubTitleHeight += 33;
- 887 }
- 888 Font subJInfoFont = new Font("黑体", Font.PLAIN, 35);
- 889 g.setFont(subJInfoFont);
- 890 g.drawString(senderInfo, startWidth + 110, startHeight + 1295);
- 891
- 892 if (jAddress.length() > 30) {
- 893 g.drawString(jAddress.substring(0, 30), startWidth + 110, startHeight + 1330);
- 894 g.drawString(jAddress.substring(30, jAddress.length()), startWidth + 110, startHeight + 1365);
- 895 } else {
- 896 g.drawString(jAddress, startWidth + 110, startHeight + 1330);
- 897 }
- 898
- 899 //绘制收件人表格 子单
- 900 g.drawLine(startWidth, startHeight + 1499, startWidth + 1198, startHeight + 1499);
- 901 g.drawLine(startWidth + 80, startHeight + 1379, startWidth + 80, startHeight + 1499);
- 902 //收件人标题字符串
- 903 Font fontsubReveicer = new Font("黑体", Font.BOLD, 30);
- 904 g.setFont(fontsubReveicer);
- 905 String revicerSubTitleStr = "收件人";
- 906 char[] revicerSubTitleArray = revicerSubTitleStr.toCharArray();
- 907 int revicerSubTitleWidth = startWidth + 40;
- 908 int revicerSubTitleHeight = startHeight + 1415;
- 909 for (int i = 0; i < revicerSubTitleStr.length(); i++) {
- 910 g.drawString(String.valueOf(revicerSubTitleArray[i]), revicerSubTitleWidth, revicerSubTitleHeight);
- 911 revicerSubTitleHeight += 33;
- 912 }
- 913
- 914 Font subDInfoFont = new Font("黑体", Font.PLAIN, 35);
- 915 g.setFont(subDInfoFont);
- 916 g.drawString(revicerInfo, startWidth + 110, startHeight + 1415);
- 917 if (dAddress.length() > 30) {
- 918 g.drawString(dAddress.substring(0, 30), startWidth + 110, startHeight + 1450);
- 919 g.drawString(dAddress.substring(30, dAddress.length()), startWidth + 110, startHeight + 1486);
- 920 } else {
- 921 g.drawString(dAddress, startWidth + 110, startHeight + 1450);
- 922 }
- 923 g.dispose();
- 924 File sfbarFile = new File(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg");
- 925 if (sfbarFile.exists() && sfbarFile.isFile()) {
- 926 sfbarFile.delete();
- 927 }
- 928 //生成订单图片
- 929 createImage(picPath + "SfOrderImage_" + orderParam.getMailNo() + ".jpg");
- 930 if (isCompress) {
- 931 compressImg(picPath + "SfOrderImage_" + orderParam.getMailNo() + ".jpg", imgWidth, imgHeidht);
- 932 }
- 933 logger.info("订单生成成功. " + picPath + "SfOrderImage_" + orderParam.getMailNo() + ".jpg");
- 934 return true;
- 935 }
- 936 } catch (Exception e) {
- 937 e.printStackTrace();
- 938 }
- 939 return false;
- 940 }
- 941
- 942 /**
- 943 * 水平翻转图像 顺时针旋转90度、左右翻转
- 944 * @param bufferedimage 目标图像
- 945 * @return
- 946 */
- 947 private static BufferedImage rotate90DX(BufferedImage bi)
- 948 {
- 949 int width = bi.getWidth();
- 950 int height = bi.getHeight();
- 951
- 952 BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
- 953
- 954 for(int i=0; i<width; i++)
- 955 for(int j=0; j<height; j++)
- 956 biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));
- 957
- 958 return biFlip;
- 959 }
- 960
- 961
- 962 /**
- 963 * 水平翻转图像 逆时针旋转90度、左右翻转
- 964 * @param bufferedimage 目标图像
- 965 * @return
- 966 */
- 967 private static BufferedImage rotate90SX(BufferedImage bi)
- 968 {
- 969 int width = bi.getWidth();
- 970 int height = bi.getHeight();
- 971
- 972 BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
- 973
- 974 for(int i=0; i<width; i++)
- 975 for(int j=0; j<height; j++)
- 976 biFlip.setRGB(j, i, bi.getRGB(i, j));
- 977
- 978 return biFlip;
- 979 }
- 980
- 981 /**
- 982 * 根据规定尺寸压缩图片
- 983 * @param imgPath 图片路径
- 984 * @param width 图片宽度
- 985 * @param height 图片高度
- 986 * */
- 987 private static void compressImg(String imgPath, int width, int height){
- 988 /**
- 989 * 设置条码图片的尺寸
- 990 * */
- 991 BufferedInputStream bis = null;
- 992 BufferedOutputStream out = null;
- 993 FileOutputStream fis = null;
- 994 try {
- 995 File sfFile = new File(imgPath);
- 996 if (sfFile.isFile() && sfFile.exists()) {
- 997 //读取图片
- 998 bis = new BufferedInputStream(new FileInputStream(imgPath));
- 999 //转换成图片对象
- 1000 Image bi = ImageIO.read(bis).getScaledInstance(width, height, Image.SCALE_SMOOTH);
- 1001 //构建图片流 设置图片宽和高
- 1002 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- 1003 //绘制改变尺寸后的图
- 1004 tag.getGraphics().drawImage(bi, 0, 0,width, height, null);
- 1005 //保存图片
- 1006 fis = new FileOutputStream(imgPath);
- 1007 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fis);
- 1008 JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
- 1009 //设置压缩图片质量
- 1010 jep.setQuality(3f, true);
- 1011 encoder.encode(tag, jep);
- 1012 }
- 1013 } catch (Exception e) {
- 1014 e.printStackTrace();
- 1015 } finally {
- 1016 try {
- 1017 if (fis != null) fis.close();
- 1018 if (out != null) out.close();
- 1019 if (bis != null) bis.close();
- 1020 } catch (Exception e2) {
- 1021 e2.printStackTrace();
- 1022 }
- 1023 }
- 1024 }
- 1025
- 1026 //创建目录
- 1027 private static boolean createDir(String destDirName){
- 1028 try {
- 1029 File file = new File(destDirName);
- 1030 if (destDirName.endsWith(File.separator))
- 1031 destDirName += File.separator;
- 1032 if (file.mkdir())
- 1033 return true;
- 1034 } catch (Exception e) {
- 1035 e.printStackTrace();
- 1036 }
- 1037 return false;
- 1038 }
- 1039
- 1040 //删除目录
- 1041 public static boolean removeDir(File dir){
- 1042 File[] files = dir.listFiles();
- 1043 for (File file : files) {
- 1044 if (file.isDirectory()) {
- 1045 return removeDir(file);
- 1046 } else {
- 1047 return file.delete();
- 1048 }
- 1049 }
- 1050 return dir.delete();
- 1051 }
- 1052
- 1053 //删除目录
- 1054 public static boolean deleteMk(String dirPath){
- 1055 File dirFile = new File(dirPath);
- 1056 if (dirFile.isDirectory()) {
- 1057 File[] files = dirFile.listFiles();
- 1058 for (int i = 0; i < files.length; i++) {
- 1059 files[i].delete();
- 1060 }
- 1061 }
- 1062 return dirFile.delete();
- 1063 }
- 1064
- 1065 /**
- 1066 * 把文件转换为byte[]
- 1067 * @param filePath 文件路径
- 1068 * */
- 1069 public static byte[] getFileBytes(String filePath){
- 1070 byte[] buffer = null;
- 1071 FileInputStream fis = null;
- 1072 ByteArrayOutputStream bos = null;
- 1073 try {
- 1074 File file = new File(filePath);
- 1075 fis = new FileInputStream(file);
- 1076 bos = new ByteArrayOutputStream(1000);
- 1077 byte[] b = new byte[1000];
- 1078 int n;
- 1079 while ((n = fis.read(b)) != -1) {
- 1080 bos.write(b, 0, n);
- 1081 }
- 1082 buffer = bos.toByteArray();
- 1083 } catch (FileNotFoundException e) {
- 1084 e.printStackTrace();
- 1085 } catch (IOException e) {
- 1086 e.printStackTrace();
- 1087 } finally {
- 1088 try {
- 1089 if (bos != null) {
- 1090 bos.close();
- 1091 }
- 1092 if (fis != null) {
- 1093 fis.close();
- 1094 }
- 1095 } catch (Exception e2) {
- 1096 e2.printStackTrace();
- 1097 }
- 1098 }
- 1099 return buffer;
- 1100 }
- 1101 }
三、生成电子面单所需参数类。
SfPrintOrderParam.java
四、测试运行此程序
- 1 package testNetty.wu;
- 2
- 3 /**
- 4 *
- 5 */
- 6 public class App
- 7 {
- 8 public static void main( String[] args )
- 9 {
- 10 SfPrintOrderParam param = new SfPrintOrderParam();
- 11
- 12 param.setMailNo("883987638272");
- 13 param.setEarthCarryFlag("E");
- 14 param.setDestCode("0311");
- 15 param.setdContact("李XX");
- 16 param.setdCompany("河北北国商城");
- 17 param.setdMobile("13588911223");
- 18 param.setdProvince("河北省");
- 19 param.setdCity("石家庄市");
- 20 param.setdCounty("桥西区");
- 21 param.setdAddress("华润万象城");
- 22 param.setjContact("吴XX");
- 23 param.setjMobile("13716533121");
- 24 param.setjCompany("北京XXX科技公司");
- 25 param.setjProvince("北京");
- 26 param.setjCity("北京市");
- 27 param.setjCounty("朝阳区");
- 28 param.setjCounty("东三环24号XXX大厦");
- 29
- 30
- 31 SFOrderGenerateUtil.generateOrders(
- 32 "C:\\Users\\Administrator\\Desktop\\",
- 33 param,
- 34 "1",
- 35 false,
- 36 600,
- 37 400);
- 38 }
- 39 }
五、生成电子面单图片效果 如图:
