博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二维码以流的形式输出到界面,并打印不保存到本地
阅读量:5946 次
发布时间:2019-06-19

本文共 5230 字,大约阅读时间需要 17 分钟。

样式:

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         Hashtable
hints = 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     }
controller

 打印通过<body>的 οnlοad="this.focus();this.print(); 调取打印机进行打印

转载于:https://www.cnblogs.com/xueyicanfei/p/10298253.html

你可能感兴趣的文章
实作 ASP.NET 多笔数据离线编辑(转)
查看>>
solr的用分布式搜索(转)
查看>>
多媒体开发之rtmp---rtmp client 编译
查看>>
异常处理汇总 ~ 修正果带着你的Code飞奔吧!
查看>>
Java开发手冊 Java学习手冊教程(MtJava开发手冊)
查看>>
The Willpower Instinct
查看>>
注入复习总结
查看>>
OpenStack 部署总结之:单节点icehouse网桥的配置
查看>>
js---12数据类型,数据类型转换,NaN,
查看>>
C字符串处理函数
查看>>
超短reads(primer、barcode、UMI、index等)比对方法
查看>>
lua正则表达式如何匹配中文
查看>>
arcgis api for js热力图优化篇-不依赖地图服务
查看>>
php逻辑操作符中&和&&的异同
查看>>
Git 远程仓库(分布式版本控制系统)
查看>>
设计模式原则之里氏替换原则
查看>>
LeetCode: Longest Common Prefix 解题报告
查看>>
Multipart polyline to single part lines
查看>>
zeromq_传说中最快的消息队列
查看>>
ARM的栈指令
查看>>