java批量生成带logo的二维码图片,并在图片下方附文字,压缩打包下载

web层:

@RequestMapping("downloadZipQr") public void downloadZipQr(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestParam("data") List<String> data) throws IOException, WriterException { zipService.downloadZipQr(httpServletRequest, httpServletResponse, data); }


service层:

void downloadZipQr(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, List<String> data) throws WriterException, IOException;

serviceImpl实现层:

@Service("zipService") public class ZipServiceImpl implements ZipService { @Override public void downloadAllQr(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, List<String> data) throws WriterException, IOException { // TODO Auto-generated method stub String date = DateUtil.DateToStringShort(new Date()); httpServletResponse.setContentType("application/zip"); httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + new String((date+"qr"+data.size()).getBytes(), "ISO-8859-1") + ".zip"); OutputStream outputStream = httpServletResponse.getOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); for (String mac : data) { // 生成二维码 // String string = qrCodeServiceImpl.LogoMatrix(new QRRequest()); ZipEntry entry = new ZipEntry(mac + "." + "JPG"); zipOutputStream.putNextEntry(entry); QRRequest qRRequest = new QRRequest(); qRRequest.setFile_extension("png"); qRRequest.setAttachContents("Mac:" + mac); qRRequest.setContents("deviceType" + mac); qRRequest.setFileName("deviceType" + mac); LogoMatrixText(qRRequest, zipOutputStream); zipOutputStream.flush(); } zipOutputStream.close(); outputStream.flush(); outputStream.close(); } public static BufferedImage encode(QRRequest qrRquest) { // TODO Auto-generated method stub // 生成条形码时的一些配置 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%) hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 内容所使用字符集编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix; BufferedImage image = null; try { // 生成二维码 bitMatrix = new MultiFormatWriter().encode(qrRquest.getContents(), BarcodeFormat.QR_CODE, qrRquest.getWidth(), qrRquest.getHeight(), hints); image = MatrixToImageWriter.toBufferedImage(bitMatrix); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } return image; } public static String LogoMatrixText(QRRequest qrRquest, ZipOutputStream zipOutputStream) { // File image, String uploadPath, String realUploadPath, String imgPath) // { // TODO Auto-generated method stub /**
         * 读取二维码图片,并构建绘图对象
         */ OutputStream os = null; String logoFilePath = "c:/logo/logo.png"; String bgFath = "c:/logo/bg.png"; try { BufferedImage imageQr = encode(qrRquest); if (imageQr == null) { return null; } Image image = ImageIO.read(new File(bgFath)); int width = image.getWidth(null); int height = image.getHeight(null); BufferedImage bufferImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bufferImage.createGraphics(); g2.drawImage(image, 0, 0, width, height, null); int matrixWidth = bufferImage.getWidth(); int matrixHeigh = bufferImage.getHeight(); // Image image2 = ImageIO.read(new File(qrFilePath)); g2.drawImage(imageQr, 0, 0, matrixWidth, matrixWidth, null); // 读取Logo图片 BufferedImage logo = ImageIO.read(new File(logoFilePath)); // 开始绘制图片 g2.drawImage(logo, matrixWidth / 5 * 2, matrixWidth / 5 * 2, matrixWidth / 5, matrixWidth / 5, null);// 绘制 BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke);// 设置笔画对象 // 指定弧度的圆角矩形 RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth / 5 * 2, matrixWidth / 5 * 2, matrixWidth / 5, matrixWidth / 5, 20, 20); g2.setColor(Color.white); g2.draw(round);// 绘制圆弧矩形 // 设置logo 有一道灰色边框 BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 设置笔画对象 RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth / 5 * 2 + 2, matrixWidth / 5 * 2 + 2, matrixWidth / 5 - 4, matrixWidth / 5 - 4, 20, 20); g2.setColor(new Color(128, 128, 128)); g2.draw(round2);// 绘制圆弧矩形 Font f = new Font("微软雅黑", Font.PLAIN, 56); Color mycolor = Color.black;// new Color(0, 0, 255); g2.setColor(mycolor); g2.setFont(f); g2.drawString(qrRquest.getAttachContents(), matrixWidth - 570, matrixHeigh - 70); g2.dispose(); bufferImage.flush(); ImageIO.write(bufferImage, qrRquest.getFile_extension(), zipOutputStream); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return qrRquest.getFileName() + "." + qrRquest.getFile_extension(); } }

 

上一篇:计算图 graph


下一篇:图论复习第一章