经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
使用java Graphics 绘图工具生成顺丰快递电子面单
来源:cnblogs  作者:咫尺的梦想_w  时间:2019/7/5 9:23:22  对本文有异议

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

     废话不多说直接上代码。

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

     SFBarCodeGenerateUtil.java

  1. 1 package testNetty.wu;
  2. 2
  3. 3 import java.awt.Color;
  4. 4 import java.awt.Graphics;
  5. 5 import java.awt.Image;
  6. 6 import java.awt.image.BufferedImage;
  7. 7 import java.io.BufferedInputStream;
  8. 8 import java.io.BufferedOutputStream;
  9. 9 import java.io.File;
  10. 10 import java.io.FileInputStream;
  11. 11 import java.io.FileNotFoundException;
  12. 12 import java.io.FileOutputStream;
  13. 13 import java.io.OutputStream;
  14. 14 import java.util.ArrayList;
  15. 15 import java.util.List;
  16. 16 import java.util.regex.Pattern;
  17. 17
  18. 18 import javax.imageio.ImageIO;
  19. 19
  20. 20 import org.apache.log4j.Logger;
  21. 21
  22. 22
  23. 23 import com.sun.image.codec.jpeg.JPEGCodec;
  24. 24 import com.sun.image.codec.jpeg.JPEGImageEncoder;
  25. 25
  26. 26 /**
  27. 27 * 顺丰速运条码生成工具<br/>
  28. 28 * 采用 code128c编码规则<br/>
  29. 29 * <pre>
  30. 30 * CODE128C:[00]-[99]的数字对集合,共100个
  31. 31 * 即只能表示偶数位长度的数字
  32. 32 * </pre>
  33. 33 * @author wu
  34. 34 * @version 1.0
  35. 35 * @Time 2017/03/29
  36. 36 * */
  37. 37 public class SFBarCodeGenerateUtil {
  38. 38
  39. 39 private static final Logger logger = Logger.getLogger(SFBarCodeGenerateUtil.class.getSimpleName());
  40. 40
  41. 41 /**图片格式 jpg 格式*/
  42. 42 public static final String PICTURE_JPG = "JPG";
  43. 43 /**图片格式 png 格式*/
  44. 44 public static final String PICTURE_PNG = "PNG";
  45. 45 /**图片格式 gif 格式*/
  46. 46 public static final String PICTURE_GIF = "GIF";
  47. 47
  48. 48 /** code128编码字符集 二维数组*/
  49. 49 private static String[][] code128 = {
  50. 50 { " ", " ", "00", "212222", "11011001100" },
  51. 51 { "!", "!", "01", "222122", "11001101100" },
  52. 52 { "\"", "\"", "02", "222221", "11001100110" },
  53. 53 { "#", "#", "03", "121223", "10010011000" },
  54. 54 { "$", "$", "04", "121322", "10010001100" },
  55. 55 { "%", "%", "05", "131222", "10001001100" },
  56. 56 { "&", "&", "06", "122213", "10011001000" },
  57. 57 { "'", "'", "07", "122312", "10011000100" },
  58. 58 { "(", "(", "08", "132212", "10001100100" },
  59. 59 { ")", ")", "09", "221213", "11001001000" },
  60. 60 { "*", "*", "10", "221312", "11001000100" },
  61. 61 { "+", "+", "11", "231212", "11000100100" },
  62. 62 { ",", ",", "12", "112232", "10110011100" },
  63. 63 { "-", "-", "13", "122132", "10011011100" },
  64. 64 { ".", ".", "14", "122231", "10011001110" },
  65. 65 { "/", "/", "15", "113222", "10111001100" },
  66. 66 { "0", "0", "16", "123122", "10011101100" },
  67. 67 { "1", "1", "17", "123221", "10011100110" },
  68. 68 { "2", "2", "18", "223211", "11001110010" },
  69. 69 { "3", "3", "19", "221132", "11001011100" },
  70. 70 { "4", "4", "20", "221231", "11001001110" },
  71. 71 { "5", "5", "21", "213212", "11011100100" },
  72. 72 { "6", "6", "22", "223112", "11001110100" },
  73. 73 { "7", "7", "23", "312131", "11101101110" },
  74. 74 { "8", "8", "24", "311222", "11101001100" },
  75. 75 { "9", "9", "25", "321122", "11100101100" },
  76. 76 { ":", ":", "26", "321221", "11100100110" },
  77. 77 { ";", ";", "27", "312212", "11101100100" },
  78. 78 { "<", "<", "28", "322112", "11100110100" },
  79. 79 { "=", "=", "29", "322211", "11100110010" },
  80. 80 { ">", ">", "30", "212123", "11011011000" },
  81. 81 { "?", "?", "31", "212321", "11011000110" },
  82. 82 { "@", "@", "32", "232121", "11000110110" },
  83. 83 { "A", "A", "33", "111323", "10100011000" },
  84. 84 { "B", "B", "34", "131123", "10001011000" },
  85. 85 { "C", "C", "35", "131321", "10001000110" },
  86. 86 { "D", "D", "36", "112313", "10110001000" },
  87. 87 { "E", "E", "37", "132113", "10001101000" },
  88. 88 { "F", "F", "38", "132311", "10001100010" },
  89. 89 { "G", "G", "39", "211313", "11010001000" },
  90. 90 { "H", "H", "40", "231113", "11000101000" },
  91. 91 { "I", "I", "41", "231311", "11000100010" },
  92. 92 { "J", "J", "42", "112133", "10110111000" },
  93. 93 { "K", "K", "43", "112331", "10110001110" },
  94. 94 { "L", "L", "44", "132131", "10001101110" },
  95. 95 { "M", "M", "45", "113123", "10111011000" },
  96. 96 { "N", "N", "46", "113321", "10111000110" },
  97. 97 { "O", "O", "47", "133121", "10001110110" },
  98. 98 { "P", "P", "48", "313121", "11101110110" },
  99. 99 { "Q", "Q", "49", "211331", "11010001110" },
  100. 100 { "R", "R", "50", "231131", "11000101110" },
  101. 101 { "S", "S", "51", "213113", "11011101000" },
  102. 102 { "T", "T", "52", "213311", "11011100010" },
  103. 103 { "U", "U", "53", "213131", "11011101110" },
  104. 104 { "V", "V", "54", "311123", "11101011000" },
  105. 105 { "W", "W", "55", "311321", "11101000110" },
  106. 106 { "X", "X", "56", "331121", "11100010110" },
  107. 107 { "Y", "Y", "57", "312113", "11101101000" },
  108. 108 { "Z", "Z", "58", "312311", "11101100010" },
  109. 109 { "[", "[", "59", "332111", "11100011010" },
  110. 110 { "\\", "\\", "60", "314111", "11101111010" },
  111. 111 { "]", "]", "61", "221411", "11001000010" },
  112. 112 { "^", "^", "62", "431111", "11110001010" },
  113. 113 { "_", "_", "63", "111224", "10100110000" },
  114. 114 { "NUL", "`", "64", "111422", "10100001100" },
  115. 115 { "SOH", "a", "65", "121124", "10010110000" },
  116. 116 { "STX", "b", "66", "121421", "10010000110" },
  117. 117 { "ETX", "c", "67", "141122", "10000101100" },
  118. 118 { "EOT", "d", "68", "141221", "10000100110" },
  119. 119 { "ENQ", "e", "69", "112214", "10110010000" },
  120. 120 { "ACK", "f", "70", "112412", "10110000100" },
  121. 121 { "BEL", "g", "71", "122114", "10011010000" },
  122. 122 { "BS", "h", "72", "122411", "10011000010" },
  123. 123 { "HT", "i", "73", "142112", "10000110100" },
  124. 124 { "LF", "j", "74", "142211", "10000110010" },
  125. 125 { "VT", "k", "75", "241211", "11000010010" },
  126. 126 { "FF", "I", "76", "221114", "11001010000" },
  127. 127 { "CR", "m", "77", "413111", "11110111010" },
  128. 128 { "SO", "n", "78", "241112", "11000010100" },
  129. 129 { "SI", "o", "79", "134111", "10001111010" },
  130. 130 { "DLE", "p", "80", "111242", "10100111100" },
  131. 131 { "DC1", "q", "81", "121142", "10010111100" },
  132. 132 { "DC2", "r", "82", "121241", "10010011110" },
  133. 133 { "DC3", "s", "83", "114212", "10111100100" },
  134. 134 { "DC4", "t", "84", "124112", "10011110100" },
  135. 135 { "NAK", "u", "85", "124211", "10011110010" },
  136. 136 { "SYN", "v", "86", "411212", "11110100100" },
  137. 137 { "ETB", "w", "87", "421112", "11110010100" },
  138. 138 { "CAN", "x", "88", "421211", "11110010010" },
  139. 139 { "EM", "y", "89", "212141", "11011011110" },
  140. 140 { "SUB", "z", "90", "214121", "11011110110" },
  141. 141 { "ESC", "{", "91", "412121", "11110110110" },
  142. 142 { "FS", "|", "92", "111143", "10101111000" },
  143. 143 { "GS", "},", "93", "111341", "10100011110" },
  144. 144 { "RS", "~", "94", "131141", "10001011110" },
  145. 145 { "US", "DEL", "95", "114113", "10111101000" },
  146. 146 { "FNC3", "FNC3", "96", "114311", "10111100010" },
  147. 147 { "FNC2", "FNC2", "97", "411113", "11110101000" },
  148. 148 { "SHIFT", "SHIFT", "98", "411311", "11110100010" },
  149. 149 { "CODEC", "CODEC", "99", "113141", "10111011110" },
  150. 150 { "CODEB", "FNC4", "CODEB", "114131", "10111101110" },
  151. 151 { "FNC4", "CODEA", "CODEA", "311141", "11101011110" },
  152. 152 { "FNC1", "FNC1", "FNC1", "411131", "11110101110" },
  153. 153 { "StartA", "StartA", "StartA", "211412", "11010000100" },
  154. 154 { "StartB", "StartB", "StartB", "211214", "11010010000" },
  155. 155 { "StartC", "StartC", "StartC", "211232", "11010011100" },
  156. 156 { "Stop", "Stop", "Stop", "2331112", "1100011101011" },
  157. 157 };
  158. 158
  159. 159 /**
  160. 160 * 生产Code128的条形码的code
  161. 161 * @param barCode 生成条码的数字号码
  162. 162 * @return
  163. 163 */
  164. 164 private static String getCode(String barCode) {
  165. 165 String rtnCode = "";// 返回的参数
  166. 166 List<Integer> rtnCodeNumb = new ArrayList<Integer>();// 2截取位的组合
  167. 167 int examine = 105; // 首位
  168. 168 // 编码不能是奇数
  169. 169 if (!((barCode.length() & 1) == 0))
  170. 170 return "";
  171. 171 while (barCode.length() != 0) {
  172. 172 int temp = 0;
  173. 173 try {
  174. 174 // Code128 编码必须为数字
  175. 175 temp = (Integer) Integer.valueOf(barCode.substring(0, 2));
  176. 176 } catch (Exception e) {
  177. 177 e.printStackTrace();
  178. 178 return "";
  179. 179 }
  180. 180 // 获得条纹
  181. 181 rtnCode += getValue(barCode, barCode.substring(0, 2), temp);
  182. 182 rtnCodeNumb.add(temp);
  183. 183 // 条码截取2个就需要去掉用过的前二位
  184. 184 barCode = barCode.substring(2);
  185. 185 }
  186. 186 if (rtnCodeNumb.size() == 0) {
  187. 187 return "";
  188. 188 }
  189. 189 rtnCode = getValue(examine) + rtnCode; // 获取开始位
  190. 190 for (int i = 0; i != rtnCodeNumb.size(); i++) {
  191. 191 examine += rtnCodeNumb.get(i) * (i + 1);
  192. 192 }
  193. 193 examine = examine % 103; // 获得校验位
  194. 194 rtnCode += getValue(examine); // 获取校验位
  195. 195 rtnCode += "1100011101011"; // 结束位
  196. 196 return rtnCode;
  197. 197 }
  198. 198
  199. 199 /**
  200. 200 * 根据编号获得条纹
  201. 201 *
  202. 202 * @param encode
  203. 203 * @param p_Value
  204. 204 * @param p_SetID
  205. 205 * @return
  206. 206 */
  207. 207 private static String getValue(String encode, String p_Value, int p_SetID) {
  208. 208 return code128[p_SetID][4];
  209. 209 }
  210. 210
  211. 211 /**
  212. 212 * 根据编号获得条纹
  213. 213 * @param p_CodeId
  214. 214 * @return
  215. 215 */
  216. 216 private static String getValue(int p_CodeId) {
  217. 217 return code128[p_CodeId][4];
  218. 218 }
  219. 219
  220. 220 // 条码的高度像素数
  221. 221 private static int m_nImageHeight = 40;
  222. 222
  223. 223 /**
  224. 224 * 生成条码
  225. 225 * @param barString 条码模式字符串
  226. 226 * @param path 生成条码图片的路径
  227. 227 */
  228. 228 private static boolean kiCode128C(String barString, String path) {
  229. 229 OutputStream out = null;
  230. 230 try {
  231. 231 File myPNG = new File(path);
  232. 232 out = new FileOutputStream(myPNG);
  233. 233 int nImageWidth = 0;
  234. 234 char[] cs = barString.toCharArray();
  235. 235 for (int i = 0; i != cs.length; i++) {
  236. 236 nImageWidth = cs.length;
  237. 237 }
  238. 238 BufferedImage bi = new BufferedImage(nImageWidth, m_nImageHeight,
  239. 239 BufferedImage.TYPE_INT_RGB);
  240. 240 Graphics g = bi.getGraphics();
  241. 241 for (int i = 0; i < cs.length; i++) {
  242. 242 if ("1".equals(cs[i] + "")) {
  243. 243 g.setColor(Color.BLACK);
  244. 244 g.fillRect(i, 0, 1, m_nImageHeight);
  245. 245 } else {
  246. 246 g.setColor(Color.WHITE);
  247. 247 g.fillRect(i, 0, 1, m_nImageHeight);
  248. 248 }
  249. 249 }
  250. 250 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  251. 251 encoder.encode(bi);
  252. 252 return true;
  253. 253 } catch (FileNotFoundException fe) {
  254. 254 logger.error("系统找不到指定路径!!" + path, fe);
  255. 255 return false;
  256. 256 } catch (Exception e) {
  257. 257 e.printStackTrace();
  258. 258 } finally {
  259. 259 try {
  260. 260 if (out != null)
  261. 261 out.close();
  262. 262 } catch (Exception e2) {
  263. 263 e2.printStackTrace();
  264. 264 }
  265. 265 }
  266. 266 return false;
  267. 267 }
  268. 268
  269. 269 /**
  270. 270 * 生成条形码<br/>
  271. 271 * 条码是图片格式(JPG、PNG、GIF)
  272. 272 * @param wayBillNo
  273. 273 * 运单号
  274. 274 * @param generatePathName
  275. 275 * 生成条形码路径及名称
  276. 276 * @param width
  277. 277 * 条码图片宽度
  278. 278 * @param height
  279. 279 * 条码图片高度
  280. 280 * @param picTyp
  281. 281 * 图片格式<br/>
  282. 282 * JPG、PNG、GIF三种图片类型选择
  283. 283 * @return boolean类型值
  284. 284 * true 生成成功,false 生成失败
  285. 285 * */
  286. 286 public static boolean generateBarCode(String wayBillNo, String generatePathName, int width,
  287. 287 int height, String picTyp){
  288. 288 //只能是数字
  289. 289 if (!Pattern.matches("[0-9]+", wayBillNo)) {
  290. 290 logger.error("生成CODE128C条码只能是0~9的数字不能有其他字符!!");
  291. 291 //运单号长度只能是偶数
  292. 292 } else if (wayBillNo.length() % 2 != 0) {
  293. 293 logger.error("生成CODE128C条码的长度只能是偶数!!");
  294. 294 //生成条码
  295. 295 } else if (kiCode128C(getCode(wayBillNo), generatePathName)){
  296. 296 /**
  297. 297 * 设置条码图片的尺寸
  298. 298 * */
  299. 299 BufferedInputStream bis = null;
  300. 300 BufferedOutputStream out = null;
  301. 301 try {
  302. 302 File sfFile = new File(generatePathName);
  303. 303 if (sfFile.isFile() && sfFile.exists()) {
  304. 304 //读取图片
  305. 305 bis = new BufferedInputStream(new FileInputStream(generatePathName));
  306. 306 //转换成图片对象
  307. 307 Image bi = ImageIO.read(bis);
  308. 308 //构建图片流 设置图片宽和高
  309. 309 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  310. 310 //绘制改变尺寸后的图
  311. 311 tag.getGraphics().drawImage(bi, 0, 0,width, height, null);
  312. 312 //保存图片
  313. 313 out = new BufferedOutputStream(new FileOutputStream(generatePathName));
  314. 314 ImageIO.write(tag, picTyp,out);
  315. 315 out.flush();
  316. 316 }
  317. 317 } catch (Exception e) {
  318. 318 e.printStackTrace();
  319. 319 } finally {
  320. 320 try {
  321. 321 if (out != null)
  322. 322 out.close();
  323. 323 if (bis != null)
  324. 324 bis.close();
  325. 325 } catch (Exception e2) {
  326. 326 e2.printStackTrace();
  327. 327 }
  328. 328 }
  329. 329 logger.info("条码生成成功. " + generatePathName + " 像素:" + width + "*" + height);
  330. 330 return true;
  331. 331 }
  332. 332 logger.error("条码生成失败!");
  333. 333 return false;
  334. 334 }
  335. 335
  336. 336 }

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

SFOrderGenerateUtil.java

  1. 1 package testNetty.wu;
  2. 2
  3. 3 import java.awt.BasicStroke;
  4. 4 import java.awt.Color;
  5. 5 import java.awt.Font;
  6. 6 import java.awt.Graphics2D;
  7. 7 import java.awt.Image;
  8. 8 import java.awt.RenderingHints;
  9. 9 import java.awt.image.BufferedImage;
  10. 10 import java.io.BufferedInputStream;
  11. 11 import java.io.BufferedOutputStream;
  12. 12 import java.io.ByteArrayOutputStream;
  13. 13 import java.io.File;
  14. 14 import java.io.FileInputStream;
  15. 15 import java.io.FileNotFoundException;
  16. 16 import java.io.FileOutputStream;
  17. 17 import java.io.IOException;
  18. 18
  19. 19 import javax.imageio.ImageIO;
  20. 20
  21. 21 import org.apache.commons.io.FileUtils;
  22. 22 import org.apache.log4j.Logger;
  23. 23
  24. 24 import com.sun.image.codec.jpeg.JPEGCodec;
  25. 25 import com.sun.image.codec.jpeg.JPEGEncodeParam;
  26. 26 import com.sun.image.codec.jpeg.JPEGImageEncoder;
  27. 27
  28. 28 /**
  29. 29 * 生成电子面单图片工具
  30. 30 * @author wu
  31. 31 * @version 1.0
  32. 32 * @time 2017/04/25
  33. 33 * */
  34. 34 public class SFOrderGenerateUtil {
  35. 35
  36. 36 private static final Logger logger = Logger.getLogger(SFOrderGenerateUtil.class.getSimpleName());
  37. 37
  38. 38 //图片的宽度
  39. 39 public static final int IMG_WIDTH = 1198;
  40. 40 //图片的宽度
  41. 41 public static final int IMG_HEIGHT = 1800;
  42. 42 //LOGO的宽度
  43. 43 public static final int LOGO_WIDTH = 240;
  44. 44 //LOGO高度
  45. 45 public static final int LOGO_HEIGHT = 100;
  46. 46 //LOGO客服电话的宽度
  47. 47 public static final int LOGO_TEL_WIDTH = 220;
  48. 48 //LOGO客服电话高度
  49. 49 public static final int LOGO_TEL_HEIGHT = 84;
  50. 50
  51. 51 //Logo路径
  52. 52 public static final String LOGO_PATH = "C:\\Users\\Administrator\\Desktop\\expressLogo\\logoSC.png";
  53. 53 //Logo客服电话
  54. 54 public static final String LOGO_TEL_PATH = "C:\\Users\\Administrator\\Desktop\\expressLogo\\sf_Tel.png";;
  55. 55
  56. 56
  57. 57 public static BufferedImage image;
  58. 58 public static void createImage(String fileLocation) {
  59. 59 FileOutputStream fos = null;
  60. 60 BufferedOutputStream bos = null;
  61. 61 try {
  62. 62 fos = new FileOutputStream(fileLocation);
  63. 63 bos = new BufferedOutputStream(fos);
  64. 64 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
  65. 65 encoder.encode(image);
  66. 66 } catch (Exception e) {
  67. 67 e.printStackTrace();
  68. 68 } finally {
  69. 69 try {
  70. 70 if (bos != null) bos.close();
  71. 71 if (fos != null) fos.close();
  72. 72 } catch (Exception e2) {
  73. 73 e2.printStackTrace();
  74. 74 }
  75. 75 }
  76. 76 }
  77. 77
  78. 78 /**
  79. 79 * 生成订单图片
  80. 80 * @param orderPath
  81. 81 * 生成订单存放路径
  82. 82 * @param printTyp
  83. 83 * 订单打印类型 1:A4纸打印 2:热敏纸打印
  84. 84 * @param orderParam
  85. 85 * 生成订单所需的参数对象
  86. 86 * @param isCompress
  87. 87 * 是否需要根据输入宽高压缩图片
  88. 88 * <pre>
  89. 89 * isCompress 为ture时 所输入的宽高才起作用
  90. 90 * </pre>
  91. 91 * @param imgWidth
  92. 92 * 图片宽度
  93. 93 * @param imgHeidht
  94. 94 * 图片高度
  95. 95 * @author Administrator
  96. 96 * @return
  97. 97 *
  98. 98 * */
  99. 99 public static boolean generateOrders(String orderPath, SfPrintOrderParam orderParam, String printTyp, boolean isCompress, int imgWidth, int imgHeidht){
  100. 100 if (null == orderParam)
  101. 101 return false;
  102. 102 int startHeight = 0; //表格的起始高度
  103. 103 int startWidth = 0; //表格的起始宽度
  104. 104 try {
  105. 105 if (orderParam.getSubMailNos().size() == 0 || orderParam.getSubMailNos().isEmpty()) {
  106. 106 generateParentOrder(orderPath, orderParam, printTyp, isCompress, imgWidth, imgHeidht);
  107. 107 return true;
  108. 108 } else {
  109. 109 String picPath = orderPath;
  110. 110 File mk = new File(picPath + orderParam.getMailNo());
  111. 111 if (mk.exists()){
  112. 112 FileUtils.deleteDirectory(mk);
  113. 113 }
  114. 114 for (int k = 0; k < orderParam.getSubMailNos().size(); k++) {
  115. 115 image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
  116. 116 Graphics2D g = image.createGraphics();
  117. 117 //以运单号为名称创建存放订单的目录
  118. 118 FileUtils.forceMkdir(mk);
  119. 119 picPath += orderParam.getMailNo() + "/";
  120. 120
  121. 121 //设置背景色为白色
  122. 122 g.setColor(Color.WHITE);
  123. 123 //设置颜色区域大小
  124. 124 g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
  125. 125 //提高导入Img的清晰度
  126. 126
  127. 127
  128. 128 /*
  129. 129 * 绘制表格 填充内容
  130. 130 * */
  131. 131 //表格线条的颜色
  132. 132 g.setColor(Color.BLACK);
  133. 133
  134. 134 //消除文本出现锯齿现象
  135. 135 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  136. 136 //g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
  137. 137
  138. 138 //表格的四个边框
  139. 139 g.drawLine(startWidth, startHeight, startWidth + 374, startHeight); //上边框
  140. 140 g.drawLine(startWidth, startHeight, startWidth, startHeight + 562); //左边框
  141. 141 g.drawLine(startWidth, startHeight + 562, startWidth + 375, startHeight + 562); //下边框
  142. 142 g.drawLine(startWidth + 374, startHeight, startWidth + 374, startHeight + 563); //右边框
  143. 143
  144. 144 //绘制表格内容 第一行
  145. 145 g.drawLine(startWidth, startHeight + 48, startWidth + 375, startHeight + 48);
  146. 146
  147. 147 //插入顺丰速运Logo
  148. 148 Image logoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
  149. 149 g.drawImage(logoImg, startWidth + 4, startHeight + 8, null);
  150. 150 //插入第二个logo (客服电话)
  151. 151 Image logoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
  152. 152 g.drawImage(logoTelImg, startWidth + 280, startHeight + 15, null);
  153. 153
  154. 154 //填写产品类型
  155. 155 Font fontSfTyp = new Font("黑体", Font.BOLD, 36);
  156. 156 g.setFont(fontSfTyp);
  157. 157 //产品类型字符串
  158. 158 String sfProTypStr = orderParam.getEarthCarryFlag();
  159. 159 g.drawString(sfProTypStr, startWidth + 150, startHeight + 41);
  160. 160
  161. 161 //绘制第二行
  162. 162 g.drawLine(startWidth, startHeight + 124, startWidth + 375, startHeight + 124);
  163. 163 g.drawLine(startWidth + 276, startHeight + 48, startWidth + 276, startHeight + 124);
  164. 164
  165. 165 //生成code128c 条码
  166. 166 SFBarCodeGenerateUtil.generateBarCode(orderParam.getMailNo(), //运单号
  167. 167 picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", //图片名称
  168. 168 262, //图片宽度
  169. 169 45, //图片高度
  170. 170 SFBarCodeGenerateUtil.PICTURE_JPG);
  171. 171 //导入条码图片
  172. 172 Image sfBarImg = insertImage(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", 0, 0, false);
  173. 173 g.drawImage(sfBarImg, startWidth + 7, startHeight + 53, null);
  174. 174 sfBarImg.flush();
  175. 175
  176. 176 //设置字体
  177. 177 Font fontSfBarCode = new Font("黑体",Font.BOLD,10);
  178. 178 g.setFont(fontSfBarCode);
  179. 179
  180. 180 String pageNum = (k + 1) + "/" + orderParam.getSubMailNos().size();
  181. 181 g.drawString(pageNum, startWidth + 7, startHeight + 108);
  182. 182
  183. 183 //子单号
  184. 184 String subBarCodeStr = orderParam.getSubMailNos().get(k);
  185. 185 subBarCodeStr = subBarCodeStr.replaceAll("(.{3})", "$1 ");
  186. 186 g.drawString("子单号 " + subBarCodeStr, startWidth + 80, startHeight + 108);
  187. 187
  188. 188 //条码字符串(条码母单号)
  189. 189 String sfBarCodeStr = orderParam.getMailNo();
  190. 190 sfBarCodeStr = sfBarCodeStr.replaceAll("(.{3})", "$1 ");
  191. 191 g.drawString("母单号 " + sfBarCodeStr, startWidth + 80, startHeight + 120);
  192. 192
  193. 193 //绘制产品类型框
  194. 194 g.drawLine(startWidth + 276, startHeight + 70, startWidth + 375, startHeight + 70);
  195. 195 Font fontSfProtypSub = new Font("黑体", Font.BOLD, 14);
  196. 196 g.setFont(fontSfProtypSub);
  197. 197
  198. 198 //产品类型字符串
  199. 199 String subSfProTypStr = orderParam.getExpressTyp();
  200. 200 g.drawString(subSfProTypStr, startWidth + 295, startHeight + 65);
  201. 201
  202. 202 //目的地栏的绘制
  203. 203 g.drawLine(startWidth, startHeight + 176, startWidth + 375, startHeight + 176);
  204. 204 g.drawLine(startWidth + 21, startHeight + 124, startWidth + 21, startHeight + 176);
  205. 205
  206. 206 //目的地填写
  207. 207 Font fontDest = new Font("黑体", Font.BOLD, 10);
  208. 208 g.setFont(fontDest);
  209. 209 //目的地标题
  210. 210 String destTitleStr = "目的地";
  211. 211 char[] destTitleArray = destTitleStr.toCharArray();
  212. 212 int destTitleWidth = startWidth + 6;
  213. 213 int destTitleHeight = startHeight + 140;
  214. 214 for (int i = 0; i < destTitleStr.length(); i++) {
  215. 215 g.drawString(String.valueOf(destTitleArray[i]), destTitleWidth, destTitleHeight);
  216. 216 destTitleHeight += 12;
  217. 217 }
  218. 218
  219. 219 //目的地代码
  220. 220 Font fontDestCode = new Font("Arial", Font.BOLD, 36);
  221. 221 g.setFont(fontDestCode);
  222. 222 //目的地代码字符串
  223. 223 String destCode = orderParam.getDestCode();
  224. 224 g.drawString(destCode, startWidth + 24, startHeight + 165);
  225. 225
  226. 226 //收件人表格栏
  227. 227 g.drawLine(startWidth, startHeight + 225, startWidth + 1198, startHeight + 225);
  228. 228 g.drawLine(startWidth + 21, startHeight + 176, startWidth + 21, startHeight + 225);
  229. 229
  230. 230 //设置收件人标题字体
  231. 231 Font fontRevicer = new Font("黑体", Font.BOLD, 10);
  232. 232 g.setFont(fontRevicer);
  233. 233 //收件人标题字符串
  234. 234 String revicerTitleStr = "收件人";
  235. 235 char[] revicerTitleArray = revicerTitleStr.toCharArray();
  236. 236 int revicerTitleWidth = startWidth + 6;
  237. 237 int revicerTitleHeight = startHeight + 192;
  238. 238 for (int i = 0; i < revicerTitleStr.length(); i++) {
  239. 239 g.drawString(String.valueOf(revicerTitleArray[i]), revicerTitleWidth, revicerTitleHeight);
  240. 240 revicerTitleHeight += 11;
  241. 241 }
  242. 242
  243. 243 /*
  244. 244 * 收件人详细信息
  245. 245 * */
  246. 246 String dContact = orderParam.getdContact(); //收件人姓名
  247. 247 String dTel = orderParam.getdTel(); //联系电话
  248. 248 String dMoblie = orderParam.getdMobile(); //联系人手机号
  249. 249 String dCompany = orderParam.getdCompany(); //公司名称
  250. 250 String dProvince = orderParam.getdProvince();
  251. 251 String dCity = orderParam.getdCity();
  252. 252 String dCounty = orderParam.getdCounty();
  253. 253 String dAddress = orderParam.getdAddress(); //详细地址
  254. 254
  255. 255 String revicerInfo = dContact + " " + dTel + " " + dMoblie + "" + dCompany;
  256. 256 dAddress = dProvince + dCity + dCounty + dAddress;
  257. 257
  258. 258 //设置收件人信息字体
  259. 259 Font fontRevicerInfo = new Font("黑体", Font.BOLD, 14);
  260. 260 g.setFont(fontRevicerInfo);
  261. 261 g.drawString(revicerInfo, startWidth + 24, startHeight + 190);
  262. 262 //设置收件人详细地址字体
  263. 263 Font fontReviceAddress = new Font("黑体", Font.BOLD, 14);
  264. 264 g.setFont(fontReviceAddress);
  265. 265 if (dAddress.length() > 23) {
  266. 266 g.drawString(dAddress.substring(0, 23), startWidth + 24, startHeight + 205);
  267. 267 g.drawString(dAddress.substring(23, dAddress.length()), startWidth + 24, startHeight + 220);
  268. 268 } else {
  269. 269 g.drawString(dAddress, startWidth + 24, startHeight + 205);
  270. 270 }
  271. 271
  272. 272 //绘制寄件人表格
  273. 273 g.drawLine(startWidth, startHeight + 259, startWidth + 375, startHeight +259);
  274. 274 g.drawLine(startWidth + 21, startHeight + 225, startWidth + 21, startHeight + 259);
  275. 275
  276. 276 //设置寄件人标题字体
  277. 277 Font fontSender = new Font("黑体", Font.BOLD, 10);
  278. 278 g.setFont(fontSender);
  279. 279 //寄件人标题字符串
  280. 280 String senderTitleStr = "寄件人";
  281. 281 char[] senderTitleArray = senderTitleStr.toCharArray();
  282. 282 int senderTitleWidth = startWidth + 6;
  283. 283 int senderTitleHeight = startHeight + 235;
  284. 284 for (int i = 0; i < senderTitleStr.length(); i++) {
  285. 285 g.drawString(String.valueOf(senderTitleArray[i]), senderTitleWidth, senderTitleHeight);
  286. 286 senderTitleHeight += 11;
  287. 287 }
  288. 288
  289. 289
  290. 290 /*
  291. 291 * 寄件人信息
  292. 292 * **/
  293. 293 String jContact = orderParam.getjContact(); //寄件人姓名
  294. 294 String jTel = orderParam.getjTel(); //寄件人联系电话
  295. 295 String jMobile = orderParam.getjMobile();
  296. 296 String jCompany = orderParam.getjCompany();
  297. 297 String jProvince = orderParam.getjProvince();
  298. 298 String jCity = orderParam.getjCity();
  299. 299 String jCounty = orderParam.getjCounty();
  300. 300 String jAddress = orderParam.getjAddress();
  301. 301
  302. 302 String senderInfo = jContact + " " + jTel + " " + jMobile + " " + jCompany;
  303. 303 jAddress = jProvince + jCity + jCounty + jAddress;
  304. 304
  305. 305 //设置寄件人信息字体
  306. 306 Font fontSenderInfo = new Font("黑体", Font.PLAIN, 8);
  307. 307 g.setFont(fontSenderInfo);
  308. 308 g.drawString(senderInfo, startWidth + 24, startHeight + 240);
  309. 309
  310. 310 //设置寄件人详细地址字体
  311. 311 Font fontSenderAddress = new Font("黑体", Font.PLAIN, 8);
  312. 312 g.setFont(fontSenderAddress);
  313. 313 if (jAddress.length() > 27) {
  314. 314 g.drawString(jAddress.substring(0, 27), startWidth + 24, startHeight + 250);
  315. 315 g.drawString(jAddress.substring(27, jAddress.length()), startWidth + 24, startHeight + 265);
  316. 316 } else {
  317. 317 g.drawString(jAddress, startWidth + 24, startHeight + 250);
  318. 318 }
  319. 319
  320. 320 //绘制派送方式表格
  321. 321 g.drawLine(startWidth + 310, startHeight + 225, startWidth + 310, startHeight + 338);
  322. 322 //设置派送类型字体
  323. 323 Font fontSenTyp = new Font("黑体", Font.BOLD, 55);
  324. 324 g.setFont(fontSenTyp);
  325. 325
  326. 326 //快递详细信息表格
  327. 327 g.drawLine(startWidth, startHeight + 304, startWidth + 310, startHeight + 304);
  328. 328
  329. 329 //快递详细信息
  330. 330 Font cellFont = new Font("黑体", Font.PLAIN, 9);
  331. 331 g.setFont(cellFont);
  332. 332 String[][] cellValue1 = {
  333. 333 {"付款方式:",orderParam.getPayMethod()},
  334. 334 {"月结账号:",orderParam.getMonthSettleNo()},
  335. 335 {"第三方地区:",orderParam.getThridArea()},
  336. 336 {"实际重量:",orderParam.getRealWeight()}
  337. 337 };
  338. 338 int cellLineHeight = startHeight + 268;
  339. 339 for (int i = 0; i < cellValue1.length; i++) {
  340. 340 g.drawString(cellValue1[i][0], startWidth + 3, cellLineHeight);
  341. 341 g.drawString(cellValue1[i][1], startWidth + 52, cellLineHeight);
  342. 342 cellLineHeight += 10;
  343. 343 }
  344. 344 String[][] cellValue2 = {
  345. 345 {"计费重量:",orderParam.getChargWeight()},
  346. 346 {"声明价值:",orderParam.getDeclarPrice()},
  347. 347 {"保价费用:",orderParam.getSupportFee()},
  348. 348 {"定时派送:",orderParam.getSendTime()}
  349. 349 };
  350. 350 cellLineHeight = startHeight + 268;
  351. 351 for (int i = 0; i < cellValue2.length; i++) {
  352. 352 g.drawString(cellValue2[i][0], startWidth + 100, cellLineHeight);
  353. 353 g.drawString(cellValue2[i][1], startWidth + 150, cellLineHeight);
  354. 354 cellLineHeight += 10;
  355. 355 }
  356. 356 String[][] cellValue3 = {
  357. 357 {"包装费用:",orderParam.getPackFee()},
  358. 358 {"运费:",orderParam.getFreight()},
  359. 359 {"费用合计:",orderParam.getSumFee()}
  360. 360 };
  361. 361 cellLineHeight = startHeight + 268;
  362. 362 for (int i = 0; i < cellValue3.length; i++) {
  363. 363 g.drawString(cellValue3[i][0], startWidth + 220, cellLineHeight);
  364. 364 g.drawString(cellValue3[i][1], startWidth + 270, cellLineHeight);
  365. 365 cellLineHeight += 10;
  366. 366 }
  367. 367
  368. 368 //转寄协议客户
  369. 369 Font fowardFont = new Font("黑体", Font.BOLD, 9);
  370. 370 g.setFont(fowardFont);
  371. 371 String fowardStr = "转寄协议客户";
  372. 372 g.drawString(fowardStr, startWidth + 250, startHeight + 302);
  373. 373
  374. 374 //托寄物
  375. 375 g.drawLine(startWidth + 21, startHeight + 304, startWidth + 21, startHeight + 338);
  376. 376 //托寄物标题字符串
  377. 377 String articleTitleStr = "托寄物";
  378. 378 char[] articleTitleArray = articleTitleStr.toCharArray();
  379. 379 int articleTitleWidth = startWidth + 6;
  380. 380 int articleTitleHeight = startHeight + 315;
  381. 381 for (int i = 0; i < articleTitleStr.length(); i++) {
  382. 382 g.drawString(String.valueOf(articleTitleArray[i]), articleTitleWidth, articleTitleHeight);
  383. 383 articleTitleHeight += 10;
  384. 384 }
  385. 385
  386. 386 Font cargoFont = new Font("黑体", Font.PLAIN, 8);
  387. 387 g.setFont(cargoFont);
  388. 388 String cargoContent = orderParam.getCargoContent();
  389. 389 g.drawString(cargoContent, startWidth + 24, 315);
  390. 390
  391. 391 //收件员信息
  392. 392 g.drawLine(startWidth + 220, startHeight + 304, startWidth + 220, startHeight + 338);
  393. 393 Font receDriverFont = new Font("黑体", Font.PLAIN, 8);
  394. 394 g.setFont(receDriverFont);
  395. 395 String[][] receDriverStrs = {
  396. 396 {"收件员:",orderParam.getReviceDriver()},
  397. 397 {"寄件日期:",orderParam.getSendDate()},
  398. 398 {"派件员:",orderParam.getSendDriver()}
  399. 399 };
  400. 400 cellLineHeight = startHeight + 315;
  401. 401 for (int i = 0; i < receDriverStrs.length; i++) {
  402. 402 g.drawString(receDriverStrs[i][0], startWidth + 225, cellLineHeight);
  403. 403 g.drawString(receDriverStrs[i][1], startWidth + 265, cellLineHeight);
  404. 404 cellLineHeight += 10;
  405. 405 }
  406. 406 //签名
  407. 407 String signStr = "签名:";
  408. 408 g.drawString(signStr, startWidth + 312, startHeight + 268);
  409. 409 //月日
  410. 410 String monthDay = "月 日";
  411. 411 g.drawString(monthDay, startWidth + 345, startHeight + 336);
  412. 412
  413. 413 //母单与子单分割线
  414. 414 g.drawLine(startWidth, startHeight + 338, startWidth + 375, startHeight + 338);
  415. 415
  416. 416 //--------------------------- 如果有子单号 生成子单信息 ----------------------------------//
  417. 417 g.drawLine(startWidth, startHeight + 394, startWidth + 375, startHeight + 394);
  418. 418 g.drawLine(startWidth + 100, startHeight + 338, startWidth + 100, startHeight + 394);
  419. 419 //插入Logo
  420. 420 Image sublogoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
  421. 421 g.drawImage(sublogoImg, startWidth + 10, startHeight + 338, null);
  422. 422 //插入第二个logo (客服电话)
  423. 423 Image sublogoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
  424. 424 g.drawImage(sublogoTelImg, startWidth + 15, startHeight + 368, null);
  425. 425
  426. 426 //导入子单条码
  427. 427 //生成code128c 条码
  428. 428 SFBarCodeGenerateUtil.generateBarCode(orderParam.getSubMailNos().get(k), //运单号
  429. 429 picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg", //图片名称
  430. 430 169, //图片宽度
  431. 431 37, //图片高度
  432. 432 SFBarCodeGenerateUtil.PICTURE_JPG);
  433. 433 Image sfSubBarImg = insertImage(picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg", 0, 0, false);
  434. 434 g.drawImage(sfSubBarImg, startWidth + 150, startHeight + 342, null);
  435. 435 sfSubBarImg.flush();
  436. 436
  437. 437 //子单号
  438. 438 Font subOrderFont = new Font("黑体", Font.BOLD, 10);
  439. 439 g.setFont(subOrderFont);
  440. 440 g.drawString("子单号: " + subBarCodeStr, startWidth + 160, startHeight + 390);
  441. 441
  442. 442 //绘制寄件人表格 子单
  443. 443 g.drawLine(startWidth, startHeight + 431, startWidth + 375, startHeight + 431);
  444. 444 g.drawLine(startWidth + 21, startHeight + 394, startWidth + 21, startHeight + 431);
  445. 445 //设置寄件人标题字体
  446. 446 Font fontsubSender = new Font("黑体", Font.BOLD, 10);
  447. 447 g.setFont(fontsubSender);
  448. 448 //寄件人标题字符串
  449. 449 String senderSubTitleStr = "寄件人";
  450. 450 char[] senderSubTitleArray = senderSubTitleStr.toCharArray();
  451. 451 int senderSubTitleWidth = startWidth + 6;
  452. 452 int senderSubTitleHeight = startHeight + 408;
  453. 453 for (int i = 0; i < senderSubTitleStr.length(); i++) {
  454. 454 g.drawString(String.valueOf(senderSubTitleArray[i]), senderSubTitleWidth, senderSubTitleHeight);
  455. 455 senderSubTitleHeight += 10;
  456. 456 }
  457. 457 Font subJInfoFont = new Font("黑体", Font.PLAIN, 8);
  458. 458 g.setFont(subJInfoFont);
  459. 459 g.drawString(senderInfo, startWidth + 24, startHeight + 405);
  460. 460
  461. 461 if (jAddress.length() > 30) {
  462. 462 g.drawString(jAddress.substring(0, 30), startWidth + 24, startHeight + 415);
  463. 463 g.drawString(jAddress.substring(30, jAddress.length()), startWidth + 24, startHeight + 425);
  464. 464 } else {
  465. 465 g.drawString(jAddress, startWidth + 24, startHeight + 415);
  466. 466 }
  467. 467
  468. 468 //绘制收件人表格 子单
  469. 469 g.drawLine(startWidth, startHeight + 469, startWidth + 375, startHeight + 469);
  470. 470 g.drawLine(startWidth + 21, startHeight + 394, startWidth + 21, startHeight + 469);
  471. 471 //收件人标题字符串
  472. 472 Font fontsubReveicer = new Font("黑体", Font.BOLD, 10);
  473. 473 g.setFont(fontsubReveicer);
  474. 474 String revicerSubTitleStr = "收件人";
  475. 475 char[] revicerSubTitleArray = revicerSubTitleStr.toCharArray();
  476. 476 int revicerSubTitleWidth = startWidth + 6;
  477. 477 int revicerSubTitleHeight = startHeight + 445;
  478. 478 for (int i = 0; i < revicerSubTitleStr.length(); i++) {
  479. 479 g.drawString(String.valueOf(revicerSubTitleArray[i]), revicerSubTitleWidth, revicerSubTitleHeight);
  480. 480 revicerSubTitleHeight += 10;
  481. 481 }
  482. 482
  483. 483 Font subDInfoFont = new Font("黑体", Font.PLAIN, 8);
  484. 484 g.setFont(subDInfoFont);
  485. 485 g.drawString(revicerInfo, startWidth + 24, startHeight + 442);
  486. 486 if (dAddress.length() > 35) {
  487. 487 g.drawString(dAddress.substring(0, 35), startWidth + 24, startHeight + 452);
  488. 488 g.drawString(dAddress.substring(35, dAddress.length()), startWidth + 24, startHeight + 462);
  489. 489 } else {
  490. 490 g.drawString(dAddress, startWidth + 24, startHeight + 452);
  491. 491 }
  492. 492 createImage(picPath + "SfOrderImage_" + orderParam.getMailNo() + "_" + (k + 1) + ".jpg");
  493. 493 //如果需要需要根据自定义尺寸压缩图片
  494. 494 if (isCompress) {
  495. 495 //压缩图片
  496. 496 compressImg(picPath + "SfOrderImage_" + orderParam.getMailNo() + "_" + (k + 1) + ".jpg", imgWidth, imgHeidht);
  497. 497 }
  498. 498 g.dispose();
  499. 499 File sfSubFile = new File(picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg");
  500. 500 if (sfSubFile.exists() && sfSubFile.isFile()) {
  501. 501 if (!sfSubFile.delete()){
  502. 502 logger.error("删除" + picPath + "SFBarCoding_sub_" + orderParam.getSubMailNos().get(k) + ".jpg 失败!");
  503. 503 }
  504. 504 }
  505. 505 File sfbarFile = new File(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg");
  506. 506 if (sfbarFile.exists() && sfbarFile.isFile()) {
  507. 507 if (!sfbarFile.delete()) {
  508. 508 logger.error("删除" + picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg 失败!");
  509. 509 }
  510. 510 }
  511. 511 logger.error("订单生成成功. " + picPath + "SfOrderImage_" + orderParam.getMailNo() + "_" + (k + 1) + ".jpg");
  512. 512 picPath = orderPath;
  513. 513 }
  514. 514 }
  515. 515 return true;
  516. 516
  517. 517 } catch (IOException e) {
  518. 518 e.printStackTrace();
  519. 519 } catch (Exception e) {
  520. 520 e.printStackTrace();
  521. 521 }
  522. 522 return false;
  523. 523 }
  524. 524
  525. 525 /**
  526. 526 * 插入图片 自定义图片的宽高
  527. 527 * @param imgPath
  528. 528 * 插入图片的路径
  529. 529 * @param imgWidth
  530. 530 * 设置图片的宽度
  531. 531 * @param imgHeight
  532. 532 * 设置图片的高度
  533. 533 * @param isCompress
  534. 534 * 是否按输入的宽高定义图片的尺寸,只有为true时 输入的宽度和高度才起作用<br/>
  535. 535 * 为false时输入的宽高不起作用,按输入图片的默认尺寸
  536. 536 * @return
  537. 537 * @throws Exception
  538. 538 * */
  539. 539 private static Image insertImage(String imgPath, int imgWidth, int imgHeight, boolean isCompress) throws Exception {
  540. 540 File fileimage = new File(imgPath);
  541. 541 Image src = ImageIO.read(fileimage);
  542. 542 if (isCompress) {
  543. 543 Image image = src.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH);
  544. 544 return image;
  545. 545 }
  546. 546 return src;
  547. 547 }
  548. 548
  549. 549 /**
  550. 550 * 生成母订单
  551. 551 * @param orderParam 生成订单所需的参数对象
  552. 552 * @return boolean
  553. 553 * */
  554. 554 private static boolean generateParentOrder(String orderPath, SfPrintOrderParam orderParam, String printTyp, boolean isCompress, int imgWidth, int imgHeidht){
  555. 555 if (null == orderParam)
  556. 556 return false;
  557. 557 String picPath = orderPath;
  558. 558 int startHeight = 0; //表格的起始高度
  559. 559 int startWidth = 0; //表格的起始宽度
  560. 560 try {
  561. 561 if (orderParam.getSubMailNos().size() == 0 || orderParam.getSubMailNos().isEmpty()) {
  562. 562 image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
  563. 563 Graphics2D g = image.createGraphics();
  564. 564
  565. 565 //以运单号为名称创建存放订单的目录
  566. 566 File mk = new File(picPath + orderParam.getMailNo());
  567. 567 if (mk.exists()) {
  568. 568 FileUtils.deleteDirectory(mk);
  569. 569 }
  570. 570 FileUtils.forceMkdir(mk);
  571. 571
  572. 572 picPath += orderParam.getMailNo() + "/";
  573. 573
  574. 574 //设置背景色为白色
  575. 575 g.setColor(Color.WHITE);
  576. 576 //设置颜色区域大小
  577. 577 g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
  578. 578
  579. 579 /*
  580. 580 * 绘制表格 填充内容
  581. 581 * */
  582. 582 //表格线条的颜色
  583. 583 g.setColor(Color.BLACK);
  584. 584 //边框加粗
  585. 585 g.setStroke(new BasicStroke(2.0f));
  586. 586 //消除文本出现锯齿现象
  587. 587 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  588. 588 //表格的四个边框
  589. 589 g.drawLine(startWidth, startHeight, startWidth + 1198, startHeight); //上边框
  590. 590
  591. 591 g.drawLine(startWidth, startHeight, startWidth, startHeight + 1800); //左边框
  592. 592 g.drawLine(startWidth, startHeight + 1799, startWidth + 1198, startHeight + 1799); //下边框
  593. 593 g.drawLine(startWidth + 1197, startHeight, startWidth + 1197, startHeight + 1800); //右边框
  594. 594
  595. 595 //绘制表格内容 第一行
  596. 596 g.drawLine(startWidth, startHeight + 155, startWidth + 1198, startHeight + 155);
  597. 597
  598. 598 //A4纸打印是才有Logo
  599. 599 if (Integer.valueOf(printTyp) == ExpressConstant.ORDER_PRINT_TYP_A4) {
  600. 600 //插入Logo
  601. 601 Image logoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
  602. 602 g.drawImage(logoImg, startWidth + 30, startHeight + 30, null);
  603. 603 //插入第二个logo (客服电话)
  604. 604 Image logoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
  605. 605 g.drawImage(logoTelImg, startWidth + 920, startHeight + 30, null);
  606. 606 }
  607. 607
  608. 608 //E路标识
  609. 609 Font fontSfTyp = new Font("微软雅黑", Font.BOLD, 145);
  610. 610 g.setFont(fontSfTyp);
  611. 611 //E路标识
  612. 612 String sfProTypStr = orderParam.getEarthCarryFlag();
  613. 613 g.drawString(sfProTypStr, startWidth + 445, startHeight + 130);
  614. 614
  615. 615 //绘制第二行
  616. 616 g.drawLine(startWidth, startHeight + 396, startWidth + 1198, startHeight + 396);
  617. 617 g.drawLine(startWidth + 750, startHeight + 155, startWidth + 750, startHeight + 396);
  618. 618
  619. 619 //生成code128c 条码
  620. 620 SFBarCodeGenerateUtil.generateBarCode(orderParam.getMailNo(), //运单号
  621. 621 picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", //图片名称
  622. 622 600, //图片宽度
  623. 623 120, //图片高度
  624. 624 SFBarCodeGenerateUtil.PICTURE_JPG);
  625. 625 //导入条码图片
  626. 626 Image sfBarImg = insertImage(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", 0, 0, false);
  627. 627 g.drawImage(sfBarImg, startWidth + 80, startHeight + 191, null);
  628. 628
  629. 629 //设置字体
  630. 630 Font fontSfBarCode = new Font("黑体",Font.BOLD,35);
  631. 631 g.setFont(fontSfBarCode);
  632. 632
  633. 633 String sfBarCodeStr = orderParam.getMailNo();
  634. 634 sfBarCodeStr = sfBarCodeStr.replaceAll("(.{3})", "$1 ");
  635. 635 g.drawString("母单号 " + sfBarCodeStr, startWidth + 150, startHeight + 355);
  636. 636
  637. 637 //绘制产品类型框
  638. 638 g.drawLine(startWidth + 750, startHeight + 240, startWidth + 1198, startHeight + 240);
  639. 639 Font fontSfProtypSub = new Font("黑体", Font.BOLD, 50);
  640. 640 g.setFont(fontSfProtypSub);
  641. 641
  642. 642 //产品类型字符串
  643. 643 String subSfProTypStr = orderParam.getExpressTyp();
  644. 644 g.drawString(subSfProTypStr, startWidth + 850, startHeight + 220);
  645. 645
  646. 646 //目的地栏的绘制
  647. 647 g.drawLine(startWidth, startHeight + 563, startWidth + 1198, startHeight + 563);
  648. 648 g.drawLine(startWidth + 80, startHeight + 396, startWidth + 80, startHeight + 563);
  649. 649
  650. 650 //目的地填写
  651. 651 Font fontDest = new Font("黑体", Font.BOLD, 30);
  652. 652 g.setFont(fontDest);
  653. 653 //目的地标题
  654. 654 String destTitleStr = "目的地";
  655. 655 char[] destTitleArray = destTitleStr.toCharArray();
  656. 656 int destTitleWidth = startWidth + 40;
  657. 657 int destTitleHeight = startHeight + 460;
  658. 658 for (int i = 0; i < destTitleStr.length(); i++) {
  659. 659 g.drawString(String.valueOf(destTitleArray[i]), destTitleWidth, destTitleHeight);
  660. 660 destTitleHeight += 33;
  661. 661 }
  662. 662
  663. 663 //目的地代码
  664. 664 Font fontDestCode = new Font("Arial", Font.BOLD, 214);
  665. 665 g.setFont(fontDestCode);
  666. 666 //目的地代码字符串
  667. 667 String destCode = orderParam.getDestCode() == null ? "" : orderParam.getDestCode();
  668. 668 g.drawString(destCode, startWidth + 90, startHeight + 555);
  669. 669
  670. 670 //收件人表格栏
  671. 671 g.drawLine(startWidth, startHeight + 720, startWidth + 1198, startHeight + 720);
  672. 672 g.drawLine(startWidth + 80, startHeight + 563, startWidth + 80, startHeight + 720);
  673. 673
  674. 674 //设置收件人标题字体
  675. 675 Font fontRevicer = new Font("黑体", Font.BOLD, 25);
  676. 676 g.setFont(fontRevicer);
  677. 677 //收件人标题字符串
  678. 678 String revicerTitleStr = "收件人";
  679. 679 char[] revicerTitleArray = revicerTitleStr.toCharArray();
  680. 680 int revicerTitleWidth = startWidth + 40;
  681. 681 int revicerTitleHeight = startHeight + 620;
  682. 682 for (int i = 0; i < revicerTitleStr.length(); i++) {
  683. 683 g.drawString(String.valueOf(revicerTitleArray[i]), revicerTitleWidth, revicerTitleHeight);
  684. 684 revicerTitleHeight += 31;
  685. 685 }
  686. 686
  687. 687 /*
  688. 688 * 收件人详细信息
  689. 689 * */
  690. 690 String dContact = orderParam.getdContact(); //收件人姓名
  691. 691 String dTel = orderParam.getdTel(); //联系电话
  692. 692 String dMoblie = orderParam.getdMobile(); //联系人手机号
  693. 693 String dCompany = orderParam.getdCompany(); //公司名称
  694. 694 String dProvince = orderParam.getdProvince();
  695. 695 String dCity = orderParam.getdCity();
  696. 696 String dCounty = orderParam.getdCounty();
  697. 697 String dAddress = orderParam.getdAddress(); //详细地址
  698. 698
  699. 699 String revicerInfo = dContact + " " + dTel + " " + dMoblie + " " + dCompany;
  700. 700 dAddress = dProvince + dCity + dCounty + dAddress;
  701. 701
  702. 702 //设置收件人信息字体
  703. 703 Font fontRevicerInfo = new Font("黑体", Font.BOLD, 35);
  704. 704 g.setFont(fontRevicerInfo);
  705. 705 g.drawString(revicerInfo, startWidth + 90, startHeight + 610);
  706. 706 //设置收件人详细地址字体
  707. 707 Font fontReviceAddress = new Font("黑体", Font.BOLD, 40);
  708. 708 g.setFont(fontReviceAddress);
  709. 709 if (dAddress.length() > 30) {
  710. 710 g.drawString(dAddress.substring(0, 30), startWidth + 90, startHeight + 655);
  711. 711 g.drawString(dAddress.substring(30, dAddress.length()), startWidth + 90, startHeight + 700);
  712. 712 } else {
  713. 713 g.drawString(dAddress, startWidth + 90, startHeight + 655);
  714. 714 }
  715. 715
  716. 716 //绘制寄件人表格
  717. 717 g.drawLine(startWidth, startHeight + 828, startWidth + 1198, startHeight +828);
  718. 718 g.drawLine(startWidth + 80, startHeight + 720, startWidth + 80, startHeight + 828);
  719. 719
  720. 720 //设置寄件人标题字体
  721. 721 Font fontSender = new Font("黑体", Font.BOLD, 25);
  722. 722 g.setFont(fontSender);
  723. 723 //寄件人标题字符串
  724. 724 String senderTitleStr = "寄件人";
  725. 725 char[] senderTitleArray = senderTitleStr.toCharArray();
  726. 726 int senderTitleWidth = startWidth + 40;
  727. 727 int senderTitleHeight = startHeight + 752;
  728. 728 for (int i = 0; i < senderTitleStr.length(); i++) {
  729. 729 g.drawString(String.valueOf(senderTitleArray[i]), senderTitleWidth, senderTitleHeight);
  730. 730 senderTitleHeight += 27;
  731. 731 }
  732. 732
  733. 733
  734. 734 /*
  735. 735 * 寄件人信息
  736. 736 * **/
  737. 737 String jContact = orderParam.getjContact(); //寄件人姓名
  738. 738 String jTel = orderParam.getjTel(); //寄件人联系电话
  739. 739 String jMobile = orderParam.getjMobile();
  740. 740 String jCompany = orderParam.getjCompany();
  741. 741 String jProvince = orderParam.getjProvince();
  742. 742 String jCity = orderParam.getjCity();
  743. 743 String jCounty = orderParam.getjCounty();
  744. 744 String jAddress = orderParam.getjAddress();
  745. 745
  746. 746 String senderInfo = jContact + " " + jTel + " " + jMobile + " " + jCompany;
  747. 747 jAddress = jProvince + jCity + jCounty + jAddress;
  748. 748
  749. 749 //设置寄件人信息字体
  750. 750 Font fontSenderInfo = new Font("黑体", Font.PLAIN, 30);
  751. 751 g.setFont(fontSenderInfo);
  752. 752 g.drawString(senderInfo, startWidth + 90, startHeight + 752);
  753. 753
  754. 754 //设置寄件人详细地址字体
  755. 755 Font fontSenderAddress = new Font("黑体", Font.PLAIN, 30);
  756. 756 g.setFont(fontSenderAddress);
  757. 757 if (jAddress.length() > 27) {
  758. 758 g.drawString(jAddress.substring(0, 27), startWidth + 90, startHeight + 785);
  759. 759 g.drawString(jAddress.substring(27, jAddress.length()), startWidth + 90, startHeight + 817);
  760. 760 } else {
  761. 761 g.drawString(jAddress, startWidth + 90, startHeight + 785);
  762. 762 }
  763. 763
  764. 764 //绘制派送方式表格
  765. 765 g.drawLine(startWidth + 850, startHeight + 720, startWidth + 850, startHeight + 1080);
  766. 766 //设置派送类型字体
  767. 767 Font fontSenTyp = new Font("黑体", Font.BOLD, 55);
  768. 768 g.setFont(fontSenTyp);
  769. 769
  770. 770 //快递详细信息表格
  771. 771 g.drawLine(startWidth, startHeight + 972, startWidth + 850, startHeight + 972);
  772. 772
  773. 773 //快递详细信息
  774. 774 Font cellFont = new Font("黑体", Font.PLAIN, 22);
  775. 775 g.setFont(cellFont);
  776. 776 String[][] cellValue1 = {
  777. 777 {"付款方式:",orderParam.getPayMethod()},
  778. 778 {"月结账号:",orderParam.getMonthSettleNo()},
  779. 779 {"第三方地区:",orderParam.getThridArea()},
  780. 780 {"实际重量:",orderParam.getRealWeight()}
  781. 781 };
  782. 782 int cellLineHeight = startHeight + 855;
  783. 783 for (int i = 0; i < cellValue1.length; i++) {
  784. 784 g.drawString(cellValue1[i][0], startWidth + 30, cellLineHeight);
  785. 785 g.drawString(cellValue1[i][1], startWidth + 155, cellLineHeight);
  786. 786 cellLineHeight += 30;
  787. 787 }
  788. 788 String[][] cellValue2 = {
  789. 789 {"计费重量:",orderParam.getChargWeight()},
  790. 790 {"声明价值:",orderParam.getDeclarPrice()},
  791. 791 {"保价费用:",orderParam.getSupportFee()},
  792. 792 {"定时派送:",orderParam.getSendTime()}
  793. 793 };
  794. 794 cellLineHeight = startHeight + 855;
  795. 795 for (int i = 0; i < cellValue2.length; i++) {
  796. 796 g.drawString(cellValue2[i][0], startWidth + 355, cellLineHeight);
  797. 797 g.drawString(cellValue2[i][1], startWidth + 470, cellLineHeight);
  798. 798 cellLineHeight += 30;
  799. 799 }
  800. 800 String[][] cellValue3 = {
  801. 801 {"包装费用:",orderParam.getPackFee()},
  802. 802 {"运费:",orderParam.getFreight()},
  803. 803 {"费用合计:",orderParam.getSumFee()}
  804. 804 };
  805. 805 cellLineHeight = startHeight + 855;
  806. 806 for (int i = 0; i < cellValue3.length; i++) {
  807. 807 g.drawString(cellValue3[i][0], startWidth + 655, cellLineHeight);
  808. 808 g.drawString(cellValue3[i][1], startWidth + 770, cellLineHeight);
  809. 809 cellLineHeight += 30;
  810. 810 }
  811. 811
  812. 812 //转寄协议客户
  813. 813 Font fowardFont = new Font("黑体", Font.BOLD, 25);
  814. 814 g.setFont(fowardFont);
  815. 815 String fowardStr = "转寄协议客户";
  816. 816 g.drawString(fowardStr, startWidth + 693, startHeight + 965);
  817. 817
  818. 818 //托寄物
  819. 819 g.drawLine(startWidth + 80, startHeight + 972, startWidth + 80, startHeight + 1080);
  820. 820 //托寄物标题字符串
  821. 821 String articleTitleStr = "托寄物";
  822. 822 char[] articleTitleArray = articleTitleStr.toCharArray();
  823. 823 int articleTitleWidth = startWidth + 40;
  824. 824 int articleTitleHeight = startHeight + 1005;
  825. 825 for (int i = 0; i < articleTitleStr.length(); i++) {
  826. 826 g.drawString(String.valueOf(articleTitleArray[i]), articleTitleWidth, articleTitleHeight);
  827. 827 articleTitleHeight += 31;
  828. 828 }
  829. 829
  830. 830 //收件员信息
  831. 831 g.drawLine(startWidth + 600, startHeight + 972, startWidth + 600, startHeight + 1080);
  832. 832 Font receDriverFont = new Font("黑体", Font.PLAIN, 25);
  833. 833 g.setFont(receDriverFont);
  834. 834 String[][] receDriverStrs = {
  835. 835 {"收件员:",orderParam.getReviceDriver()},
  836. 836 {"寄件日期:",orderParam.getSendDate()},
  837. 837 {"派件员:",orderParam.getSendDriver()}
  838. 838 };
  839. 839 cellLineHeight = startHeight + 1005;
  840. 840 for (int i = 0; i < receDriverStrs.length; i++) {
  841. 841 g.drawString(receDriverStrs[i][0], startWidth + 610, cellLineHeight);
  842. 842 g.drawString(receDriverStrs[i][1], startWidth + 730, cellLineHeight);
  843. 843 cellLineHeight += 31;
  844. 844 }
  845. 845 //签名
  846. 846 String signStr = "签名:";
  847. 847 g.drawString(signStr, startWidth + 860, startHeight + 860);
  848. 848 //月日
  849. 849 String monthDay = "月 日";
  850. 850 g.drawString(monthDay, startWidth + 1100, startHeight + 1070);
  851. 851
  852. 852 //母单与子单分割线
  853. 853 g.drawLine(startWidth, startHeight + 1080, startWidth + 1198, startHeight + 1080);
  854. 854 //--------------------------- 第二联信息 ----------------------------------//
  855. 855 g.drawLine(startWidth, startHeight + 1260, startWidth + 1198, startHeight + 1260);
  856. 856 g.drawLine(startWidth + 300, startHeight + 1080, startWidth + 300, startHeight + 1260);
  857. 857 //插入Logo
  858. 858 Image sublogoImg = insertImage(LOGO_PATH, LOGO_WIDTH, LOGO_HEIGHT, true);
  859. 859 g.drawImage(sublogoImg, startWidth + 40, startHeight + 1085, null);
  860. 860 //插入第二个logo (客服电话)
  861. 861 Image sublogoTelImg = insertImage(LOGO_TEL_PATH, LOGO_TEL_WIDTH, LOGO_TEL_HEIGHT, true);
  862. 862 g.drawImage(sublogoTelImg, startWidth + 50, startHeight + 1170, null);
  863. 863
  864. 864 //导入子单条码
  865. 865 Image sfSubBarImg = insertImage(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg", 0, 0, false);
  866. 866 g.drawImage(sfSubBarImg, startWidth + 400, startHeight + 1095, null);
  867. 867
  868. 868 //子单号
  869. 869 Font subOrderFont = new Font("黑体", Font.BOLD, 30);
  870. 870 g.setFont(subOrderFont);
  871. 871 g.drawString("母单号 " + sfBarCodeStr, startWidth + 460, startHeight + 1250);
  872. 872
  873. 873 //绘制寄件人表格 子单
  874. 874 g.drawLine(startWidth, startHeight + 1379, startWidth + 1198, startHeight + 1379);
  875. 875 g.drawLine(startWidth + 80, startHeight + 1260, startWidth + 80, startHeight + 1379);
  876. 876 //设置寄件人标题字体
  877. 877 Font fontsubSender = new Font("黑体", Font.BOLD, 30);
  878. 878 g.setFont(fontsubSender);
  879. 879 //寄件人标题字符串
  880. 880 String senderSubTitleStr = "寄件人";
  881. 881 char[] senderSubTitleArray = senderSubTitleStr.toCharArray();
  882. 882 int senderSubTitleWidth = startWidth + 40;
  883. 883 int senderSubTitleHeight = startHeight + 1295;
  884. 884 for (int i = 0; i < senderSubTitleStr.length(); i++) {
  885. 885 g.drawString(String.valueOf(senderSubTitleArray[i]), senderSubTitleWidth, senderSubTitleHeight);
  886. 886 senderSubTitleHeight += 33;
  887. 887 }
  888. 888 Font subJInfoFont = new Font("黑体", Font.PLAIN, 35);
  889. 889 g.setFont(subJInfoFont);
  890. 890 g.drawString(senderInfo, startWidth + 110, startHeight + 1295);
  891. 891
  892. 892 if (jAddress.length() > 30) {
  893. 893 g.drawString(jAddress.substring(0, 30), startWidth + 110, startHeight + 1330);
  894. 894 g.drawString(jAddress.substring(30, jAddress.length()), startWidth + 110, startHeight + 1365);
  895. 895 } else {
  896. 896 g.drawString(jAddress, startWidth + 110, startHeight + 1330);
  897. 897 }
  898. 898
  899. 899 //绘制收件人表格 子单
  900. 900 g.drawLine(startWidth, startHeight + 1499, startWidth + 1198, startHeight + 1499);
  901. 901 g.drawLine(startWidth + 80, startHeight + 1379, startWidth + 80, startHeight + 1499);
  902. 902 //收件人标题字符串
  903. 903 Font fontsubReveicer = new Font("黑体", Font.BOLD, 30);
  904. 904 g.setFont(fontsubReveicer);
  905. 905 String revicerSubTitleStr = "收件人";
  906. 906 char[] revicerSubTitleArray = revicerSubTitleStr.toCharArray();
  907. 907 int revicerSubTitleWidth = startWidth + 40;
  908. 908 int revicerSubTitleHeight = startHeight + 1415;
  909. 909 for (int i = 0; i < revicerSubTitleStr.length(); i++) {
  910. 910 g.drawString(String.valueOf(revicerSubTitleArray[i]), revicerSubTitleWidth, revicerSubTitleHeight);
  911. 911 revicerSubTitleHeight += 33;
  912. 912 }
  913. 913
  914. 914 Font subDInfoFont = new Font("黑体", Font.PLAIN, 35);
  915. 915 g.setFont(subDInfoFont);
  916. 916 g.drawString(revicerInfo, startWidth + 110, startHeight + 1415);
  917. 917 if (dAddress.length() > 30) {
  918. 918 g.drawString(dAddress.substring(0, 30), startWidth + 110, startHeight + 1450);
  919. 919 g.drawString(dAddress.substring(30, dAddress.length()), startWidth + 110, startHeight + 1486);
  920. 920 } else {
  921. 921 g.drawString(dAddress, startWidth + 110, startHeight + 1450);
  922. 922 }
  923. 923 g.dispose();
  924. 924 File sfbarFile = new File(picPath + "SFBarCoding_" + orderParam.getMailNo() + ".jpg");
  925. 925 if (sfbarFile.exists() && sfbarFile.isFile()) {
  926. 926 sfbarFile.delete();
  927. 927 }
  928. 928 //生成订单图片
  929. 929 createImage(picPath + "SfOrderImage_" + orderParam.getMailNo() + ".jpg");
  930. 930 if (isCompress) {
  931. 931 compressImg(picPath + "SfOrderImage_" + orderParam.getMailNo() + ".jpg", imgWidth, imgHeidht);
  932. 932 }
  933. 933 logger.info("订单生成成功. " + picPath + "SfOrderImage_" + orderParam.getMailNo() + ".jpg");
  934. 934 return true;
  935. 935 }
  936. 936 } catch (Exception e) {
  937. 937 e.printStackTrace();
  938. 938 }
  939. 939 return false;
  940. 940 }
  941. 941
  942. 942 /**
  943. 943 * 水平翻转图像 顺时针旋转90度、左右翻转
  944. 944 * @param bufferedimage 目标图像
  945. 945 * @return
  946. 946 */
  947. 947 private static BufferedImage rotate90DX(BufferedImage bi)
  948. 948 {
  949. 949 int width = bi.getWidth();
  950. 950 int height = bi.getHeight();
  951. 951
  952. 952 BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
  953. 953
  954. 954 for(int i=0; i<width; i++)
  955. 955 for(int j=0; j<height; j++)
  956. 956 biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));
  957. 957
  958. 958 return biFlip;
  959. 959 }
  960. 960
  961. 961
  962. 962 /**
  963. 963 * 水平翻转图像 逆时针旋转90度、左右翻转
  964. 964 * @param bufferedimage 目标图像
  965. 965 * @return
  966. 966 */
  967. 967 private static BufferedImage rotate90SX(BufferedImage bi)
  968. 968 {
  969. 969 int width = bi.getWidth();
  970. 970 int height = bi.getHeight();
  971. 971
  972. 972 BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
  973. 973
  974. 974 for(int i=0; i<width; i++)
  975. 975 for(int j=0; j<height; j++)
  976. 976 biFlip.setRGB(j, i, bi.getRGB(i, j));
  977. 977
  978. 978 return biFlip;
  979. 979 }
  980. 980
  981. 981 /**
  982. 982 * 根据规定尺寸压缩图片
  983. 983 * @param imgPath 图片路径
  984. 984 * @param width 图片宽度
  985. 985 * @param height 图片高度
  986. 986 * */
  987. 987 private static void compressImg(String imgPath, int width, int height){
  988. 988 /**
  989. 989 * 设置条码图片的尺寸
  990. 990 * */
  991. 991 BufferedInputStream bis = null;
  992. 992 BufferedOutputStream out = null;
  993. 993 FileOutputStream fis = null;
  994. 994 try {
  995. 995 File sfFile = new File(imgPath);
  996. 996 if (sfFile.isFile() && sfFile.exists()) {
  997. 997 //读取图片
  998. 998 bis = new BufferedInputStream(new FileInputStream(imgPath));
  999. 999 //转换成图片对象
  1000. 1000 Image bi = ImageIO.read(bis).getScaledInstance(width, height, Image.SCALE_SMOOTH);
  1001. 1001 //构建图片流 设置图片宽和高
  1002. 1002 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  1003. 1003 //绘制改变尺寸后的图
  1004. 1004 tag.getGraphics().drawImage(bi, 0, 0,width, height, null);
  1005. 1005 //保存图片
  1006. 1006 fis = new FileOutputStream(imgPath);
  1007. 1007 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fis);
  1008. 1008 JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
  1009. 1009 //设置压缩图片质量
  1010. 1010 jep.setQuality(3f, true);
  1011. 1011 encoder.encode(tag, jep);
  1012. 1012 }
  1013. 1013 } catch (Exception e) {
  1014. 1014 e.printStackTrace();
  1015. 1015 } finally {
  1016. 1016 try {
  1017. 1017 if (fis != null) fis.close();
  1018. 1018 if (out != null) out.close();
  1019. 1019 if (bis != null) bis.close();
  1020. 1020 } catch (Exception e2) {
  1021. 1021 e2.printStackTrace();
  1022. 1022 }
  1023. 1023 }
  1024. 1024 }
  1025. 1025
  1026. 1026 //创建目录
  1027. 1027 private static boolean createDir(String destDirName){
  1028. 1028 try {
  1029. 1029 File file = new File(destDirName);
  1030. 1030 if (destDirName.endsWith(File.separator))
  1031. 1031 destDirName += File.separator;
  1032. 1032 if (file.mkdir())
  1033. 1033 return true;
  1034. 1034 } catch (Exception e) {
  1035. 1035 e.printStackTrace();
  1036. 1036 }
  1037. 1037 return false;
  1038. 1038 }
  1039. 1039
  1040. 1040 //删除目录
  1041. 1041 public static boolean removeDir(File dir){
  1042. 1042 File[] files = dir.listFiles();
  1043. 1043 for (File file : files) {
  1044. 1044 if (file.isDirectory()) {
  1045. 1045 return removeDir(file);
  1046. 1046 } else {
  1047. 1047 return file.delete();
  1048. 1048 }
  1049. 1049 }
  1050. 1050 return dir.delete();
  1051. 1051 }
  1052. 1052
  1053. 1053 //删除目录
  1054. 1054 public static boolean deleteMk(String dirPath){
  1055. 1055 File dirFile = new File(dirPath);
  1056. 1056 if (dirFile.isDirectory()) {
  1057. 1057 File[] files = dirFile.listFiles();
  1058. 1058 for (int i = 0; i < files.length; i++) {
  1059. 1059 files[i].delete();
  1060. 1060 }
  1061. 1061 }
  1062. 1062 return dirFile.delete();
  1063. 1063 }
  1064. 1064
  1065. 1065 /**
  1066. 1066 * 把文件转换为byte[]
  1067. 1067 * @param filePath 文件路径
  1068. 1068 * */
  1069. 1069 public static byte[] getFileBytes(String filePath){
  1070. 1070 byte[] buffer = null;
  1071. 1071 FileInputStream fis = null;
  1072. 1072 ByteArrayOutputStream bos = null;
  1073. 1073 try {
  1074. 1074 File file = new File(filePath);
  1075. 1075 fis = new FileInputStream(file);
  1076. 1076 bos = new ByteArrayOutputStream(1000);
  1077. 1077 byte[] b = new byte[1000];
  1078. 1078 int n;
  1079. 1079 while ((n = fis.read(b)) != -1) {
  1080. 1080 bos.write(b, 0, n);
  1081. 1081 }
  1082. 1082 buffer = bos.toByteArray();
  1083. 1083 } catch (FileNotFoundException e) {
  1084. 1084 e.printStackTrace();
  1085. 1085 } catch (IOException e) {
  1086. 1086 e.printStackTrace();
  1087. 1087 } finally {
  1088. 1088 try {
  1089. 1089 if (bos != null) {
  1090. 1090 bos.close();
  1091. 1091 }
  1092. 1092 if (fis != null) {
  1093. 1093 fis.close();
  1094. 1094 }
  1095. 1095 } catch (Exception e2) {
  1096. 1096 e2.printStackTrace();
  1097. 1097 }
  1098. 1098 }
  1099. 1099 return buffer;
  1100. 1100 }
  1101. 1101 }

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

SfPrintOrderParam.java

  1. 1 package testNetty.wu;
  2. 2
  3. 3 import java.io.Serializable;
  4. 4 import java.util.ArrayList;
  5. 5 import java.util.List;
  6. 6
  7. 7 /**
  8. 8 * 打印订单所需要的参数
  9. 9 * */
  10. 10 public class SfPrintOrderParam implements Serializable{
  11. 11
  12. 12 /**
  13. 13 *
  14. 14 */
  15. 15 private static final long serialVersionUID = 1L;
  16. 16
  17. 17 //陆运标识
  18. 18 private String earthCarryFlag = "";
  19. 19 //母运单号
  20. 20 private String mailNo = "";
  21. 21 //子运单号
  22. 22 private List<String> subMailNos = new ArrayList<String>();
  23. 23 //目的地代码
  24. 24 private String destCode = "";
  25. 25 //产品类型
  26. 26 private String expressTyp = "";
  27. 27 //收件人姓名
  28. 28 private String dContact = "";
  29. 29 //收件人公司名称
  30. 30 private String dCompany = "";
  31. 31 //收件人联系方式
  32. 32 private String dTel = "";
  33. 33 //收件人手机号
  34. 34 private String dMobile = "";
  35. 35 //收件人省份
  36. 36 private String dProvince = "";
  37. 37 //收件人城市
  38. 38 private String dCity = "";
  39. 39 //收件人区、县
  40. 40 private String dCounty = "";
  41. 41 //收件人详细地址
  42. 42 private String dAddress = "";
  43. 43 //寄件人姓名
  44. 44 private String jContact = "";
  45. 45 //寄件人公司名称
  46. 46 private String jCompany = "";
  47. 47 //寄件人联系方式
  48. 48 private String jTel = "";
  49. 49 //寄件人手机号
  50. 50 private String jMobile = "";
  51. 51 //寄件人省份
  52. 52 private String jProvince = "";
  53. 53 //寄件人城市
  54. 54 private String jCity = "";
  55. 55 //寄件人区、县
  56. 56 private String jCounty = "";
  57. 57 //寄件人详细地址
  58. 58 private String jAddress = "";
  59. 59 //付款方式
  60. 60 private String payMethod = "";
  61. 61 //月结账号
  62. 62 private String monthSettleNo = "";
  63. 63 //第三方地区
  64. 64 private String thridArea = "";
  65. 65 //实际重量
  66. 66 private String realWeight = "";
  67. 67 //计费重量
  68. 68 private String chargWeight = "";
  69. 69 //声明价值
  70. 70 private String declarPrice = "";
  71. 71 //保价费用
  72. 72 private String supportFee = "";
  73. 73 //定时派送时间
  74. 74 private String sendTime = "";
  75. 75 //包装费用
  76. 76 private String packFee = "";
  77. 77 //运费
  78. 78 private String freight = "";
  79. 79 //费用合计
  80. 80 private String sumFee = "";
  81. 81 //收件员编号
  82. 82 private String reviceDriver = "";
  83. 83 //寄件日期
  84. 84 private String sendDate = "";
  85. 85 //派件员
  86. 86 private String sendDriver = "";
  87. 87 //拖寄物信息
  88. 88 private String cargoContent = "";
  89. 89
  90. 90 public SfPrintOrderParam() {
  91. 91 super();
  92. 92 // TODO Auto-generated constructor stub
  93. 93 }
  94. 94
  95. 95 public String getEarthCarryFlag() {
  96. 96 return earthCarryFlag;
  97. 97 }
  98. 98 public void setEarthCarryFlag(String earthCarryFlag) {
  99. 99 this.earthCarryFlag = earthCarryFlag;
  100. 100 }
  101. 101 public String getMailNo() {
  102. 102 return mailNo;
  103. 103 }
  104. 104 public void setMailNo(String mailNo) {
  105. 105 this.mailNo = mailNo;
  106. 106 }
  107. 107 public List<String> getSubMailNos() {
  108. 108 return subMailNos;
  109. 109 }
  110. 110 public String getExpressTyp() {
  111. 111 return expressTyp;
  112. 112 }
  113. 113
  114. 114 public void setExpressTyp(String expressTyp) {
  115. 115 this.expressTyp = expressTyp;
  116. 116 }
  117. 117
  118. 118 public void setSubMailNos(List<String> subMailNos) {
  119. 119 this.subMailNos = subMailNos;
  120. 120 }
  121. 121 public String getDestCode() {
  122. 122 return destCode;
  123. 123 }
  124. 124 public void setDestCode(String destCode) {
  125. 125 this.destCode = destCode;
  126. 126 }
  127. 127 public String getdContact() {
  128. 128 return dContact;
  129. 129 }
  130. 130 public void setdContact(String dContact) {
  131. 131 this.dContact = dContact;
  132. 132 }
  133. 133 public String getdCompany() {
  134. 134 return dCompany;
  135. 135 }
  136. 136 public void setdCompany(String dCompany) {
  137. 137 this.dCompany = dCompany;
  138. 138 }
  139. 139 public String getdTel() {
  140. 140 return dTel;
  141. 141 }
  142. 142 public void setdTel(String dTel) {
  143. 143 this.dTel = dTel;
  144. 144 }
  145. 145 public String getdMobile() {
  146. 146 return dMobile;
  147. 147 }
  148. 148 public void setdMobile(String dMobile) {
  149. 149 this.dMobile = dMobile;
  150. 150 }
  151. 151 public String getdProvince() {
  152. 152 return dProvince;
  153. 153 }
  154. 154 public void setdProvince(String dProvince) {
  155. 155 this.dProvince = dProvince;
  156. 156 }
  157. 157 public String getdCity() {
  158. 158 return dCity;
  159. 159 }
  160. 160 public void setdCity(String dCity) {
  161. 161 this.dCity = dCity;
  162. 162 }
  163. 163 public String getdCounty() {
  164. 164 return dCounty;
  165. 165 }
  166. 166 public void setdCounty(String dCounty) {
  167. 167 this.dCounty = dCounty;
  168. 168 }
  169. 169 public String getdAddress() {
  170. 170 return dAddress;
  171. 171 }
  172. 172 public void setdAddress(String dAddress) {
  173. 173 this.dAddress = dAddress;
  174. 174 }
  175. 175 public String getjContact() {
  176. 176 return jContact;
  177. 177 }
  178. 178 public void setjContact(String jContact) {
  179. 179 this.jContact = jContact;
  180. 180 }
  181. 181 public String getjCompany() {
  182. 182 return jCompany;
  183. 183 }
  184. 184 public void setjCompany(String jCompany) {
  185. 185 this.jCompany = jCompany;
  186. 186 }
  187. 187 public String getjTel() {
  188. 188 return jTel;
  189. 189 }
  190. 190 public void setjTel(String jTel) {
  191. 191 this.jTel = jTel;
  192. 192 }
  193. 193 public String getjMobile() {
  194. 194 return jMobile;
  195. 195 }
  196. 196 public void setjMobile(String jMobile) {
  197. 197 this.jMobile = jMobile;
  198. 198 }
  199. 199 public String getjProvince() {
  200. 200 return jProvince;
  201. 201 }
  202. 202 public void setjProvince(String jProvince) {
  203. 203 this.jProvince = jProvince;
  204. 204 }
  205. 205 public String getjCity() {
  206. 206 return jCity;
  207. 207 }
  208. 208 public void setjCity(String jCity) {
  209. 209 this.jCity = jCity;
  210. 210 }
  211. 211 public String getjCounty() {
  212. 212 return jCounty;
  213. 213 }
  214. 214 public void setjCounty(String jCounty) {
  215. 215 this.jCounty = jCounty;
  216. 216 }
  217. 217 public String getjAddress() {
  218. 218 return jAddress;
  219. 219 }
  220. 220 public void setjAddress(String jAddress) {
  221. 221 this.jAddress = jAddress;
  222. 222 }
  223. 223 public String getPayMethod() {
  224. 224 return payMethod;
  225. 225 }
  226. 226 public void setPayMethod(String payMethod) {
  227. 227 this.payMethod = payMethod;
  228. 228 }
  229. 229 public String getMonthSettleNo() {
  230. 230 return monthSettleNo;
  231. 231 }
  232. 232 public void setMonthSettleNo(String monthSettleNo) {
  233. 233 this.monthSettleNo = monthSettleNo;
  234. 234 }
  235. 235 public String getThridArea() {
  236. 236 return thridArea;
  237. 237 }
  238. 238 public void setThridArea(String thridArea) {
  239. 239 this.thridArea = thridArea;
  240. 240 }
  241. 241 public String getRealWeight() {
  242. 242 return realWeight;
  243. 243 }
  244. 244 public void setRealWeight(String realWeight) {
  245. 245 this.realWeight = realWeight;
  246. 246 }
  247. 247 public String getChargWeight() {
  248. 248 return chargWeight;
  249. 249 }
  250. 250 public void setChargWeight(String chargWeight) {
  251. 251 this.chargWeight = chargWeight;
  252. 252 }
  253. 253 public String getDeclarPrice() {
  254. 254 return declarPrice;
  255. 255 }
  256. 256 public void setDeclarPrice(String declarPrice) {
  257. 257 this.declarPrice = declarPrice;
  258. 258 }
  259. 259 public String getSupportFee() {
  260. 260 return supportFee;
  261. 261 }
  262. 262 public void setSupportFee(String supportFee) {
  263. 263 this.supportFee = supportFee;
  264. 264 }
  265. 265 public String getSendTime() {
  266. 266 return sendTime;
  267. 267 }
  268. 268 public void setSendTime(String sendTime) {
  269. 269 this.sendTime = sendTime;
  270. 270 }
  271. 271 public String getPackFee() {
  272. 272 return packFee;
  273. 273 }
  274. 274 public void setPackFee(String packFee) {
  275. 275 this.packFee = packFee;
  276. 276 }
  277. 277 public String getFreight() {
  278. 278 return freight;
  279. 279 }
  280. 280 public void setFreight(String freight) {
  281. 281 this.freight = freight;
  282. 282 }
  283. 283 public String getSumFee() {
  284. 284 return sumFee;
  285. 285 }
  286. 286 public void setSumFee(String sumFee) {
  287. 287 this.sumFee = sumFee;
  288. 288 }
  289. 289 public String getReviceDriver() {
  290. 290 return reviceDriver;
  291. 291 }
  292. 292 public void setReviceDriver(String reviceDriver) {
  293. 293 this.reviceDriver = reviceDriver;
  294. 294 }
  295. 295 public String getSendDate() {
  296. 296 return sendDate;
  297. 297 }
  298. 298 public void setSendDate(String sendDate) {
  299. 299 this.sendDate = sendDate;
  300. 300 }
  301. 301 public String getSendDriver() {
  302. 302 return sendDriver;
  303. 303 }
  304. 304 public void setSendDriver(String sendDriver) {
  305. 305 this.sendDriver = sendDriver;
  306. 306 }
  307. 307
  308. 308 public String getCargoContent() {
  309. 309 return cargoContent;
  310. 310 }
  311. 311 public void setCargoContent(String cargoContent) {
  312. 312 this.cargoContent = cargoContent;
  313. 313 }
  314. 314
  315. 315 }

四、测试运行此程序

  1. 1 package testNetty.wu;
  2. 2
  3. 3 /**
  4. 4 *
  5. 5 */
  6. 6 public class App
  7. 7 {
  8. 8 public static void main( String[] args )
  9. 9 {
  10. 10 SfPrintOrderParam param = new SfPrintOrderParam();
  11. 11
  12. 12 param.setMailNo("883987638272");
  13. 13 param.setEarthCarryFlag("E");
  14. 14 param.setDestCode("0311");
  15. 15 param.setdContact("李XX");
  16. 16 param.setdCompany("河北北国商城");
  17. 17 param.setdMobile("13588911223");
  18. 18 param.setdProvince("河北省");
  19. 19 param.setdCity("石家庄市");
  20. 20 param.setdCounty("桥西区");
  21. 21 param.setdAddress("华润万象城");
  22. 22 param.setjContact("吴XX");
  23. 23 param.setjMobile("13716533121");
  24. 24 param.setjCompany("北京XXX科技公司");
  25. 25 param.setjProvince("北京");
  26. 26 param.setjCity("北京市");
  27. 27 param.setjCounty("朝阳区");
  28. 28 param.setjCounty("东三环24号XXX大厦");
  29. 29
  30. 30
  31. 31 SFOrderGenerateUtil.generateOrders(
  32. 32 "C:\\Users\\Administrator\\Desktop\\",
  33. 33 param,
  34. 34 "1",
  35. 35 false,
  36. 36 600,
  37. 37 400);
  38. 38 }
  39. 39 }

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

     

 

原文链接:http://www.cnblogs.com/wm-dv/p/11135573.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号