MyQRUtils.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. //
  18. //import javax.imageio.ImageIO;
  19. //import javax.print.*;
  20. //import javax.print.attribute.*;
  21. //import javax.print.attribute.standard.JobName;
  22. //import javax.print.attribute.standard.MediaPrintableArea;
  23. //import javax.print.attribute.standard.MediaSizeName;
  24. //import javax.print.attribute.standard.OrientationRequested;
  25. //import javax.print.event.PrintJobEvent;
  26. //import javax.print.event.PrintJobListener;
  27. //import java.awt.*;
  28. //import java.awt.image.BufferedImage;
  29. //import java.awt.print.PrinterJob;
  30. //import java.io.*;
  31. //import java.util.Hashtable;
  32. //import java.util.Locale;
  33. //
  34. //
  35. ///**
  36. // * 二维码图片打印机工具类
  37. // */
  38. //public class MyQRUtils {
  39. //
  40. // private static final Log logger = LogFactory.getLog(MyQRUtils.class);
  41. //
  42. // private static final int BLACK = 0xFF000000;
  43. // private static final int WHITE = 0xFFFFFFFF;
  44. // private static final int LogoPart = 4;
  45. //
  46. // /**
  47. // * 生成二维码前的配置信息
  48. // * @param content 生成二维码图片内容
  49. // * @param width 二维码图片的宽度
  50. // * @param height 二维码图片的高度
  51. // * @return
  52. // */
  53. // public static BitMatrix setBitMatrix(String content, int width, int height){
  54. // Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  55. // hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  56. // hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //指定纠错等级
  57. // BitMatrix bitMatrix=null;
  58. // try {
  59. // //生成二维码
  60. // bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  61. // } catch (WriterException e) {
  62. // logger.error("生成二维码错误",e);
  63. // }
  64. // return bitMatrix;
  65. // }
  66. //
  67. // /**
  68. // * 将LOGO图片放在二维码中间(水印效果)
  69. // * 将生成的图片以流的形式输出到页面展示
  70. // * @param matrix BitMatrix
  71. // * @param format 图片格式
  72. // * @param outStream 输出流
  73. // * @param logoPath LOGO地址
  74. // * @param showBottomText 二维码图片底部需要显示的问题
  75. // * @throws IOException
  76. // */
  77. // public static void megerToFile(BitMatrix matrix,String format,OutputStream outStream,String logoPath,String showBottomText) throws IOException {
  78. // BufferedImage image = toBufferedImage(matrix);
  79. // Graphics2D gs = image.createGraphics();
  80. //
  81. // //1.加入LOGO水印效果
  82. // if(null != logoPath && !"".equals(logoPath)){
  83. // //1.1 载入LOGO图片
  84. // BufferedImage logoImg = ImageIO.read(new File(logoPath));
  85. // //1.2 考虑到LOGO图片贴到二维码中,建议大小不要超过二维码的1/5;
  86. // int width = image.getWidth() / LogoPart;
  87. // int height = image.getHeight() / LogoPart;
  88. // //1.3 LOGO居中显示
  89. // int x = (image.getWidth() - width) / 2;
  90. // int y = (image.getHeight() - height) / 2;
  91. // gs.drawImage(logoImg, x, y, logoImg.getWidth(), logoImg.getHeight(), null);
  92. // logoImg.flush();
  93. // }
  94. // //2.二维码图片底部插入文字
  95. // if(null != showBottomText && !"".equals(showBottomText)){
  96. // //2.1 设置字体样式
  97. // Font font = new Font("微软雅黑", Font.PLAIN, 14);
  98. // gs.setColor(Color.BLACK);
  99. // gs.setFont(font);
  100. // //2.2 字体显示位置
  101. // int x = (image.getWidth() - getWatermarkLength(showBottomText, gs))/2;
  102. // int y = image.getHeight()-2;
  103. // gs.drawString(showBottomText, x, y);
  104. // }
  105. // gs.dispose();
  106. // ImageIO.write(image, format, outStream);
  107. // }
  108. //
  109. // /**
  110. // * 将LOGO图片放在二维码中间(水印效果)
  111. // * 将生成的图片生成到本地硬盘路径下
  112. // * @param matrix BitMatrix
  113. // * @param format 图片格式
  114. // * @param imagePath 图片存放路径
  115. // * @param logoPath LOGO地址
  116. // * @param showBottomText 二维码图片底部需要显示的问题
  117. // * @throws IOException
  118. // */
  119. // public static void megerToFile2(BitMatrix matrix,String format,String imagePath,String logoPath,String showBottomText) throws IOException {
  120. // BufferedImage image = toBufferedImage(matrix);
  121. // Graphics2D gs = image.createGraphics();
  122. //
  123. // //1.加入LOGO水印效果
  124. // if(null != logoPath && !"".equals(logoPath)){
  125. // BufferedImage logoImg = ImageIO.read(new File(logoPath));
  126. // int width = image.getWidth() / LogoPart;
  127. // int height = image.getHeight() / LogoPart;
  128. // int x = (image.getWidth() - width) / 2;
  129. // int y = (image.getHeight() - height) / 2;
  130. // gs.drawImage(logoImg, x, y, logoImg.getWidth(), logoImg.getHeight(), null);
  131. // logoImg.flush();
  132. // }
  133. //
  134. // //2.二维码图片底部插入文字
  135. // if(null != showBottomText && !"".equals(showBottomText)){
  136. // //2.1 设置字体样式
  137. // Font font = new Font("微软雅黑", Font.PLAIN, 14);
  138. // gs.setColor(Color.BLACK);
  139. // gs.setFont(font);
  140. // //2.2 字体显示位置
  141. // int x = (image.getWidth() - getWatermarkLength(showBottomText, gs))/2;
  142. // int y = image.getHeight()-2;
  143. // gs.drawString(showBottomText, x, y);
  144. // }
  145. // gs.dispose();
  146. // ImageIO.write(image, format, new File(imagePath));
  147. // }
  148. //
  149. // /**
  150. // * 获取水印字体的长度
  151. // * @param fontString
  152. // * @param gs
  153. // * @return
  154. // */
  155. // public static int getWatermarkLength(String fontString,Graphics2D gs){
  156. // return gs.getFontMetrics(gs.getFont()).charsWidth(fontString.toCharArray(),0,fontString.length());
  157. // }
  158. //
  159. // public static BufferedImage toBufferedImage(BitMatrix matrix){
  160. // int width = matrix.getWidth();
  161. // int height = matrix.getHeight();
  162. // BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  163. //
  164. // for(int x=0;x<width;x++){
  165. // for(int y=0;y<height;y++){
  166. // image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
  167. // }
  168. // }
  169. // return image;
  170. // }
  171. //
  172. //
  173. // /**
  174. // * 打印保存的图片/二维码
  175. // * @param file
  176. // * @return
  177. // */
  178. // public static void getPrint(File file ){
  179. // // 构建打印请求属性集
  180. // DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
  181. // // 设置打印格式
  182. // PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
  183. // // 查找所有的打印服务
  184. // PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);
  185. // //循环打印机名称
  186. // for (int i = 0; i < printServices.length; i++) {
  187. // String name = printServices[i].getName();
  188. // System.out.println("打印机名称:"+name);
  189. // }
  190. // // 测试,直接用第一个了
  191. // PrintService printService = printServices[0];
  192. // String printName = printService.getName();
  193. // File picFile = new File(file.toURI());
  194. // if (!picFile.exists() || !picFile.isFile()) {
  195. // return;
  196. // }
  197. // String fileName = picFile.getName();
  198. // // 获取图片后缀名,设置打印文件格式
  199. // String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
  200. // switch (suffix) {
  201. // case "jpg":
  202. // case "jpeg":
  203. // flavor = DocFlavor.INPUT_STREAM.JPEG;
  204. // break;
  205. // case "png":
  206. // flavor = DocFlavor.INPUT_STREAM.PNG;
  207. // break;
  208. // case "pdf":
  209. // flavor = DocFlavor.INPUT_STREAM.PDF;
  210. // break;
  211. // case "gif":
  212. // flavor = DocFlavor.INPUT_STREAM.GIF;
  213. // default:
  214. // break;
  215. // }
  216. // // 设置打印文件名
  217. // pras.add(new JobName(fileName, Locale.CHINA));
  218. // // 设置打印方向
  219. // pras.add(OrientationRequested.LANDSCAPE);
  220. // // 设置打印区域
  221. // MediaPrintableArea mp = new MediaPrintableArea(0, 0, 100, 100, Size2DSyntax.MM);
  222. // pras.add(mp);
  223. // try {
  224. // FileInputStream fis = new FileInputStream(picFile);
  225. // HashDocAttributeSet das = new HashDocAttributeSet();
  226. // // 使用打印服务生成打印任务
  227. // DocPrintJob job = printService.createPrintJob();
  228. // // 获取需要打印的文档类doc
  229. // SimpleDoc doc = new SimpleDoc(fis, flavor, das);
  230. // job.print(doc, pras);
  231. // } catch (PrintException e) {
  232. // e.printStackTrace();
  233. // } catch (FileNotFoundException e) {
  234. // e.printStackTrace();
  235. // }
  236. //
  237. // }
  238. //
  239. //}