JAVA生成跳转指定页面并且附带参数的二维码

简介: JAVA生成跳转指定页面并且附带参数的二维码

所需jar包:

 <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>
/**
 *
 * 实现说明:把服务器上的产品详情页的链接地址(含有ID)生成二维码,微信扫码,跳转到指定页
 * 需要jar:zxing-code-3.1.0.jar
 *
 */
public class GenerateCode {
 
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
 
    public static void main(String[] args) {
        try {
            boolean flag = generateCode("518");
            if (flag) {
                System.out.println("成功生成二维码");
            }
        } catch (WriterException | IOException e) {
            System.err.println("生成二维码失败");
            e.printStackTrace();
        }
    }
 
    public static boolean generateCode(String productId) throws WriterException, IOException {
        // 这里是URL,扫描之后就跳转到这个界面
        String text = "https://www.baidu.com/?uudi=" + productId;
 
        String path = "E:/"; // 图片生成的位置
        int width = 400;
        int height = 400;
        // 二维码图片格式
        String format = "jpg";
        // 设置编码,防止中文乱码
        Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object>();
        ht.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 设置二维码参数(编码内容,编码类型,图片宽度,图片高度,格式)
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, ht);
        // 生成二维码(定义二维码输出服务器路径)
        File outputFile = new File(path);
        if (!outputFile.exists()) {
            // 创建文件夹
            outputFile.mkdir();
        }
 
        int b_width = bitMatrix.getWidth();
        int b_height = bitMatrix.getHeight();
        // 建立图像缓冲器
        BufferedImage image = new BufferedImage(b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
        for (int x = 0; x < b_width; x++) {
            for (int y = 0; y < b_height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }
        // 生成二维码
        ImageIO.write(image, format, new File(path + "/code." + format));
        // 二维码的名称
        // code.jpg
 
        return true;
    }
}

通过接口下载:

@Controller
public class GenerateCode {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
 
 
    @GetMapping("/download/{code}")
    public void downloadCode(@PathVariable String code, HttpServletResponse response) throws WriterException, IOException {
        // 这里是URL,扫描之后就跳转到这个界面
        String text = "https://www.baidu.com/?intited=" + code;
 
        int width = 400;
        int height = 400;
        // 二维码图片格式
        String format = "jpg";
 
 
        // 设置编码,防止中文乱码
        Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object>();
        ht.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 设置二维码参数(编码内容,编码类型,图片宽度,图片高度,格式)
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, ht);
 
        int b_width = bitMatrix.getWidth();
        int b_height = bitMatrix.getHeight();
        // 建立图像缓冲器
        BufferedImage image = new BufferedImage(b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
        for (int x = 0; x < b_width; x++) {
            for (int y = 0; y < b_height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }
        String fileName= new StringBuffer().append(code).append(".").append(format).toString();
        response.setHeader("Content-Type","application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
 
        response.setContentType("application/octet-stream; charset=utf-8");
        // 生成二维码
        ServletOutputStream fileOutputStream = response.getOutputStream();
        ImageIO.write(image, format, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }
 
 
}

或者这样也行

@Controller
public class GenerateCode {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
 
    @GetMapping("/download/{code}")
    public void downloadCode(@PathVariable String code, HttpServletRequest request, HttpServletResponse response) throws WriterException, IOException {
        // 这里是URL,扫描之后就跳转到这个界面
        String text = "https://www.baidu.com/?intited=" + code;
 
        int width = 400;
        int height = 400;
        // 二维码图片格式
        String format = "jpg";
 
 
        // 设置编码,防止中文乱码
        Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object>();
        ht.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 设置二维码参数(编码内容,编码类型,图片宽度,图片高度,格式)
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, ht);
 
        int b_width = bitMatrix.getWidth();
        int b_height = bitMatrix.getHeight();
        // 建立图像缓冲器
        BufferedImage image = new BufferedImage(b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
        for (int x = 0; x < b_width; x++) {
            for (int y = 0; y < b_height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }
        String fileName= new StringBuffer().append(code).append(".").append(format).toString();
        // 设置下载文件的mineType,告诉浏览器下载文件类型
        String mineType = request.getServletContext().getMimeType(fileName);
        System.out.println(mineType);
        response.setContentType(mineType);
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
 
 
        // 生成二维码
        ServletOutputStream fileOutputStream = response.getOutputStream();
        ImageIO.write(image, format, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }
 
 
}

参考:https://my.oschina.net/ydsakyclguozi/blog/887368

目录
相关文章
|
27天前
|
Java API 编译器
Java编译器注解运行和自动生成代码问题之编译时通过参数设置选项值问题如何解决
Java编译器注解运行和自动生成代码问题之编译时通过参数设置选项值问题如何解决
|
1月前
|
缓存 安全 算法
Java面试题:如何通过JVM参数调整GC行为以优化应用性能?如何使用synchronized和volatile关键字解决并发问题?如何使用ConcurrentHashMap实现线程安全的缓存?
Java面试题:如何通过JVM参数调整GC行为以优化应用性能?如何使用synchronized和volatile关键字解决并发问题?如何使用ConcurrentHashMap实现线程安全的缓存?
17 0
|
4天前
|
开发框架 前端开发 Java
【前端学java】SpringBootWeb极速入门-实现一个简单的web页面01
【8月更文挑战第12天】SpringBootWeb极速入门-实现一个简单的web页面01
18 3
【前端学java】SpringBootWeb极速入门-实现一个简单的web页面01
|
10天前
|
消息中间件 Java 大数据
"深入理解Kafka单线程Consumer:核心参数配置、Java实现与实战指南"
【8月更文挑战第10天】在大数据领域,Apache Kafka以高吞吐和可扩展性成为主流数据流处理平台。Kafka的单线程Consumer因其实现简单且易于管理而在多种场景中受到欢迎。本文解析单线程Consumer的工作机制,强调其在错误处理和状态管理方面的优势,并通过详细参数说明及示例代码展示如何有效地使用KafkaConsumer类。了解这些内容将帮助开发者优化实时数据处理系统的性能与可靠性。
38 7
|
23天前
|
存储 Java
java 服务 JVM 参数设置配置
java 服务 JVM 参数设置配置
23 3
|
4天前
|
Java 编译器 数据库连接
Java中的无参数构造方法
Java中的无参数构造方法
|
5天前
|
JavaScript Java
分别使用java script和jQuery添加页面元素
分别使用java script和jQuery添加页面元素
6 0
|
27天前
|
Java 测试技术 Maven
Java编译器注解运行和自动生成代码问题之在编译时需要设置-proc:none参数问题如何解决
Java编译器注解运行和自动生成代码问题之在编译时需要设置-proc:none参数问题如何解决
|
8天前
|
Java
JAVA 获取 URL 指定参数的值
JAVA 获取 URL 指定参数的值
16 0
|
1月前
|
测试技术 API Android开发
《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)
【7月更文挑战第15天】这是关于自动化测试框架中Selenium API二次封装的教程总结。教程中介绍了如何设计一个支持不同浏览器测试的页面基类(BasePage),该基类包含了对Selenium方法的二次封装,如元素的输入、点击、清除等常用操作,以减少重复代码。此外,页面基类还提供了获取页面标题和URL的方法。
44 2