MyQRUtils.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //package com.tld.util;
  2. //
  3. //import com.google.zxing.BarcodeFormat;
  4. //import com.google.zxing.EncodeHintType;
  5. //import com.google.zxing.MultiFormatWriter;
  6. //import com.google.zxing.WriterException;
  7. //import com.google.zxing.common.BitMatrix;
  8. //import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  9. //import org.apache.commons.logging.Log;
  10. //import org.apache.commons.logging.LogFactory;
  11. //import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. //import org.apache.poi.hssf.usermodel.HSSFFont;
  13. //import org.apache.poi.hssf.usermodel.HSSFSheet;
  14. //import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. //import org.apache.poi.ss.usermodel.HorizontalAlignment;
  16. //import org.apache.poi.ss.usermodel.VerticalAlignment;
  17. //import org.apache.poi.xssf.usermodel.*;
  18. //
  19. //import javax.imageio.ImageIO;
  20. //import javax.print.*;
  21. //import javax.print.attribute.*;
  22. //import javax.print.attribute.standard.JobName;
  23. //import javax.print.attribute.standard.MediaPrintableArea;
  24. //import javax.print.attribute.standard.MediaSizeName;
  25. //import javax.print.attribute.standard.OrientationRequested;
  26. //import javax.print.event.PrintJobEvent;
  27. //import javax.print.event.PrintJobListener;
  28. //import java.awt.*;
  29. //import java.awt.image.BufferedImage;
  30. //import java.awt.image.ImageProducer;
  31. //import java.awt.print.PrinterJob;
  32. //import java.io.*;
  33. //import java.util.Hashtable;
  34. //import java.util.Locale;
  35. //
  36. //
  37. ///**
  38. // * 二维码图片打印机工具类
  39. // */
  40. //public class MyQRUtils {
  41. //
  42. // private static final Log logger = LogFactory.getLog(MyQRUtils.class);
  43. //
  44. // private static final int BLACK = 0xFF000000;
  45. // private static final int WHITE = 0xFFFFFFFF;
  46. // private static final int LogoPart = 4;
  47. //
  48. // // 方法中用到的宏定义
  49. // private static final short TWIPS_PER_PIEXL = 15; // 1 Pixel = 1440 TPI / 96 DPI = 15 Twips
  50. // private static final double THGPS_PER_PIEXL = 2.5;
  51. // public static XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("D\\test\\test.xlsx"));;
  52. //
  53. // /**
  54. // * 生成二维码前的配置信息
  55. // * @param content 生成二维码图片内容
  56. // * @param width 二维码图片的宽度
  57. // * @param height 二维码图片的高度
  58. // * @return
  59. // */
  60. // public static BitMatrix setBitMatrix(String content, int width, int height){
  61. // Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  62. // hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  63. // hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //指定纠错等级
  64. // BitMatrix bitMatrix=null;
  65. // try {
  66. // //生成二维码
  67. // bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  68. // } catch (WriterException e) {
  69. // logger.error("生成二维码错误",e);
  70. // }
  71. // return bitMatrix;
  72. // }
  73. //
  74. // /**
  75. // * 将LOGO图片放在二维码中间(水印效果)
  76. // * 将生成的图片以流的形式输出到页面展示
  77. // * @param matrix BitMatrix
  78. // * @param format 图片格式
  79. // * @param outStream 输出流
  80. // * @param logoPath LOGO地址
  81. // * @param showBottomText 二维码图片底部需要显示的问题
  82. // * @throws IOException
  83. // */
  84. // public static void megerToFile(BitMatrix matrix,String format,OutputStream outStream,String logoPath,String showBottomText) throws IOException {
  85. // BufferedImage image = toBufferedImage(matrix);
  86. // Graphics2D gs = image.createGraphics();
  87. //
  88. // //1.加入LOGO水印效果
  89. // if(null != logoPath && !"".equals(logoPath)){
  90. // //1.1 载入LOGO图片
  91. // BufferedImage logoImg = ImageIO.read(new File(logoPath));
  92. // //1.2 考虑到LOGO图片贴到二维码中,建议大小不要超过二维码的1/5;
  93. // int width = image.getWidth() / LogoPart;
  94. // int height = image.getHeight() / LogoPart;
  95. // //1.3 LOGO居中显示
  96. // int x = (image.getWidth() - width) / 2;
  97. // int y = (image.getHeight() - height) / 2;
  98. // gs.drawImage(logoImg, x, y, logoImg.getWidth(), logoImg.getHeight(), null);
  99. // logoImg.flush();
  100. // }
  101. // //2.二维码图片底部插入文字
  102. // if(null != showBottomText && !"".equals(showBottomText)){
  103. // //2.1 设置字体样式
  104. // Font font = new Font("微软雅黑", Font.PLAIN, 14);
  105. // gs.setColor(Color.BLACK);
  106. // gs.setFont(font);
  107. // //2.2 字体显示位置
  108. // int x = (image.getWidth() - getWatermarkLength(showBottomText, gs))/2;
  109. // int y = image.getHeight()-2;
  110. // gs.drawString(showBottomText, x, y);
  111. // }
  112. // gs.dispose();
  113. // ImageIO.write(image, format, outStream);
  114. // }
  115. //
  116. // /**
  117. // * 将LOGO图片放在二维码中间(水印效果)
  118. // * 将生成的图片生成到本地硬盘路径下
  119. // * @param matrix BitMatrix
  120. // * @param format 图片格式
  121. // * @param imagePath 图片存放路径
  122. // * @param logoPath LOGO地址
  123. // * @param showBottomText 二维码图片底部需要显示的问题
  124. // * @throws IOException
  125. // */
  126. // public static void megerToFile2(BitMatrix matrix,String format,String imagePath,String logoPath,String showBottomText) throws IOException {
  127. // BufferedImage image = toBufferedImage(matrix);
  128. // Graphics2D gs = image.createGraphics();
  129. //
  130. // //1.加入LOGO水印效果
  131. // if(null != logoPath && !"".equals(logoPath)){
  132. // BufferedImage logoImg = ImageIO.read(new File(logoPath));
  133. // int width = image.getWidth() / LogoPart;
  134. // int height = image.getHeight() / LogoPart;
  135. // int x = (image.getWidth() - width) / 2;
  136. // int y = (image.getHeight() - height) / 2;
  137. // gs.drawImage(logoImg, x, y, logoImg.getWidth(), logoImg.getHeight(), null);
  138. // logoImg.flush();
  139. // }
  140. //
  141. // //2.二维码图片底部插入文字
  142. // if(null != showBottomText && !"".equals(showBottomText)){
  143. // //2.1 设置字体样式
  144. // Font font = new Font("微软雅黑", Font.PLAIN, 14);
  145. // gs.setColor(Color.BLACK);
  146. // gs.setFont(font);
  147. // //2.2 字体显示位置
  148. // int x = (image.getWidth() - getWatermarkLength(showBottomText, gs))/2;
  149. // int y = image.getHeight()-2;
  150. // gs.drawString(showBottomText, x, y);
  151. // }
  152. // gs.dispose();
  153. // ImageIO.write(image, format, new File(imagePath));
  154. // }
  155. //
  156. // /**
  157. // * 获取水印字体的长度
  158. // * @param fontString
  159. // * @param gs
  160. // * @return
  161. // */
  162. // public static int getWatermarkLength(String fontString,Graphics2D gs){
  163. // return gs.getFontMetrics(gs.getFont()).charsWidth(fontString.toCharArray(),0,fontString.length());
  164. // }
  165. //
  166. // public static BufferedImage toBufferedImage(BitMatrix matrix){
  167. // int width = matrix.getWidth();
  168. // int height = matrix.getHeight();
  169. // BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  170. //
  171. // for(int x=0;x<width;x++){
  172. // for(int y=0;y<height;y++){
  173. // image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
  174. // }
  175. // }
  176. // return image;
  177. // }
  178. //
  179. //
  180. // /**
  181. // * 打印保存的图片/二维码
  182. // * @param file
  183. // * @return
  184. // */
  185. // public static void getPrint(File file ){
  186. // // 构建打印请求属性集
  187. // DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
  188. // // 设置打印格式
  189. // PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
  190. // // 查找所有的打印服务
  191. // PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);
  192. // //循环打印机名称
  193. // for (int i = 0; i < printServices.length; i++) {
  194. // String name = printServices[i].getName();
  195. // System.out.println("打印机名称:"+name);
  196. // }
  197. // // 测试,直接用第一个了
  198. // PrintService printService = printServices[0];
  199. // String printName = printService.getName();
  200. // File picFile = new File(file.toURI());
  201. // if (!picFile.exists() || !picFile.isFile()) {
  202. // return;
  203. // }
  204. // String fileName = picFile.getName();
  205. // // 获取图片后缀名,设置打印文件格式
  206. // String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
  207. // switch (suffix) {
  208. // case "jpg":
  209. // case "jpeg":
  210. // flavor = DocFlavor.INPUT_STREAM.JPEG;
  211. // break;
  212. // case "png":
  213. // flavor = DocFlavor.INPUT_STREAM.PNG;
  214. // break;
  215. // case "pdf":
  216. // flavor = DocFlavor.INPUT_STREAM.PDF;
  217. // break;
  218. // case "gif":
  219. // flavor = DocFlavor.INPUT_STREAM.GIF;
  220. // default:
  221. // break;
  222. // }
  223. // // 设置打印文件名
  224. // pras.add(new JobName(fileName, Locale.CHINA));
  225. // // 设置打印方向
  226. // pras.add(OrientationRequested.LANDSCAPE);
  227. // // 设置打印区域
  228. // MediaPrintableArea mp = new MediaPrintableArea(0, 0, 100, 100, Size2DSyntax.MM);
  229. // pras.add(mp);
  230. // try {
  231. // FileInputStream fis = new FileInputStream(picFile);
  232. // HashDocAttributeSet das = new HashDocAttributeSet();
  233. // // 使用打印服务生成打印任务
  234. // DocPrintJob job = printService.createPrintJob();
  235. // // 获取需要打印的文档类doc
  236. // SimpleDoc doc = new SimpleDoc(fis, flavor, das);
  237. // job.print(doc, pras);
  238. // } catch (PrintException e) {
  239. // e.printStackTrace();
  240. // } catch (FileNotFoundException e) {
  241. // e.printStackTrace();
  242. // }
  243. //
  244. // }
  245. //
  246. // /**
  247. // * 插入图片
  248. // * file 图片路线径
  249. // * c1 excel中的开始列
  250. // * r1 excel中的开始行
  251. // * c2 excel中的结束列
  252. // * r2 excel中的结束行
  253. // * cellWidth 图片宽度
  254. // * cellHeight 图片高度
  255. // *
  256. // */
  257. // public void insertImage(File file, int c1, int r1, int c2, int r2, int cellWidth, int cellHeight) {
  258. // try {
  259. // ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
  260. // Image src;
  261. // System.out.println("==> 图片路径 " + file.getPath());
  262. // if (file.getPath().endsWith(".bmp") || file.getPath().endsWith(".BMP")) {
  263. // BufferedImage bi = ImageIO.read(file);
  264. // ImageProducer producer = bi.getSource();
  265. // Toolkit toolkit = Toolkit.getDefaultToolkit();
  266. // src = toolkit.createImage(producer);
  267. // } else {
  268. // src = Toolkit.getDefaultToolkit().getImage(file.getPath());
  269. // }
  270. // BufferedImage bufferImg = toBufferedImage(src);
  271. // int imageWidthPixel = bufferImg.getWidth();
  272. // int imageHeightPixel = bufferImg.getHeight();
  273. // double imageWidth = pixel2PoiWidth(imageWidthPixel);
  274. // double imageHeight = poiHeight2Pixel(imageHeightPixel);
  275. //
  276. // ImageIO.write(bufferImg, "jpg", byteArrayOut);
  277. // System.out.println();
  278. // // 根据需求对图片进行微调
  279. // XSSFClientAnchor anchor = new XSSFClientAnchor(XSSFShape.EMU_PER_POINT * 5, XSSFShape.EMU_PER_POINT * 5,
  280. // XSSFShape.EMU_PER_POINT * -5, XSSFShape.EMU_PER_POINT * -5, c1, r1, c2, r2);
  281. // anchor.setAnchorType(3);
  282. //
  283. // XSSFPicture pic = patriarch.createPicture(anchor,
  284. // wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
  285. // byteArrayOut.flush();
  286. // byteArrayOut.close();
  287. // } catch (IOException e) {
  288. // e.printStackTrace();
  289. // }
  290. // }
  291. //
  292. // private static int getColsWidth(XSSFSheet sheet, int firstCol, int colCnt) {
  293. // int colWidth = 0;
  294. // for (int i = firstCol; i < firstCol + colCnt; i++) {
  295. // colWidth = colWidth + sheet.getColumnWidth(i);
  296. // }
  297. // return colWidth;
  298. // }
  299. //
  300. // private static int getRowsHeight(XSSFSheet sheet, int firstRow, int rowCnt) {
  301. // int rowHeight = 0;
  302. // for (int i = firstRow; i < firstRow + rowCnt; i++) {
  303. // rowHeight += sheet.getRow(i).getHeight();
  304. // }
  305. // return rowHeight;
  306. // }
  307. // public static double poiHeight2Pixel(double height) {
  308. // return height / TWIPS_PER_PIEXL;
  309. // }
  310. //
  311. // // 像素转poi宽度
  312. // public static double pixel2PoiWidth(double pixel) {
  313. // double numChars = pixel * TWIPS_PER_PIEXL * THGPS_PER_PIEXL;
  314. // return numChars;
  315. // }
  316. //
  317. //}