样式:
1 package com.gdzy.ZKZHFWPT.util; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics2D; 6 import java.awt.image.BufferedImage; 7 import java.io.File; 8 import java.io.IOException; 9 import java.io.OutputStream; 10 import java.util.Hashtable; 11 12 import javax.imageio.ImageIO; 13 14 import com.google.zxing.EncodeHintType; 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 import com.google.zxing.BarcodeFormat; 19 import com.google.zxing.MultiFormatWriter; 20 import com.google.zxing.WriterException; 21 import com.google.zxing.common.BitMatrix; 22 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 23 24 public class QRCodeUtils{ 25 26 private static final Log logger = LogFactory.getLog(QRCodeUtils.class); 27 28 private static final int BLACK = 0xFF000000; 29 private static final int WHITE = 0xFFFFFFFF; 30 private static final int LogoPart = 4; 31 private static int margin = 4; //白边大小,取值范围0~4 32 /** 33 * 生成二维码前的配置信息 34 * @param content 生成二维码图片内容 35 * @param width 二维码图片的宽度 36 * @param height 二维码图片的高度 37 * @return 38 */ 39 public static BitMatrix setBitMatrix(String content,int width,int height){ 40 Hashtablehints = new Hashtable (); 41 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 42 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //指定纠错等级 43 hints.put(EncodeHintType.MARGIN, 4); 44 BitMatrix bitMatrix=null; 45 try { 46 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); 47 } catch (WriterException e) { 48 logger.error("生成二维码错误",e); 49 } 50 return bitMatrix; 51 } 52 53 /** 54 * 将LOGO图片放在二维码中间(水印效果) 55 * 将生成的图片以流的形式输出到页面展示 56 * @param matrix BitMatrix 57 * @param format 图片格式 58 * @param outStream 输出流 59 * @param logoPath LOGO地址 60 * @param showBottomText 二维码图片底部需要显示的问题 61 * @throws IOException 62 */ 63 public static void megerToFile(BitMatrix matrix,String format,OutputStream outStream,String logoPath,String showBottomText) throws IOException { 64 BufferedImage codeImage = toBufferedImage(matrix); 65 Graphics2D gs = codeImage.createGraphics(); 66 //1.加入LOGO水印效果 67 if(null != logoPath && !"".equals(logoPath)){ 68 //1.1 载入LOGO图片 69 BufferedImage logoImg = ImageIO.read(new File(logoPath)); 70 //1.2 考虑到LOGO图片贴到二维码中,建议大小不要超过二维码的1/5; 71 int width = codeImage.getWidth() / LogoPart; 72 int height = codeImage.getHeight() / LogoPart; 73 //1.3 LOGO居中显示 74 int x = (codeImage.getWidth() - width) / 2; 75 int y = (codeImage.getHeight() - height) / 2; 76 gs.drawImage(logoImg, x, y, logoImg.getWidth(), logoImg.getHeight(), null); 77 logoImg.flush(); 78 } 79 //画底部画布放置二维码及底部文字 80 int backAmageW = matrix.getWidth(); 81 int backAmageH = matrix.getHeight()+50; 82 BufferedImage backAmage = new BufferedImage(backAmageW, backAmageH, BufferedImage.TYPE_3BYTE_BGR); 83 for(int x=0;x
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %><%@ include file="/jsp/common/commonfile.jsp" %>外部项目二维码显示
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %><%@ include file="/jsp/common/commonfile.jsp" %>外部项目二维码打印 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %><%@ include file="/jsp/common/commonfile.jsp" %>外部项目二维码打印
1 public void printQrCode() throws IOException { 2 HttpServletResponse response = ServletActionContext.getResponse(); 3 OutputStream outputStream = null; 4 try { 5 outputStream = response.getOutputStream(); 6 String content = "人生若只如初见,何事秋风悲画扇"; 7 String logoPath = "E:\\1.jpg"; 8 String format = "jpg"; 9 String bottomText = "人生若只如初见,何事秋风悲画扇";10 BitMatrix bitMatrix = QRCodeUtils.setBitMatrix(content, 250, 250);11 QRCodeUtils.megerToFile(bitMatrix, format, outputStream, logoPath, bottomText);12 } catch (IOException e) {13 e.printStackTrace();14 } finally {15 if (outputStream != null) {16 try {17 outputStream.flush();18 outputStream.close();19 } catch (IOException e) {20 e.printStackTrace();21 }22 }23 }24 }
打印通过<body>的 οnlοad="this.focus();this.print(); 调取打印机进行打印