java生成二維碼,在中間附上logo,下方附上文字,img標籤嵌入
阿新 • • 發佈:2018-11-09
package com.audaque.web.action; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Objects; import javax.imageio.ImageIO; import org.apache.commons.lang3.StringUtils;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; /** * 畫制定logo和制定描述的二維碼 * * @authorsongyz * */ public class ZXingCode { private static final int QRCOLOR = 0xFF000000; // 預設是黑色 private static final int BGWHITE = 0xFFFFFFFF; // 背景顏色 private static final int WIDTH = 400; // 二維碼寬 private static final int HEIGHT = 400; // 二維碼高 // 用於設定QR二維碼引數 private static Map<EncodeHintType, Object> hints= new HashMap<EncodeHintType, Object>() { private static final long serialVersionUID = 1L; { put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 設定QR二維碼的糾錯級別(H為最高級別)具體級別資訊 put(EncodeHintType.CHARACTER_SET, "utf-8");// 設定編碼方式 put(EncodeHintType.MARGIN, 0); } }; public static void main(String[] args) throws WriterException { File logoFile = new File("F://1.png"); File QrCodeFile = new File("F://2.png"); String url = "http://124.do?appId=BEkPU4wpdaHu9SLPfmj1&responseType=cw2&redianager/security/getCode/code/1411a83d-e2f3-4f05-b89f-660bf4b24af1.go"; String note = "訪問百度連線"; ZXingCode zXingCode=new ZXingCode(); zXingCode.drawLogoQRCode(logoFile, QrCodeFile, url, note); } // 生成帶logo的二維碼圖片 public BufferedImage drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) { BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); try { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); // 引數順序分別為:編碼內容,編碼型別,生成圖片寬度,生成圖片高度,設定引數 BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints); // 開始利用二維碼資料建立Bitmap圖片,分別設為黑(0xFFFFFFFF)白(0xFF000000)兩色 for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE); } } int width = image.getWidth(); int height = image.getHeight(); if ( logoFile.exists()) { // 構建繪圖物件 Graphics2D g = image.createGraphics(); // 讀取Logo圖片 BufferedImage logo = ImageIO.read(logoFile); // 開始繪製logo圖片 g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null); g.dispose(); logo.flush(); } // 自定義文字描述 if (StringUtils.isNotEmpty(note)) { // 新的圖片,把帶logo的二維碼下面加上文字 BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D outg = outImage.createGraphics(); // 畫二維碼到新的面板 outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); // 畫文字到新的面板 outg.setColor(Color.BLACK); outg.setFont(new Font("楷體", Font.BOLD, 30)); // 字型、字型、字號 int strWidth = outg.getFontMetrics().stringWidth(note); if (strWidth > 399) { // //長度過長就擷取前面部分 // 長度過長就換行 String note1 = note.substring(0, note.length() / 2); String note2 = note.substring(note.length() / 2, note.length()); int strWidth1 = outg.getFontMetrics().stringWidth(note1); int strWidth2 = outg.getFontMetrics().stringWidth(note2); outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12); BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D outg2 = outImage2.createGraphics(); outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null); outg2.setColor(Color.BLACK); outg2.setFont(new Font("宋體", Font.BOLD, 30)); // 字型、字型、字號 outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5); outg2.dispose(); outImage2.flush(); outImage = outImage2; } else { outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 畫文字 } outg.dispose(); outImage.flush(); image = outImage; } image.flush(); ImageIO.write(image, "png", codeFile); // TODO } catch (Exception e) { e.printStackTrace(); } return image; }
}
/** * @Author: LGH * @Description: 生成二維碼 * @param: * @DATA: Created in 14:26 2017/12/10 */ @RequestMapping(value = "/getImg/{url}.go", method = { RequestMethod.GET, RequestMethod.POST}) @ResponseBody public String getImg(@PathVariable("url") String url,HttpServletResponse response) { try { int width = Constants.WIDTH; // 影象寬度 int height = Constants.HEIGHT; // 影象高度 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, 1);//外邊框邊距 BitMatrix bitMatrix = new MultiFormatWriter().encode( System.getProperty("appserver.url")+url, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩陣 BufferedImage bufImg = toBufferedImage(bitMatrix); // 生成二維碼QRCode圖片 File logoFile = new File("F://1.png"); File QrCodeFile = new File("F://2.png"); String url2 = "SLPfmj1&respe2f3-4f05-b89f-660bf4b24af1.go"; String note = "訪問百度連線"; ZXingCode zXingCode=new ZXingCode(); bufImg=zXingCode.drawLogoQRCode(logoFile, QrCodeFile, url2, note); ImageIO.write(bufImg, "png", response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } return null; }
js程式碼