在如今的生活中,二維碼隨處可見,二維碼的出現既減少了宣傳紙張的浪費,又方便了人們的生活。這一篇我來說說 Java 利用第三方 Jar 包 zxing 生成二維碼。
1、下載zxing相關Jar包並引入工程中
若是maven工程,則在pom.xml中引入如下dependency即可,若不是maven工程,則可自行百度”zxing Jar“下載。
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
2、生成二維碼主要有兩種,一種是最普通的黑白二維碼,另一種是中間帶logo的彩色二維碼,下面來一一介紹。
1)普通二維碼
這種二維碼的實現原理比較簡單,就是根據二維碼的高度和寬度,再根據zxing中的算法,拆分成一個一個的像素點,用黑白兩色來填充即可。
/** * 生成二維碼圖片文件(不帶LOGO) * * @param content 要生成二維碼的內容 * @param width 二維碼的高度 * @param height 二維碼的寬度 * @return 二維碼圖片 * @throws WriterException 異常 */ private static BufferedImage genQrcode(String content, int width, int height) throws WriterException { Map<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //根據高度和寬度生成像素矩陣 BitMatrix bitMatrix = multiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB); for (int x = 0; x < bitMatrix.getWidth(); x++) { for (int y = 0; y < bitMatrix.getHeight(); y++) { //填充黑白兩色 image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE); } } return image; }
2)中間帶logo的彩色二維碼
這種二維碼的實現原理是根據二維碼的高度和寬度,再根據zxing中的算法,拆分成一個一個的像素點,用各種顏色來填充,實現彩色二維碼和漸變色二維碼。
中間的logo則是相同原理,將logo圖片拆解成像素點,在二維碼中間區域用logo的像素點來填充。
下面列舉幾種二維碼樣式的核心生成代碼
// 左上角顏色,根據自己需要調整顏色范圍和顏色 if (x > 0 && x < 170 && y > 0 && y < 170) { Color color = new Color(231, 144, 56); int colorInt = color.getRGB(); pixels[y * width + x] = matrix.get(x, y) ? colorInt : 16777215; }
// 讀取源圖像並拆分成像素點 BufferedImage scaleImage = scale(srcImagePath, IMAGE_WIDTH, IMAGE_HEIGHT, true); int[][] srcPixels = new int[IMAGE_WIDTH][IMAGE_HEIGHT]; for (int i = 0; i < scaleImage.getWidth(); i++) { for (int j = 0; j < scaleImage.getHeight(); j++) { srcPixels[i][j] = scaleImage.getRGB(i, j);//圖片的像素點 } } 。。。 // 將Logo圖片的像素點填充到二維碼中間區域 if (x > halfW - IMAGE_HALF_WIDTH && x < halfW + IMAGE_HALF_WIDTH && y > halfH - IMAGE_HALF_WIDTH && y < halfH + IMAGE_HALF_WIDTH) { pixels[y * width + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH]; }
if (matrix.get(x, y)) { //生成漸變色 if (x < width / 2) { int num1 = (int) (255 - (255.0 - 120.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num2 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num3 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); Color color = new Color(num1, num2, num3); int colorInt = color.getRGB(); // 此處可以修改二維碼的顏色,可以分別制定二維碼和背景的顏色; pixels[y * width + x] = matrix.get(x, y) ? colorInt : 0xFF000000;// 0x000000:0xffffff } else { int num1 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num2 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num3 = (int) (255 - (255.0 - 120.0) / matrix.getHeight() * (matrix.getHeight() - y)); Color color = new Color(num1, num2, num3); int colorInt = color.getRGB(); // 此處可以修改二維碼的顏色,可以分別制定二維碼和背景的顏色; pixels[y * width + x] = matrix.get(x, y) ? colorInt : 0xFF000000;// 0x000000:0xffffff } } else { pixels[y * width + x] = -1; //白色 }
if (matrix.get(x, y)) { //生成4色 if (x < width / 2 && y < height / 2) { pixels[y * width + x] = 0xFF0094FF;// 藍色 Integer.toHexString(new Random().nextInt()); } else if (x < width / 2 && y > height / 2) { pixels[y * width + x] = 0xFFFED545;// 黃色 } else if (x > width / 2 && y > height / 2) { pixels[y * width + x] = 0xFF5ACF00;// 綠色 } else { pixels[y * width + x] = 0xFF000000;// 黑色 } } else { pixels[y * width + x] = -1; //白色 }
3、總結
以上說明了幾種二維碼的生成原理,並列舉出了部份核心代碼,其實根據拆分像素的原理,可以用任何形式的像素內容替換二維碼中的像素,這樣則可進行二維碼的深度定制,生成其他更漂亮的二維碼。
這里推薦CSDN的”一灰灰blog“,這篇文章講解了如何深度定制二維碼,希望大家可以看看。
https://blog.csdn.net/liuyueyi25/article/details/77131810
(CSDN”一灰灰blog“深度定制的二維碼)
最后,貼上本篇文章中生成二維碼的完整代碼,供大家參考,希望能有所幫助。

package com.qrcode; 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 javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Random; public class CreateColourImageQRCode { // 圖片寬度的一半 private static final int IMAGE_WIDTH = 50; private static final int IMAGE_HEIGHT = 50; private static final int IMAGE_HALF_WIDTH = IMAGE_WIDTH / 2; private static final int FRAME_WIDTH = 2; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; // 二維碼寫碼器 private static MultiFormatWriter multiWriter = new MultiFormatWriter(); public static void main(String[] args) throws UnsupportedEncodingException { // 依次為內容,寬,長,儲存路徑 CreateColourImageQRCode.encode("https://www.baidu.com/", 500, 500, null, "d:\\qrcode.jpg"); // 依次為內容,寬,長,logo圖標路徑,儲存路徑 CreateColourImageQRCode.encode("https://www.baidu.com/", 500, 500, "d:\\logo.jpg", "d:\\qrcode2.jpg"); } /** * 生成二維碼 * * @param content 要生成二維碼的內容 * @param width 二維碼的寬度 * @param height 二維碼的高度 * @param srcImagePath 二維碼中間logo圖片的路徑 * @param destImagePath 生成二維碼圖片的路徑 * @return 是否生成成功 */ public static void encode(String content, int width, int height, String srcImagePath, String destImagePath) { try { if (srcImagePath == null || "".equals(srcImagePath)) { //生成二維碼圖片文件(不帶LOGO) ImageIO.write(genQrcode(content, width, height), "jpg", new File(destImagePath)); } else { //生成二維碼圖片文件(帶LOGO) ImageIO.write(genQrcodeWithLogo(content, width, height, srcImagePath), "jpg", new File(destImagePath)); } } catch (Exception e) { e.printStackTrace(); } } /** * 生成二維碼圖片文件(不帶LOGO) * * @param content 要生成二維碼的內容 * @param width 二維碼的高度 * @param height 二維碼的寬度 * @return 二維碼圖片 * @throws WriterException 異常 */ private static BufferedImage genQrcode(String content, int width, int height) throws WriterException { Map<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //根據高度和寬度生成像素矩陣 BitMatrix bitMatrix = multiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB); for (int x = 0; x < bitMatrix.getWidth(); x++) { for (int y = 0; y < bitMatrix.getHeight(); y++) { //填充黑白兩色 image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE); } } return image; } /** * 生成二維碼圖片文件(帶LOGO) * * @param content 要生成二維碼的內容 * @param width 二維碼的寬度 * @param height 二維碼的高度 * @param srcImagePath 二維碼中間logo圖片的路徑 * @return 二維碼圖片 * @throws WriterException 異常 * @throws IOException 異常 */ private static BufferedImage genQrcodeWithLogo(String content, int width, int height, String srcImagePath) throws WriterException, IOException { // 讀取源圖像並拆分成像素點 BufferedImage scaleImage = scale(srcImagePath, IMAGE_WIDTH, IMAGE_HEIGHT, true); int[][] srcPixels = new int[IMAGE_WIDTH][IMAGE_HEIGHT]; for (int i = 0; i < scaleImage.getWidth(); i++) { for (int j = 0; j < scaleImage.getHeight(); j++) { srcPixels[i][j] = scaleImage.getRGB(i, j);//圖片的像素點 } } //編碼 Map hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //設置容錯等級,在這里我們使用M級別 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 生成二維碼 BitMatrix matrix = multiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 二維矩陣轉為一維像素數組 int halfW = matrix.getWidth() / 2; int halfH = matrix.getHeight() / 2; int[] pixels = new int[width * height]; for (int y = 0; y < matrix.getHeight(); y++) { for (int x = 0; x < matrix.getWidth(); x++) { // 左上角顏色,根據自己需要調整顏色范圍和顏色 if (x > 0 && x < 170 && y > 0 && y < 170) { Color color = new Color(231, 144, 56); int colorInt = color.getRGB(); pixels[y * width + x] = matrix.get(x, y) ? colorInt : 16777215; } else // 將Logo圖片的像素點填充到二維碼中間區域 if (x > halfW - IMAGE_HALF_WIDTH && x < halfW + IMAGE_HALF_WIDTH && y > halfH - IMAGE_HALF_WIDTH && y < halfH + IMAGE_HALF_WIDTH) { pixels[y * width + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH]; } // 在圖片四周形成邊框 else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH) || (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH) || (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH - IMAGE_HALF_WIDTH + FRAME_WIDTH) || (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)) { pixels[y * width + x] = 0xfffffff; } else { if (matrix.get(x, y)) { // //生成4色 // if (x < width / 2 && y < height / 2) { // pixels[y * width + x] = 0xFF0094FF;// 藍色 // Integer.toHexString(new Random().nextInt()); // } else if (x < width / 2 && y > height / 2) { // pixels[y * width + x] = 0xFFFED545;// 黃色 // } else if (x > width / 2 && y > height / 2) { // pixels[y * width + x] = 0xFF5ACF00;// 綠色 // } else { // pixels[y * width + x] = 0xFF000000;// 黑色 // } //生成漸變色 if (x < width / 2) { int num1 = (int) (255 - (255.0 - 120.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num2 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num3 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); Color color = new Color(num1, num2, num3); int colorInt = color.getRGB(); // 此處可以修改二維碼的顏色,可以分別制定二維碼和背景的顏色; pixels[y * width + x] = matrix.get(x, y) ? colorInt : 0xFF000000;// 0x000000:0xffffff } else { int num1 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num2 = (int) (0 - (0.0 - 0.0) / matrix.getHeight() * (matrix.getHeight() - y)); int num3 = (int) (255 - (255.0 - 120.0) / matrix.getHeight() * (matrix.getHeight() - y)); Color color = new Color(num1, num2, num3); int colorInt = color.getRGB(); // 此處可以修改二維碼的顏色,可以分別制定二維碼和背景的顏色; pixels[y * width + x] = matrix.get(x, y) ? colorInt : 0xFF000000;// 0x000000:0xffffff } } else { pixels[y * width + x] = -1; //白色 } } } } BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getRaster().setDataElements(0, 0, width, height, pixels); return image; } /** * 把傳入的原始圖像按高度和寬度進行縮放,生成符合要求的圖標 * * @param srcImageFile 源文件地址 * @param height 目標高度 * @param width 目標寬度 * @param hasFiller 比例不對時是否需要補白:true為補白; false為不補白; * @throws IOException */ private static BufferedImage scale(String srcImageFile, int height, int width, boolean hasFiller) throws IOException { double ratio = 0.0; // 縮放比例 File file = new File(srcImageFile); BufferedImage srcImage = ImageIO.read(new File(srcImageFile)); Image destImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); // 計算比例 if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) { if (srcImage.getHeight() > srcImage.getWidth()) { ratio = (new Integer(height)).doubleValue() / srcImage.getHeight(); } else { ratio = (new Integer(width)).doubleValue() / srcImage.getWidth(); } AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null); destImage = op.filter(srcImage, null); } if (hasFiller) {// 補白 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphic = image.createGraphics(); graphic.setColor(Color.white); graphic.fillRect(0, 0, width, height); if (width == destImage.getWidth(null)) { graphic.drawImage(destImage, 0, (height - destImage.getHeight(null)) / 2, destImage.getWidth(null), destImage.getHeight(null), Color.white, null);//畫圖片 } else { graphic.drawImage(destImage, (width - destImage.getWidth(null)) / 2, 0, destImage.getWidth(null), destImage.getHeight(null), Color.white, null); } graphic.dispose(); destImage = image; } return (BufferedImage) destImage; } }