JAVA-EE生成图片填充二维码以及特殊形状的二维码以及带有背景图的二维码和彩色二维码------计算机网络经典

package com.powernode.qrcode.servlet;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
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.*;
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();
//        生成彩色二维码
//            BufferedImage image = QrCodeGenWrapper.of(url)
//                    .setDrawPreColor(Color.BLUE)//设置二维码颜色
//                            .asBufferedImage();
//            生成带有背景图的二维码
//            BufferedImage image = QrCodeGenWrapper.of(url)
//                    .setBgImg(request.getPart("logo").getInputStream())
//                    .setBgOpacity(0.7F)//设置透明度
//                    .asBufferedImage();
//            生成特殊形状的二维码
//            BufferedImage image = QrCodeGenWrapper.of(url)
//                    .setDrawEnableScale(true)//启用二维码绘制时的缩小放大功能
//                    .setDrawStyle(QrCodeOptions.DrawStyle.DIAMOND)//指定绘制样式采用钻石
//                    .asBufferedImage();
//            生成图片填充二维码
            BufferedImage image = QrCodeGenWrapper.of(url)
                    .setErrorCorrection(ErrorCorrectionLevel.H)//设置二维码的错误纠正级别
                    .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)//绘制二维码时采用图片填充
                    .addImg(1,1,request.getPart("logo").getInputStream())//添加图片
                    .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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
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.*;
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();
//        生成彩色二维码
//            BufferedImage image = QrCodeGenWrapper.of(url)
//                    .setDrawPreColor(Color.BLUE)//设置二维码颜色
//                            .asBufferedImage();
//            生成带有背景图的二维码
//            BufferedImage image = QrCodeGenWrapper.of(url)
//                    .setBgImg(request.getPart("logo").getInputStream())
//                    .setBgOpacity(0.7F)//设置透明度
//                    .asBufferedImage();
//            生成特殊形状的二维码
//            BufferedImage image = QrCodeGenWrapper.of(url)
//                    .setDrawEnableScale(true)//启用二维码绘制时的缩小放大功能
//                    .setDrawStyle(QrCodeOptions.DrawStyle.DIAMOND)//指定绘制样式采用钻石
//                    .asBufferedImage();
//            生成图片填充二维码
            BufferedImage image = QrCodeGenWrapper.of(url)
                    .setErrorCorrection(ErrorCorrectionLevel.H)//设置二维码的错误纠正级别
                    .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)//绘制二维码时采用图片填充
                    .addImg(1,1,request.getPart("logo").getInputStream())//添加图片
                    .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)