java练习:二维码生成和输出
<!--二维码生成-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
package com.example.myblog.util; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.awt.image.BufferedImage; import java.util.Hashtable; public class QRCodeUtil { private static final String CHARSET = "utf-8"; // 二维码尺寸 private static final int QRCODE_SIZE = 300; public static BufferedImage createImage(String content) { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = null; try { bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); } catch (WriterException e) { e.printStackTrace(); } int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } return image; } }
package com.example.myblog.api; import com.example.myblog.util.QRCodeUtil; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import io.swagger.annotations.ApiParam; import org.springframework.web.bind.annotation.*; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Hashtable; import java.util.UUID; /** * @author wyk on 2018/10/27 */ @RestController @RequestMapping("/qrcode") public class QRCodeApi { private static final String CHARSET = "utf-8"; // 二维码尺寸 private static final int QRCODE_SIZE = 300; // LOGO宽度 private static final int LOGO_WIDTH = 80; // LOGO高度 private static final int LOGO_HEIGHT = 80; @RequestMapping(value = "/creatImg/{content}",method = RequestMethod.GET) public BufferedImage createImage( @PathVariable String content ) { System.out.println(content); Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = null; try { bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); } catch (WriterException e) { e.printStackTrace(); } int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } return image; } /** * 插入LOGO * * @param source 二维码图片 * @param logoPath LOGO图片地址 * @param needCompress 是否压缩 * @throws Exception */ public static void insertImage(BufferedImage source, InputStream logoPath, boolean needCompress) { Image src = null; try { src = ImageIO.read(logoPath); } catch (IOException e) { e.printStackTrace(); } int width = src.getWidth(null); int height = src.getHeight(null); if (needCompress) { // 压缩LOGO if (width > LOGO_WIDTH) { width = LOGO_WIDTH; } if (height > LOGO_HEIGHT) { height = LOGO_HEIGHT; } Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); // 绘制缩小后的图 g.drawImage(image, 0, 0, null); g.dispose(); src = image; } // 插入LOGO Graphics2D graph = source.createGraphics(); int x = (QRCODE_SIZE - width) / 2; int y = (QRCODE_SIZE - height) / 2; graph.drawImage(src, x, y, width, height, null); Shape shape = new RoundRectangle2D.Float(x, y, width, width, 12, 12); graph.setStroke(new BasicStroke(3f)); graph.draw(shape); graph.dispose(); } @RequestMapping("/generate") @ResponseBody public void generateQR(@RequestParam("content") String content, HttpServletResponse response){ BufferedImage image; // 禁止图像缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); image = QRCodeUtil.createImage(content); // 创建二进制的输出流 try(ServletOutputStream sos = response.getOutputStream()){ // 将图像输出到Servlet输出流中。 ImageIO.write(image, "jpeg", sos); } catch (IOException e) { e.printStackTrace(); } } // 输出为二维码图片 @RequestMapping(value = "/generate/{content}",method = RequestMethod.GET) public void generate( @PathVariable String content, HttpServletResponse response ){ BufferedImage image; // 禁止图像缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); image = QRCodeUtil.createImage(content); // 创建二进制的输出流 try(ServletOutputStream sos = response.getOutputStream()){ // 将图像输出到Servlet输出流中。 ImageIO.write(image, "jpeg", sos); } catch (IOException e) { e.printStackTrace(); } } // 保存为一个文件 @RequestMapping(value = "/generate2/{content}",method = RequestMethod.GET) public String generate2( @PathVariable String content, HttpServletResponse response ){ BufferedImage image; // 禁止图像缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); image = QRCodeUtil.createImage(content); String fileName = String.valueOf(UUID.randomUUID()).replaceAll("-",""); String path = "D:\\dataa\\ceshi\\"+fileName+".jpeg"; try{ File file = new File(path); ImageIO.write(image,"jpeg",file); }catch (IOException e){ e.printStackTrace(); } return path; } }