java生成帶logo的二維碼

隨着微信在市場上的佔有率不對升高,二維碼開始逐漸進入人們的視野,掃碼支付,掃碼關注,掃碼打開鏈接...java

究竟二維碼是個什麼東西我就不在這裏贅述了,關於這方面你們能夠去上搜索引擎apache

我在這裏就簡單介紹一下一個Java的生成二維碼工具,是Google提供的,zxing微信

上代碼,工具

  1 package com.xxxx.xx;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Graphics2D;
  6 import java.awt.image.BufferedImage;
  7 import java.io.File;
  8 import java.util.HashMap;
  9 import java.util.Map;
 10 import java.util.Objects;
 11 
 12 import javax.imageio.ImageIO;
 13 
 14 import org.apache.commons.lang3.StringUtils;
 15 
 16 import com.google.zxing.BarcodeFormat;
 17 import com.google.zxing.EncodeHintType;
 18 import com.google.zxing.MultiFormatWriter;
 19 import com.google.zxing.WriterException;
 20 import com.google.zxing.common.BitMatrix;
 21 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 22 
 23 /**
 24  * 畫制定logo和制定描述的二維碼
 25  * 
 26  * @author songyz
 27  *
 28  */
 29 public class ZXingCode {
 30     private static final int QRCOLOR = 0xFF000000; // 默認是黑色
 31     private static final int BGWHITE = 0xFFFFFFFF; // 背景顏色
 32 
 33     private static final int WIDTH = 400; // 二維碼寬
 34     private static final int HEIGHT = 400; // 二維碼高
 35 
 36     // 用於設置QR二維碼參數
 37     private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
 38         private static final long serialVersionUID = 1L;
 39         {
 40             put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 設置QR二維碼的糾錯級別(H爲最高級別)具體級別信息
 41             put(EncodeHintType.CHARACTER_SET, "utf-8");// 設置編碼方式
 42             put(EncodeHintType.MARGIN, 0);
 43         }
 44     };
 45 
 46     public static void main(String[] args) throws WriterException {
 47         File logoFile = new File("D://QrCode/logo3.png");
 48         File QrCodeFile = new File("D://QrCode/05.png");
 49         String url = "https://www.baidu.com/";
 50         String note = "訪問百度鏈接";
 51         drawLogoQRCode(logoFile, QrCodeFile, url, note);
 52     }
 53 
 54     // 生成帶logo的二維碼圖片
 55     public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {
 56         try {
 57             MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
 58             // 參數順序分別爲:編碼內容,編碼類型,生成圖片寬度,生成圖片高度,設置參數
 59             BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
 60             BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
 61 
 62             // 開始利用二維碼數據建立Bitmap圖片,分別設爲黑(0xFFFFFFFF)白(0xFF000000)兩色
 63             for (int x = 0; x < WIDTH; x++) {
 64                 for (int y = 0; y < HEIGHT; y++) {
 65                     image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
 66                 }
 67             }
 68 
 69             int width = image.getWidth();
 70             int height = image.getHeight();
 71             if (Objects.nonNull(logoFile) && logoFile.exists()) {
 72                 // 構建繪圖對象
 73                 Graphics2D g = image.createGraphics();
 74                 // 讀取Logo圖片
 75                 BufferedImage logo = ImageIO.read(logoFile);
 76                 // 開始繪製logo圖片
 77                 g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
 78                 g.dispose();
 79                 logo.flush();
 80             }
 81 
 82             // 自定義文本描述
 83             if (StringUtils.isNotEmpty(note)) {
 84                 // 新的圖片,把帶logo的二維碼下面加上文字
 85                 BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
 86                 Graphics2D outg = outImage.createGraphics();
 87                 // 畫二維碼到新的面板
 88                 outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
 89                 // 畫文字到新的面板
 90                 outg.setColor(Color.BLACK);
 91                 outg.setFont(new Font("楷體", Font.BOLD, 30)); // 字體、字型、字號
 92                 int strWidth = outg.getFontMetrics().stringWidth(note);
 93                 if (strWidth > 399) {
 94                     // //長度過長就截取前面部分
 95                     // 長度過長就換行
 96                     String note1 = note.substring(0, note.length() / 2);
 97                     String note2 = note.substring(note.length() / 2, note.length());
 98                     int strWidth1 = outg.getFontMetrics().stringWidth(note1);
 99                     int strWidth2 = outg.getFontMetrics().stringWidth(note2);
100                     outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
101                     BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
102                     Graphics2D outg2 = outImage2.createGraphics();
103                     outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
104                     outg2.setColor(Color.BLACK);
105                     outg2.setFont(new Font("宋體", Font.BOLD, 30)); // 字體、字型、字號
106                     outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
107                     outg2.dispose();
108                     outImage2.flush();
109                     outImage = outImage2;
110                 } else {
111                     outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 畫文字
112                 }
113                 outg.dispose();
114                 outImage.flush();
115                 image = outImage;
116             }
117 
118             image.flush();
119 
120             ImageIO.write(image, "png", codeFile); // TODO
121         } catch (Exception e) {
122             e.printStackTrace();
123         }
124     }
125 
126 }

效果以下字體

相關文章
相關標籤/搜索