java生成二维码扫描跳转到指定的路径URL

导入依赖

 <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.1.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.1.0</version></dependency>

二维码生成工具类

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
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.OutputStream;
import java.util.Hashtable;
import java.util.Random;/*** Created by IntelliJ IDEA* Time: 2020/12/10 11:35,* Email: 1243052362@qq.com** @author yangdong*/
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<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();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;}/*** 插入LOGO** @param source*            二维码图片* @param imgPath*            LOGO图片地址* @param needCompress*            是否压缩* @throws Exception*/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();}/*** 生成二维码(内嵌LOGO)** @param content*            内容* @param imgPath*            LOGO地址* @param destPath*            存放目录* @param needCompress*            是否压缩LOGO* @throws Exception*/public static void encode(String content, String imgPath, String destPath,boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);mkdirs(destPath);String file = new Random().nextInt(99999999)+".jpg";ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));}/*** 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)* @author lanyuan* Email: mmm333zzz520@163.com* @date 2013-12-11 上午10:16:36* @param destPath 存放目录*/public static void mkdirs(String destPath) {File file =new File(destPath);//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)if (!file.exists() && !file.isDirectory()) {file.mkdirs();}}/*** 生成二维码(内嵌LOGO)** @param content*            内容* @param imgPath*            LOGO地址* @param destPath*            存储地址* @throws Exception*/public static void encode(String content, String imgPath, String destPath)throws Exception {QRCodeUtil.encode(content, imgPath, destPath, false);}/*** 生成二维码** @param content*            内容* @param destPath*            存储地址* @param needCompress*            是否压缩LOGO* @throws Exception*/public static void encode(String content, String destPath,boolean needCompress) throws Exception {QRCodeUtil.encode(content, null, destPath, needCompress);}/*** 生成二维码** @param content*            内容* @param destPath*            存储地址* @throws Exception*/public static void encode(String content, String destPath) throws Exception {QRCodeUtil.encode(content, null, destPath, false);}/*** 生成二维码(内嵌LOGO)** @param content*            内容* @param imgPath*            LOGO地址* @param output*            输出流* @param needCompress*            是否压缩LOGO* @throws Exception*/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);}/*** 生成二维码** @param content*            内容* @param output*            输出流* @throws Exception*/public static void encode(String content, OutputStream output)throws Exception {QRCodeUtil.encode(content, null, output, false);}/*** 解析二维码** @param file*            二维码图片* @return* @throws Exception*/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<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result = new MultiFormatReader().decode(bitmap, hints);String resultStr = result.getText();return resultStr;}/*** 解析二维码** @param path*            二维码图片地址* @return* @throws Exception*/public static String decode(String path) throws Exception {return QRCodeUtil.decode(new File(path));}public static void main(String[] args) throws Exception {String text = "https://www.baidu.com";  //这里设置自定义网站urlString logoPath = "E:\\work\\chat\\chat.jpg"; //插入的logo图片logoPath = null;String destPath = "E:\\work";QRCodeUtil.encode(text, logoPath, destPath, true);}}

java生成二维码扫描跳转到指定的路径URL相关推荐

  1. JAVA生成二维码链接

    本人对一些没用过的技术一般都比较好奇,项目上用到了二维码扫描跳转链接,项目后自己百度搜索研究,总结了一下,生成二维码后跳转链接的一些知识. 1.需要用到的maven依赖 <!-- https:/ ...

  2. JAVA生成二维码(二)深度处理

    1.写这个博客的目的 解决JAVA生成二维码(一)中的一些问题. 2.解读排错率,编码模式,版本问题.以下都是自己测试中存在的问题 1.排错率 排错的原理是二维码在编码过程中进行了冗余,就像是123被 ...

  3. JAVA生成二维码扫码进入h5微信支付宝支付

    第一步准备 (1)微信需要的公众服务号和商户号:沙箱有很多问题,所以本人以正式的配置实现,其中公众号需要配置授权路径 其中公众号需配置 商户号需到产品中心 -> 开发配置 -> 支付配置 ...

  4. java 生成二维码 QRCode、zxing 两种方式

    版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢. https://blog.csdn.net/testcs_dn/article/details/ ...

  5. 二维码相关---java生成二维码名片,并且自动保存到手机通讯录中...

    二维码相关---java生成二维码名片,并且自动保存到手机通讯录中... 技术qq交流群:JavaDream:251572072 1.首先介绍一个api.   Zxing是Google提供的关于条码 ...

  6. SpringBoot生成二维码 扫描并可下载文件

    生成二维码 扫描并可下载文件 pom.xml 依赖 application.yaml Controller ImageBuilderUtils 工具类 适当根据自己的业务需求变通,然后就能轻松使用了, ...

  7. java生成二维码,并在前端展示。

    java生成二维码,并在前端展示,扫码实现下载功能. 后端生成二维码以流的形式 前端接收二维码并展示 后端生成二维码以流的形式 这是以流的形式展示二维码.当然也可以以文件的格式,文件格式就是Path ...

  8. java生成二维码(链接生成二维码)

    Java二维码如何生成? awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import com. ...

  9. Java 生成二维码。

    Java 生成二维码. 文章目录 Java 生成二维码. 二维码. 发展. 一维码. 二维码. 优缺点. QR Code. 生成方法. 第三方 jar.eg. zxing 和 qrcodejar. J ...

最新文章

  1. 独家 | CycleGAN之美 赛马翻译成斑马背后的直觉和数学(附论文)
  2. 1、SELECT:数据表查询语句
  3. 释疑の语法LOCAL
  4. vue项目使用eslint
  5. js 中转换成list集合_程序员:java集合介绍-List,具说很详细,你不来看看?
  6. matlab imread_MATLAB图像处理:29:在几何变换输出中指定填充值
  7. 【高等数学】一元函数积分表
  8. python开发信息系统权限设置_python Django 用户管理和权限认证
  9. 戴尔企业级技术社区达人积分等级制度
  10. 九、全面提高人民生话水平
  11. 数据结构——树的概述
  12. Java程序员在写SQL程序时候常犯的10个错误
  13. 时速云Kubernetes进阶培训 第三期
  14. 遗传算法matlab_【优化求解】遗传算法解决背包问题
  15. LayUI动态添加table表格
  16. 腾讯云轻量服务器和虚拟主机VPS有什么区别?
  17. linux下格式化SD卡
  18. 4月6日----4月10日二年级课程表
  19. SQL*Loader使用方法nbsp;(转自一沙弥的…
  20. phpBB 2.06漏洞

热门文章

  1. 如何选择第三方电子合同服务平台?
  2. 重装Win10系统有教程吗?如何在线一键重装Win10系统?
  3. 代理IP是如何使用的
  4. PAT_甲级 简单模拟
  5. mysql eav_数据库设计之EAV(实体、属性、值)
  6. 办公技能01:最简单的调整图片分辨率方法——用windows自带的画图功能
  7. 【2】Kali破解家用WI-FI密码 - WPA/WPA2加密
  8. mysql 内部 临时表_MySQL内部临时表策略 - Mr.南柯 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  9. hadoop与spark环境搭建命令简易教程(Ubuntu18.04)
  10. C语言程序设计博客作业02