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

Java生成二维码(使用Google开源项目ZXing )

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

Java生成二维码(使用Google开源项目ZXing )

1. 添加依赖
		
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.google.zxing
            core
            3.4.1
        
        
            com.google.zxing
            javase
            3.4.1
        

        
        
            org.projectlombok
            lombok
        
2. 创建工具类
package com.chen.qrcode.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;


public class QrCodeUtil {

    
    public static byte[] generateQrCodeByte(String text, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        return pngOutputStream.toByteArray();
    }

    
    public static void generateQrCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

}

3. 创建测试控制层
package com.chen.qrcode.controller;

import com.chen.qrcode.util.QrCodeUtil;
import com.google.zxing.WriterException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Objects;


@RestController
@RequestMapping("/qr-code")
@Slf4j
public class QrCodeController {

    
    public static String url = "https://www.baidu.com/";

    
    @GetMapping("methodOne")
    public ResponseEntity methodOne() {
        byte[] qrCode = null;
        try {
            qrCode = QrCodeUtil.generateQrCodeByte(url, 350, 350);
        } catch (Exception e) {
            log.info("Exception:{}", e.getMessage());
        }
        // Header设置文件类型(对于ResponseEntity响应的方式,必须设置文件类型)
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

        return new ResponseEntity<>(qrCode, headers, HttpStatus.CREATED);
    }

    
    @GetMapping("methodTwo")
    public void methodTwo(HttpServletResponse response) throws IOException {
        byte[] qrCode = null;
        try {
            qrCode = QrCodeUtil.generateQrCodeByte(url, 350, 350);
        } catch (Exception e) {
            log.info("Exception:{}", e.getMessage());
        }
        // Header设置文件类型(ContentType不设置也没事)
        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        response.getOutputStream().write(Objects.requireNonNull(qrCode));
    }

    
    @GetMapping("methodThree")
    public void methodThree() throws IOException, WriterException {
        //下载路径及文件名
        String filePath = "D:\or-code.png";
        QrCodeUtil.generateQrCodeImage(url, 350, 350, filePath);
    }

}

4. 测试方法
  • methodOne

  • methodTwo

  • methodThree

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

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

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