package com.dy.pmsBase.code;
|
|
|
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.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 = "markCode")
|
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked")
|
public class MarkCodeCtrl {
|
|
/**
|
* 客户端请求得到默认密码
|
* @return 默认密码
|
*/
|
@GetMapping(path = "show")
|
public BaseResponse<List<ResultVo>> show() {
|
try{
|
List<ResultVo> list = new ArrayList<>() ;
|
URL logoUrl = MarkCodeCtrl.class.getResource("/images/logo.png") ;
|
String[][] marks = QrCodeConstant.Marks() ;
|
for(String[] mark : marks){
|
ResultVo vo = new ResultVo() ;
|
vo.code = mark[0] ;
|
vo.name = mark[1] ;
|
BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(mark[0], QrCodeConstant.MarkQrCodeWidth, QrCodeConstant.MarkQrCodeHeight));
|
if(logoUrl != null){
|
bufferedImage = QrCodeGenerator.addQrCodeLogo(bufferedImage, new File(logoUrl.getFile()));
|
}
|
byte[] codes = QrCodeGenerator.bufferedImageToByteArray(bufferedImage, "JPG");
|
vo.imgBase64 = Base64.encode(codes);
|
list.add(vo) ;
|
}
|
return BaseResponseUtils.buildSuccess(list);
|
}catch (Exception e){
|
log.error("生成标识类二维码时异常", e);
|
return BaseResponseUtils.buildException(e.getMessage());
|
}
|
}
|
}
|