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

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" %>


    Title


请输入文本内容:
请选择图片:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


请输入文本内容:
请选择图片:

你可能感兴趣的:(JAVA,计算机网络,java-ee,java,后端,tomcat,servlet,jsp,mvc)