二维码生成及扫描,查看附件
前面先附上代码,具体的类方法放在最后//二维码生成地址String path = "C:/qrCode/"+sdf.format(new Date())+"/"+id+".jpg";// 生成二维码String content = "http://221.6.30.xxx:xxxx/xxxx/xx/xxxx.do?rowId="+id;QrCodeCreateUtil.encode(content, path);
展示二维码前台jsp:
<table class="table table-condensed table-hover"><tbody><tr><td align="center"><img src="<%=basePath%>xx/QR-code-show.do?rowId=${xxxxxxx.rowId}"></td></tr></tbody> </table>
后台二维码显示并下载
@RequestMapping(method = RequestMethod.GET, value = "QR-code-show") public void xxxxx(String rowId, HttpServletRequest request, HttpServletResponse response) throws IOException {if(rowId != null && rowId.length() > 0){//我是通过rowId获取的二维码路径String qrCode = equipmentInfo.getQrCode();//附件String fileName = equipmentInfo.getManagerNum()+".jpg";FileUtil fileHelper = new FileUtil();//下载fileHelper.downloadFile(qrCode, request, response, fileName);} }
/*** 下载文件* * @param downLoadPath* 下载路径* @param response* @param fileName* 文件名*/ public void downloadFile(String downLoadPath, HttpServletRequest request,HttpServletResponse response, String fileName) {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {long fileLength = new File(downLoadPath).length();/** response.setContentType("application/x-msdownload;");* response.setHeader("Content-disposition", "attachment; filename="* + fileName);*/String userAgent = request.getHeader("User-Agent");// 针对IE或者以IE为内核的浏览器:if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {fileName = URLEncoder.encode(fileName, "UTF-8");} else {// 非IE浏览器的处理:fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");}response.setHeader("Content-disposition",String.format("attachment; filename=\"%s\"", fileName));response.setContentType("application/octet-stream;charset=utf-8");response.setCharacterEncoding("UTF-8");response.setHeader("Content-Length", String.valueOf(fileLength));bis = new BufferedInputStream(new FileInputStream(downLoadPath));bos = new BufferedOutputStream(response.getOutputStream());byte[] buff = new byte[2048];int bytesRead;while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {bos.write(buff, 0, bytesRead);}} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) {try {bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (bos != null) {try {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
根据生成二维码 存放的content 扫描发送请求 "http://221.6.30.xxx:xxxx/xxxx/xx/xxxx.do?rowId="+id;
/*** 查看设备信息(二维码扫描)* @param rowId* @param model* @return*/ @RequestMapping("get-sb-info") public String getSbInfo(@RequestParam(value = "rowId") String rowId, Model model) {String returnPage = "xx/xxxx";if (rowId != null) {EquipmentInfo equipmentInfo = equipmentInfoManager.get(rowId);model.addAttribute("equipmentInfo", equipmentInfo);List<FileRecord> fileList = fileRecordManager.findBy("relationId", rowId);model.addAttribute("fileList", fileList);}else{returnPage = "";}return returnPage; }
跳转jsp后显示内容
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ include file="/common/taglibs.jsp"%> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <base href="<%=basePath %>"> <html> <body style="text-align: center;margin: 0px;padding: 0px;"> <div style="margin:0 auto;width: 100%;"><table class="bordertable" cellpadding="0" cellspacing="0"><tr><td align="right" class="titlebg">xxxx:</td><td align="left">${equipmentInfo.workStatus}</td></tr><c:forEach items="${fileList}" var="file"><tr><td colspan="2" width="100%"><img src="<%=basePath%>fileRecord/fileDownload.do?fileId=${file.rowId}" width="100%"></td></tr></c:forEach></table> </div> </body> </html>
最后显示下载文件
/*** 附件下载* * @param fileId* 文件ID* @return 下载* @throws IOException*/ @RequestMapping(method = RequestMethod.GET, value = "fileDownload") public void fileDownLoad(String fileId, HttpServletRequest request,HttpServletResponse response) throws IOException {FileRecord fileRecord = fileRecordManager.get(fileId);String filePath = fileRecord.getFilePath();String fileName = fileRecord.getFileName();FileUtil fileHelper = new FileUtil();fileHelper.downloadFile(filePath, request, response, fileName); }
最后贴上二维码类 zxing-code-2.3.jar
package com.gx.soft.common.util;import com.google.zxing.*; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.OutputStream;import java.util.Hashtable;public class QrCodeCreateUtil {private static final String CHARSET = "utf-8";private static final String FORMAT_NAME = "JPG";// 二维码尺寸private static final int QRCODE_SIZE = 300;// LOGO宽度private static final int WIDTH = 60;// LOGO高度private static final int HEIGHT = 60;// /** // * 生成包含字符串信息的二维码图片 // * @param outputStream 输出流 // * @param content 字符串信息 // * @param qrCodeSize 二维码图片大小 // * @param imageFormat 二维码的格式 // * @return // * @throws WriterException // * @throws IOException // */ // public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{ // //设置二维码纠错级别Map // Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); // hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别 // QRCodeWriter qrCodeWriter = new QRCodeWriter(); // //创建比特矩阵(位矩阵)的QR码编码的字符串 // BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap); // // 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点) // int matrixWidth = byteMatrix.getWidth(); // BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB); // image.createGraphics(); // Graphics2D graphics = (Graphics2D) image.getGraphics(); // graphics.setColor(Color.WHITE); // graphics.fillRect(0, 0, matrixWidth, matrixWidth); // // 使用比特矩阵画并保存图像 // graphics.setColor(Color.BLACK); // for (int i = 0; i < matrixWidth; i++){ // for (int j = 0; j < matrixWidth; j++){ // if (byteMatrix.get(i, j)){ // graphics.fillRect(i-100, j-100, 1, 1); // } // } // } // return ImageIO.write(image, imageFormat, outputStream); // }private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {Hashtable hints = new Hashtable();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.CHARACTER_SET, CHARSET);hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,hints);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);}}if (imgPath == null || "".equals(imgPath)) {return image;}// 插入图片QrCodeCreateUtil.insertImage(image, imgPath, needCompress);return image;}private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {File file = new File(imgPath);if (!file.exists()) {System.err.println("" + imgPath + " 该文件不存在!");return;}Image src = ImageIO.read(new File(imgPath));int width = src.getWidth(null);int height = src.getHeight(null);if (needCompress) { // 压缩LOGOif (width > WIDTH) {width = WIDTH;}if (height > HEIGHT) {height = HEIGHT;}Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(image, 0, 0, null); // 绘制缩小后的图g.dispose();src = image;}// 插入LOGOGraphics2D graph = source.createGraphics();int x = (QRCODE_SIZE - width) / 2;int y = (QRCODE_SIZE - height) / 2;graph.drawImage(src, x, y, width, height, null);Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);graph.setStroke(new BasicStroke(3f));graph.draw(shape);graph.dispose();}public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {BufferedImage image = QrCodeCreateUtil.createImage(content, imgPath, needCompress);mkdirs(destPath);// String file = new Random().nextInt(99999999)+".jpg";// ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));ImageIO.write(image, FORMAT_NAME, new File(destPath));}public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {BufferedImage image = QrCodeCreateUtil.createImage(content, imgPath, needCompress);return image;}public static void mkdirs(String destPath) {File file = new File(destPath);// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)if (!file.exists() && !file.isDirectory()) {file.mkdirs();}}public static void encode(String content, String imgPath, String destPath) throws Exception {QrCodeCreateUtil.encode(content, imgPath, destPath, false);}public static void encode(String content, String destPath) throws Exception {QrCodeCreateUtil.encode(content, null, destPath, false);}public static void encode(String content, String imgPath, OutputStream output, boolean needCompress) throws Exception {BufferedImage image = QrCodeCreateUtil.createImage(content, imgPath, needCompress);ImageIO.write(image, FORMAT_NAME, output);}public static void encode(String content, OutputStream output) throws Exception {QrCodeCreateUtil.encode(content, null, output, false);}public static String decode(File file) throws Exception {BufferedImage image;image = ImageIO.read(file);if (image == null) {return null;}BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Result result;Hashtable hints = new Hashtable();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result = new MultiFormatReader().decode(bitmap, hints);String resultStr = result.getText();return resultStr;}public static String decode(String path) throws Exception {return QrCodeCreateUtil.decode(new File(path));}}
二维码生成及扫描,查看附件相关推荐
- 微信公众平台----带参数二维码生成和扫描事件
原文:微信公众平台----带参数二维码生成和扫描事件 摘要: 账号管理----生成带参数的二维码 消息管理----接收消息----接收事件推送 为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众 ...
- 苹果原生二维码生成与扫描及生成的二维码不清楚的解决方案
苹果原生二维码生成与扫描及生成的二维码不清楚的解决方案 参考文章: (1)苹果原生二维码生成与扫描及生成的二维码不清楚的解决方案 (2)https://www.cnblogs.com/CoderEYL ...
- 二维码生成、扫描、图片识别(Zxing)
这样的例子虽然已经很多了,不过我在网上浏览了一圈,也没找到几个图库二维码图片识别例子,好的算法识别率才高.这里有一个好点的算法,算法不是我写的,只是作为整理记录,给众多安卓开发者一个方便.demo的U ...
- Android开发——Android中的二维码生成与扫描
0. 前言 今天这篇文章主要描述二维码的生成与扫描,使用目前流行的Zxing,为什么要讲二维码,因为二维码太普遍了,随便一个Android APP都会有二维码扫描.本篇旨在帮助有需求的同学快速完成二维 ...
- Android之二维码生成与扫描
转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/51260428 本文出自:[顾林海的博客] ##前言 月底离开公司,准备 ...
- 转【微信小程序 四】二维码生成/扫描二维码
原文:https://blog.csdn.net/xbw12138/article/details/75213274 前端 二维码生成 二维码要求:每分钟刷新一次,模拟了个鸡肋,添加了个按分钟显示 ...
- iOS开发二维码生成和扫描
准备工作 导入<CoreImage/CoreImage.h>,生成二维码用 导入<AVFoundation/AVFoundation.h>,读取二维码用 设置代理协议AVCap ...
- iOS - 二维码生成、扫描及页面跳转
主要内容的介绍 普通二维码生成 彩色二维码生成 带有小图标二维码生成 扫描二维码的自定义 是否开启闪光灯 从相册中获取二维码 扫描成功之后提示音 扫描成功之后的界面之间的跳转 扫描二维码界面采取了微信 ...
- Android中的二维码生成与扫描
转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/51260428 本文出自:[顾林海的博客] 前言 月底离开公司,准备月底 ...
最新文章
- java实现 k nn算法_数据挖掘(二)——Knn算法的java实现
- 是什么让深度学习再次崛起并超越人类?
- 如何将切换anaconda 的Python 版本
- 将历史、数学、语文、地理、政治知识融会贯通的诀窍就是它
- ES6新特性_ES6集合实践---JavaScript_ECMAScript_ES6-ES11新特性工作笔记031
- C#语言中循环分类总结
- [tensorflow]tensorflow2.1.0使用内置方法进行培训和评估
- 植树问题python_《程序员的数学》思考题(一)
- 【CF724F】Uniformly Branched Trees
- 计算机报名照片没有重命名,电脑照片重命名怎么弄
- 【Vue实用功能】Vue实现浏览器全屏退出全屏
- “天鹅”类谜解大全!(转载)
- 基于词汇衔接的文档级扩展机器翻译评测指标
- Vue实战:简易布局Dome
- 转行了!文科生转程序员的外包工作经历分享
- 普源精电通过注册:拟募资7.5亿 高瓴与招银是股东
- 24c0x读写 大于256字节读写方式,以24c08为例(24c04/08/16同理)
- 022 利用头皮脑电信号预测癫痫发作2021
- html设定列的最小宽度,设置Grid Layout列最小宽度的方法
- IP解析:含义、作用、格式、分类
热门文章
- strcmp函数的使用
- 样本偏度(skewness)与随机变量的偏度及三阶统计量之间的关系和计算估计
- 股海沉浮:谁知道大连热电600719的庄家是谁?君子报仇,十年不晚!
- CVE-2021-41773 Apache2.4.49路径穿越漏洞复现
- C++背景实现 学生管理系统(添加、显示、删除、修改)
- Javaweb目前的未来学习方向
- PyPy-让Python爬的更快
- 关于iou,GIOU, iou-net
- dede过滤规则-如何过滤(1)这种-正则表达式
- UITableView中行的操作