liuxm
2024-04-26 21587d964a6a43c4dff50a405b42509ce47504e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.dy.pmsBase.qrCode;
 
 
import cn.hutool.core.codec.Base64;
import com.dy.common.util.QrCodeGenerator;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pmsGlobal.util.QrCodeConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 生成标识类二维码
 */
@Slf4j
@RestController
@RequestMapping(path = "markQrCode")
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked")
public class MarkQrCodeCtrl {
 
    /**
     * 客户端请求得到默认密码
     * @return 默认密码
     */
    @GetMapping(path = "show")
    public BaseResponse<List<ResultVo>> show() {
        try{
            List<ResultVo> list = new ArrayList<>() ;
            Resource resource = new ClassPathResource("/images/logo.png");
            File logoPngFile = resource.getFile() ;
            String[][] marks = QrCodeConstant.Marks() ;
            for(String[] mark : marks){
                ResultVo vo = new ResultVo() ;
                vo.code = mark[0] ;
                vo.name = mark[1] ;
                //vo.code = "1234567890123456789012" ;
                //BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(vo.code, 30, 30));
                BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(vo.code, QrCodeConstant.MarkQrCodeWidth, QrCodeConstant.MarkQrCodeHeight));
                if(logoPngFile != null && logoPngFile.exists()){
                    bufferedImage = QrCodeGenerator.addQrCodeLogo(bufferedImage, logoPngFile);
                }
                byte[] codes = QrCodeGenerator.bufferedImageToByteArray(bufferedImage, "JPG");
                vo.imgBase64 = "data:image/jpeg;base64," + Base64.encode(codes);
                list.add(vo) ;
            }
            return BaseResponseUtils.buildSuccess(list);
        }catch (Exception e){
            log.error("生成标识类二维码时异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
}