JAVA-EE使用GitHub上封装好的ZXing实现带LOGO的二维码生成------计算机网络经典

发布时间:2024年01月03日
package com.powernode.qrcode.servlet;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@WebServlet("/generateWithQrCodeGit")
//fileSizeThreshold文件大小达到后自动上传
//maxFileSize最大文件大小
//maxRequestSize最大请求大小
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10,maxFileSize = 1024 * 1024 * 10,maxRequestSize = 1024 * 1024 * 10)
public class generateWithQrCode extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
//        获取文本内容
            String url = request.getParameter("url");
//        生成二维码
//            BufferedImage image = QrCodeGenWrapper.of(url).asBufferedImage();
            BufferedImage image = QrCodeGenWrapper.of(url).
                    setLogo(request.getPart("logo").getInputStream()).
                    setLogoRate(7).//设置图片与二维码的比例,这里设置的是LOGO是二维码的七分之一
                    setLogoStyle(QrCodeOptions.LogoStyle.ROUND).//设置LOGO图片的样式,将LOGO的边框设置为圆形
                    asBufferedImage();
//        响应内容到浏览器
            ImageIO.write(image,"PNG",response.getOutputStream());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
package com.powernode.qrcode.servlet;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@WebServlet("/generateWithQrCodeGit")
//fileSizeThreshold文件大小达到后自动上传
//maxFileSize最大文件大小
//maxRequestSize最大请求大小
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10,maxFileSize = 1024 * 1024 * 10,maxRequestSize = 1024 * 1024 * 10)
public class generateWithQrCode extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
//        获取文本内容
            String url = request.getParameter("url");
//        生成二维码
//            BufferedImage image = QrCodeGenWrapper.of(url).asBufferedImage();
            BufferedImage image = QrCodeGenWrapper.of(url).
                    setLogo(request.getPart("logo").getInputStream()).
                    setLogoRate(7).//设置图片与二维码的比例,这里设置的是LOGO是二维码的七分之一
                    setLogoStyle(QrCodeOptions.LogoStyle.ROUND).//设置LOGO图片的样式,将LOGO的边框设置为圆形
                    asBufferedImage();
//        响应内容到浏览器
            ImageIO.write(image,"PNG",response.getOutputStream());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/generateWithQrCodeGit" method="post" enctype="multipart/form-data">
    请输入文本内容:<input type="text" name="url"/><br>
    请选择图片:<input type="file" name="logo"/><br>
    <hr>
    <input type="submit" value="生成二维码"/>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/generateWithQrCodeGit" method="post" enctype="multipart/form-data">
    请输入文本内容:<input type="text" name="url"/><br>
    请选择图片:<input type="file" name="logo"/><br>
    <hr>
    <input type="submit" value="生成二维码"/>
</form>
</body>
</html>
文章来源:https://blog.csdn.net/2201_75960169/article/details/135343122
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。