java实现手机扫描二维码后网站跳转新页面

article/2025/10/23 22:06:35

java实现手机扫描二维码后网站跳转新页面,提供zxing和hutools的方式实现二维码的生成,动态刷新,验证跳转功能。

1.效果图:

二维码设置有效时间

失效重新获取二维码

手机扫描二维码成功后网站跳转新页面

 2.后端代码:

/*** @description 二维码控制器*/
@Controller
public class QrcodeController {@Autowiredprivate RedisUtils redisUtils;/*** @description 二维码页面* @return  java.lang.String**/@GetMapping({"/","/index"})public  String index(ModelMap modelMap){modelMap.put("userId", 1);return "index";}/*** @description 生成二维码* @param  request* @Param  response* @return  void**/@GetMapping("/getQrcode")@ResponseBodypublic void getQrcode(HttpServletRequest request, HttpServletResponse response) throws Exception {//二维码中的链接,需要公网网址才可以用手机扫描出来,本地测试开通natapp进行内网渗透String url = "http://qh22wg.natappfree.cc/scanQrcode";//过期时间,30slong expireTime = 30;//设置参数String random = request.getParameter("random");String userId = request.getParameter("userId");//生成二维码唯一标识String key = String.valueOf(System.currentTimeMillis());//设置二维码过期时间redisUtils.set(key,random,expireTime);//二维码中的内容String content = url + "?key=" + key + "&userId=" + userId;//二维码图片中间logoString imgPath = null;Boolean needCompress = true;//拿到图片流ByteArrayOutputStream out = QRCodeUtil.encodeIO(content, imgPath, needCompress);//返回二维码response.setCharacterEncoding("UTF-8");response.setContentType("image/jpeg;charset=UTF-8");response.setContentLength(out.size());ServletOutputStream outputStream = response.getOutputStream();outputStream.write(out.toByteArray());outputStream.flush();outputStream.close();}/*** @description 扫描二维码* @param  request* @Param  key* @Param  userId* @return  java.lang.String**/@GetMapping("/scanQrcode")@ResponseBodypublic String scanQrcode(HttpServletRequest request, String key, String userId) throws Exception {if(redisUtils.exists(key)){redisUtils.set(userId + "_qrcode_status", "success");return "扫描成功";}return "二维码失效, 请重新扫描";}/*** @description 验证扫描二维码* @param  userId**/@RequestMapping("/confirmQrcode")@ResponseBodypublic AjaxResult confirmQrcode(String userId){if(redisUtils.exists(userId + "_qrcode_status")){redisUtils.remove(userId + "_qrcode_status");//扫描成功后跳转新链接return AjaxResult.success("扫描成功", "/success");}return AjaxResult.error("二维码失效, 请重新扫描");}/*** @description 扫描二维码成功跳转页面* @param* @return  java.lang.String**/@GetMapping("/success")public  String success(ModelMap modelMap){return "success";}}

zxing生成二维码工具:

public class QRCodeUtil {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;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;}// 插入图片QRCodeUtil.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 = QRCodeUtil.createImage(content, imgPath, needCompress);mkdirs(destPath);ImageIO.write(image, FORMAT_NAME, new File(destPath));}public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.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 {QRCodeUtil.encode(content, imgPath, destPath, false);}public static byte[] getQRCodeImage(String content) throws WriterException, IOException {QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE);ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();MatrixToImageWriter.writeToStream(bitMatrix, FORMAT_NAME, pngOutputStream);byte[] pngData = pngOutputStream.toByteArray();return pngData;}public static void encode(String content, String destPath) throws Exception {QRCodeUtil.encode(content, null, destPath, false);}public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);ImageIO.write(image, FORMAT_NAME, output);}public static void encode(String content, OutputStream output) throws Exception {QRCodeUtil.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 QRCodeUtil.decode(new File(path));}//获取生成二维码的图片流public static ByteArrayOutputStream encodeIO(String content,String imgPath,Boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);//创建储存图片二进制流的输出流ByteArrayOutputStream baos = new ByteArrayOutputStream();//将二进制数据写入ByteArrayOutputStreamImageIO.write(image, "jpg", baos);return baos;}
}

3.demo下载:https://download.csdn.net/download/weixin_39220472/33632560


http://chatgpt.dhexx.cn/article/Y2eaHKwS.shtml

相关文章

【vue】移动端扫描二维码

参考链接&#xff1a; https://github.com/gruhn/vue-qrcode-readerhttps://blog.csdn.net/zhangtian_tian/article/details/105226716 1. 效果图 2. 开启 https 需要在 https 协议下才可以调用相机&#xff0c;实现扫码。 可以通过配置 vue.config.js 中的 devServer:{http…

vue实现调用摄像头扫描二维码功能

场景 在使用vue开发的h5移动端想要实现一个调用摄像头扫描二维码的功能。可能一时间想到的解决方案就是调用微信的sdk&#xff0c;但是这个微信的扫一扫只能在微信里用&#xff0c;而且还需要公众号认证等配置操作。很麻烦。可以但没必要&#xff0c;万一你的h5和公众号没有任何…

asp.net MVC使用 jsQR 扫描二维码

jsQR是一个js 插件 , 只用前台代码就可以识别出二维码的内容。 下面的例子是复制官网例子写出来的&#xff0c;官网是&#xff08;有时候github上不了&#xff0c;下载靠运气&#xff09;&#xff1a; 例子 https://cozmo.github.io/jsQR/ 代码 https://github.com/cozmo/jsQ…

html扫描二维码

引入Jquery,bootstrap 依赖 <script src"https://cdn.staticfile.org/jquery/1.11.2/jquery.min.js"></script> <script src"https://cdn.bootcss.com/twitter-bootstrap/4.4.1/js/bootstrap.min.js"></script>Html内容,通过 a标…

如何实现扫描二维码自动跳转到网页

二维码在我们的生活中随处可见&#xff0c;比如扫码付款&#xff0c;扫描进入小程序&#xff0c;扫码关注等等。二维码可以存储各种信息&#xff0c;主要包括网址、名片、文本信息、特定代码等。今天就以QR code二维码为例&#xff0c;教大家使用条码软件生成二维码&#xff0c…

如何使用电脑扫描二维码

1.打开电脑上的任何一个浏览器&#xff0c;在地址栏输入网址&#xff1a;cdn.malu.me/qrdecode/ 2.进入网站后点击网页中间的上传图标选择本地二维码进行上传识别 3.上传完成之后系统就会自动识别出来二维码的信息&#xff0c;点击下方[打开此链接]就可以实现二维码扫描

Unity之生成扫描二维码

Unity之生成扫描二维码 Unity之生成扫描二维码前言开篇Unity版本及使用插件 正题前期准备首先生成二维码然后需要扫描二维码该使用了 挂载脚本绑定按钮和输入框运行内容生成二维码扫描二维码 结尾唠家常 今日有推荐 Unity之生成扫描二维码 前言 开篇 又到了一周一分享啦&…

Web端QR二维码扫描实现

要在Web端实现基于摄像头的实时QR二维码扫描&#xff0c;需要包含摄像头控制和QR二维码解码两个部分的代码。Dynamsoft把这两部分封装在了一个JS SDK中&#xff0c;使用起来非常方便。 开发文档 https://www.dynamsoft.com/barcode-reader/programming/javascript/api-refere…

手机chrome扫描二维码_90%的用户都不知道这项Chrome隐藏功能如何开启,超级实用!...

为了增强浏览器的功能,我们通常会在Chrome上安装各种插件。 甚至一些需要第三方电脑软件才能实现的,通过插件都可以在浏览器内完成。 但是,Chrome的运行机制是把所有打开的网页标签、插件、播放的视频都拆成独立的进程。 这样的方法有利也有弊,弊端就是插件装的太多,Chrom…

VS2017 Xamarin扫描二维码并跳转网页

【本文章并非完全原创&#xff0c;是结合网上查询的各种资料来进行整合改造&#xff0c;成为可以使用的代码】 本文要实现的功能为&#xff0c;扫描一个二维码&#xff0c;读出扫描结果&#xff0c;如果结果中含有http://则会自动打开浏览器跳转页面。 &#xff08;该二维码…

Fiori 实现在网页端调用摄像头扫描二维码进行识别

我们在UI5官方文档上进行搜索Scan,是只能找到一个BarcodeScanner的&#xff0c;这个API是无法实现我们这个需求的&#xff0c;所以如果有朋友收到这种需求&#xff0c;不想做的情况下&#xff0c;是可以推脱一下&#xff0c;把问题抛给SAP的&#xff08;笑&#xff09;。既然写…

扫描二维码登录原理

手机扫码二维码实现登录某个网站的操作过程为&#xff0c;手机登录某个APP&#xff0c;利用“扫一扫”功能扫描网页上的二维码&#xff0c;扫描成功后&#xff0c;提示“登录网页版XX”&#xff0c;同时网页上显示“成功扫描 请在手机点击确认以登录”&#xff0c;手机端点击“…

【H5扫描二维码】

H5调用摄像头识别二维码-3种方法 一.使用html5-qrcode实现二维码扫描1).下载html5-qrcode2).使用 二.使用zxing/library实现二维码扫描1).下载zxing/library2).使用 三. 使用jsQR实现二维码扫描1).使用父组件直接引用mumu-getQrcode组件 vue2中使用jsQR、zxing/library、html5-…

二维码软件如何扫描二维码打开网页

在平常生活中,我们扫描二维码付款或者扫描二维码查看某品牌的网站这些都是我们经过扫描二维码跳转到了对方的网站网页页面内容,在使用中琅二维码软件制作时,我们可以先将需要跳转的网页保存在一个文档中,然后作为二维码内容添加进二维码即可。 一、制作单个跳转网页的二维…

扫描二维码后可以自动跳转到网页

现在我们的生活中随处可见二维码的身影&#xff0c;扫码付款&#xff0c;扫描进入小程序&#xff0c;扫码关注等等。二维码可以存储各种信息&#xff0c;主要包括网址、名片、文本信息、特定代码等。今天跟大家分享使用条码软件生成二维码&#xff0c;扫描后可以跳转到网址链接…

厉害了网页扫码,所有方法都给你总结到这了,赶紧收藏

最近做一个项目&#xff0c;要通过扫一扫查询对应的信息&#xff0c;由于现在已经有一部分二维码已被生成&#xff0c;为了兼顾已生成的二维码&#xff0c;所以需要使用网页的扫一扫功能去完成项目。 项目使用技术栈&#xff1a;vue2 方案一、js 原生 热心的同事帮我已经找好…

怎样测试手机的流量

在找过GT后 我们发现用路由器测试比较方便&#xff0c;可以把所有的终端都连到路由器上&#xff0c;查看各端的流量

流量过小如何做A/B测试

AB测试对于产品和运营优化的重要性有目共睹。为了能更快的得到试验结果&#xff0c;试验流量越大越好。但是当流量不够的时候怎么办呢&#xff1f;小流量AB测试能不能做&#xff1f;能&#xff01;下面有多个节约流量的方法。 一、消除异常数据的影响 例如&#xff1a;当点击…

无线专项测试--流量测试(下)

这篇文章主要是想介绍下流量专项测试的另外一种方法tcpdumpWireshark抓包测试法。 在后台系统的开发和测试中&#xff0c;借助工具抓取网络包来进行网络层的分析是一种非常常用的技术手段&#xff0c;常用的抓包工具有Windows下的Wireshark工具和Linux下的tcpdump。由于android…

android性能测试 app 实时流量获取

下面介绍几种获取app流量的统计规则&#xff1a; 分析方法D ①如何获取uid? 1.先获取进程pid (adb shell ps |findstr 包名) 2.进入到proc/pid/status 文件中 C:\Users\chenhui>adb shell PD1816:/ $ cd proc/ PD1816:/proc $ cd 20814 PD1816:/proc/20814 $ cd status /s…