/** * 生成二维码图片并下载 * @param code 二维码主体内容 * @param width 二维码宽 * @param height 二维码高 * @param format 图片格式 * @param response * @throws WriterException * @throws IOException */ public static void downloadQR(String code, Integer width, Integer height, String format,HttpServletResponse response) throws WriterException, IOException { QRCodeWriter writer = new QRCodeWriter(); StringBuffer fileName = new StringBuffer(); fileName.append(new SimpleDateFormat("yyyyMMdd_hhmmss").format(new Date())).append(".").append(format); BitMatrix bitMatrix = writer.encode(code, BarcodeFormat.QR_CODE, width, height); response.setHeader("Content-Disposition", "attachment; filename=" + fileName.toString()); MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream()); } /** * 生成二维码在线浏览 * @param code 二维码主体内容 * @param outputStream * @throws WriterException * @throws IOException */ public static void viewRq(String code,OutputStream outputStream) throws WriterException, IOException { String format = "png"; Hashtable hints= new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(code, BarcodeFormat.QR_CODE, 500, 500,hints); MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream); }
java生成二维码在线预览,下载
最新推荐文章于 2024-07-23 13:58:41 发布