|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cn.hutool.core.codec.Base64; | 
|---|
|  |  |  | import com.alibaba.excel.EasyExcel; | 
|---|
|  |  |  | import com.dy.common.util.QrCodeGenerator; | 
|---|
|  |  |  | import com.alibaba.excel.converters.Converter; | 
|---|
|  |  |  | import com.dy.common.webUtil.BaseResponse; | 
|---|
|  |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
|---|
|  |  |  | import com.dy.pmsGlobal.util.QrCodeConstant; | 
|---|
|  |  |  | import com.dy.pmsGlobal.util.QrCodeUtil; | 
|---|
|  |  |  | import jakarta.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | 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.URLEncoder; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @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()); | 
|---|
|  |  |  | public BaseResponse<List<ResultVo>> show() throws Exception{ | 
|---|
|  |  |  | List<ResultVo> list = new ArrayList<>() ; | 
|---|
|  |  |  | String[][] marks = QrCodeConstant.Marks() ; | 
|---|
|  |  |  | for(String[] mark : marks){ | 
|---|
|  |  |  | ResultVo vo = new ResultVo() ; | 
|---|
|  |  |  | vo.code = mark[0] ; | 
|---|
|  |  |  | vo.name = mark[1] ; | 
|---|
|  |  |  | byte[] codes = QrCodeUtil.genQrCode(vo.code); | 
|---|
|  |  |  | vo.imgBase64 = "data:image/jpeg;base64," + Base64.encode(codes); | 
|---|
|  |  |  | list.add(vo) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return BaseResponseUtils.buildSuccess(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping(path = "download") | 
|---|
|  |  |  | public void download(HttpServletResponse response){ | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman | 
|---|
|  |  |  | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); | 
|---|
|  |  |  | response.setCharacterEncoding("utf-8"); | 
|---|
|  |  |  | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 | 
|---|
|  |  |  | String fileName = URLEncoder.encode(MarkQrCodeCtrl.fileName, "UTF-8").replaceAll("\\+", "%20"); | 
|---|
|  |  |  | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<ExcelVo> list = new ArrayList<>() ; | 
|---|
|  |  |  | Resource resource = new ClassPathResource("/images/logo.png"); | 
|---|
|  |  |  | File logoPngFile = resource.getFile() ; | 
|---|
|  |  |  | String[][] marks = QrCodeConstant.Marks() ; | 
|---|
|  |  |  | for(String[] mark : marks){ | 
|---|
|  |  |  | ExcelVo vo = new ExcelVo() ; | 
|---|
|  |  |  | vo.code = mark[0] ; | 
|---|
|  |  |  | vo.name = mark[1] ; | 
|---|
|  |  |  | BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(vo.code, QrCodeConstant.MarkQrCodeWidth, QrCodeConstant.MarkQrCodeHeight)); | 
|---|
|  |  |  | if(logoPngFile != null && logoPngFile.exists()){ | 
|---|
|  |  |  | bufferedImage = QrCodeGenerator.addQrCodeLogo(bufferedImage, logoPngFile); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | vo.image = QrCodeGenerator.bufferedImageToByteArray(bufferedImage, "JPG"); | 
|---|
|  |  |  | list.add(vo) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | EasyExcel.write(response.getOutputStream(), ExcelVo.class).sheet(MarkQrCodeCtrl.sheetName).doWrite(list); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | log.error("下载标识类二维码时异常", e); | 
|---|
|  |  |  | public void download(HttpServletResponse response) throws Exception{ | 
|---|
|  |  |  | List<Converter> list = new ArrayList<>() ; | 
|---|
|  |  |  | String[][] marks = QrCodeConstant.Marks() ; | 
|---|
|  |  |  | for(String[] mark : marks){ | 
|---|
|  |  |  | ExcelVo vo = new ExcelVo() ; | 
|---|
|  |  |  | vo.code = mark[0] ; | 
|---|
|  |  |  | vo.name = mark[1] ; | 
|---|
|  |  |  | vo.image = QrCodeUtil.genQrCode(vo.code); | 
|---|
|  |  |  | list.add(vo) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | QrCodeUtil.downloadExcel(response, fileName, sheetName, list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|