java url生成二维码

好了直接上代码

maven依赖配置


    <!-- https://mvnrepository.com/artifact/net.glxn.qrgen/javase -->  
    <dependency>  
        <groupId>net.glxn.qrgen</groupId>  
        <artifactId>javase</artifactId>  
        <version>2.0</version>  
    </dependency>
package com.example.datafile.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

//import com.sun.javafx.iio.ImageStorage;

/**
 * @author xsh
 * @introduce
 * @DATE 2021/9/20 09:43
 */
@RestController
@RequestMapping("/getQrCode")
@Api(tags = "获取二维码")
public class GetCloudAutQRCode {

    @ApiOperation("表单列表查询")
    @GetMapping("/getCloudAutQRCode.action")
    public void getCloudAutQRCode(HttpServletResponse response, HttpServletRequest request, HttpSession session) throws IOException {
        //String url = "https://www.baidu.com/";
        String url = request.getParameter("url");
        System.out.println(url);
        ByteArrayOutputStream out = QRCode.from(url).to(ImageType.PNG).withSize(200,200).stream();

        response.setContentType("image/png");
        response.setContentLength(out.size());

        OutputStream outStream = response.getOutputStream();

        outStream.write(out.toByteArray());

        outStream.flush();
        outStream.close();
    }

}

项目启动访问:
http://localhost:8443/getQrCode/getCloudAutQRCode.action?url=https://www.baidu.com/

效果如下:
java url生成二维码_第1张图片

你可能感兴趣的:(后端,二维码)