栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java后端二维码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java后端二维码

在pom.xml导入zxing包
        
            com.google.zxing
            javase
            3.3.1
        
        
        
            com.google.zxing
            core
            3.3.1
        
QRCodeUtils 二维码 工具类
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.codec.binary.base64;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Component
public class QRCodeUtils {
    //日志
//    private static final Logger logger = LoggerFactory.getLogger(QRCodeUtils.class);

    //二维码颜色
    private static final int BLACK = 0xFF000000;
    //二维码颜色
    private static final int WHITE = 0xFFFFFFFF;
    //二维码宽
    private static final int width = 200;
    //二维码高
    private static final int height = 200;
    //二维码生成格式
    private static final String imageType = "png";

    
    public static String zxingCodeCreate(String text) throws Exception{
        HashMap map = new HashMap();
        //EncodeHintType枚举类 纠错程度(使得其精度增加)
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //字符编码
        map.put(EncodeHintType.CHARACTER_SET,"utf-8");
        //二维码边距
        map.put(EncodeHintType.MARGIN,1);

        //生成二维码
        BitMatrix encode = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE,width,height);
        int codeWidth = encode.getWidth();
        int codeHeight = encode.getHeight();
        BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);// BufferedImage.TYPE_INT_RGB 颜色参数
        for (int i = 0; i < codeWidth; i++) {
                for (int j = 0; j < codeHeight; j++) {
                    //4、循环将二维码内容定入图片
                    image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
                }
        }
        return imageToString(image);
    }

    //把image对象转换成base64方法
    public static String imageToString(BufferedImage image) throws IOException{
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image,imageType,os);
        return base64.encodebase64String(os.toByteArray());
    }

}
控制类QRCodeController
import com.coupon.dto.Result;
import com.coupon.util.QRCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


//@SuppressWarnings("all")
@RestController
@RequestMapping("/qrcode")
@Api(tags = "二维码管理")
public class QRCodeController {

    @Autowired
    QRCodeUtils qrCodeUtils;

//    @ResponseBody
    @ApiOperation(value = "生成二维码")
    @PostMapping("/create")
    public String createQRCode(@RequestParam(value = "text")String text) throws Exception {
        return qrCodeUtils.zxingCodeCreate(text);
    }

}

前端页面实现



    
    单文件上传



    
    
    

参考资料:
b站视频:https://www.bilibili.com/video/BV1ez4y1S7h7?from=search&seid=13314849704773297464&spm_id_from=333.337.0.0
csdn资料:https://blog.csdn.net/weixin_42290280/article/details/90731220

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号