生活随笔
收集整理的這篇文章主要介紹了
Java实现二维码生成及图片合成
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、需求
- 二、依賴導入
- 三、ZXing生成二維碼圖片的工具類
- 四、合成圖片的工具類
- 五、實踐
- 結語
一、需求
??日常中一個很常見的小需求:把一個地址做成二維碼碼牌,比如商戶收銀,關注公眾號等等。其實學習了java,你也可以實現這樣的功能,現在我們就一起來做一個吧。本文的事例就是把我的博客地址做成一個碼牌,流程如下:
二、依賴導入
小提示:主要采用com.google.zxing的jar,如果是web應用,則core包與javase包一起導入;如果是非web應用則只導入core包即可。
項目類型core包javase包
| web項目 | 導入 | 導入 |
| 普通Java項目 | 導入 | – |
具體依賴如下:
<dependency><groupId>com.google.zxing
</groupId><artifactId>core
</artifactId><version>3.4.1
</version>
</dependency>
<dependency><groupId>com.google.zxing
</groupId><artifactId>javase
</artifactId><version>3.4.1
</version>
</dependency>
三、ZXing生成二維碼圖片的工具類
QRCodeUtil.java
本工具類是通過com.google.zxing來實現生成二維碼的
package com.alian.csdn.utils;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.datamatrix.encoder.SymbolShapeHint;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class QRCodeUtil {private static final int BLACK
= 0xFF000000;private static final int WHITE
= 0xFFFFFFFF;private static final String CHARSET
= "UTF-8";public static String createImage(String content
, int width
, int height
, String fileName
, String imageType
, String filePath
) {MultiFormatWriter multiFormatWriter
= new MultiFormatWriter();Map<EncodeHintType, Object> hints
= new HashMap<>();hints
.put(EncodeHintType.CHARACTER_SET
, CHARSET
);hints
.put(EncodeHintType.ERROR_CORRECTION
, ErrorCorrectionLevel.H);hints
.put(EncodeHintType.DATA_MATRIX_SHAPE
, SymbolShapeHint.FORCE_SQUARE
);try {BitMatrix bitMatrix
= multiFormatWriter
.encode(content
, BarcodeFormat.QR_CODE
, width
, height
, hints
);File file
= new File(filePath
, fileName
+ "." + imageType
);bitMatrix
= deleteWhite(bitMatrix
);writeToFile(bitMatrix
, imageType
, file
);return filePath
+ File.separator
+ fileName
+ "." + imageType
;} catch (WriterException | IOException e
) {e
.printStackTrace();}return "";}public static BitMatrix deleteWhite(BitMatrix matrix
) {int[] rec
= matrix
.getEnclosingRectangle();int resWidth
= rec
[2];int resHeight
= rec
[3];BitMatrix resMatrix
= new BitMatrix(resWidth
, resHeight
);resMatrix
.clear();for (int i
= 0; i
< resWidth
; i
++) {for (int j
= 0; j
< resHeight
; j
++) {if (matrix
.get(i
+ rec
[0], j
+ rec
[1]))resMatrix
.set(i
, j
);}}return resMatrix
;}public static void writeToFile(BitMatrix matrix
, String format
, File file
) throws IOException {int width
= matrix
.getWidth();int height
= matrix
.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
, matrix
.get(x
, y
) ? BLACK
: WHITE
);}}if (!ImageIO.write(image
, format
, file
)) {throw new IOException("Could not write an image of format " + format
+ " to " + file
);}}
}
四、合成圖片的工具類
ImageUtil .java
本工具類是主要通過java基礎組件來實現圖片合成的(包含文字繪制)
package com.alian.csdn.utils;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageUtil {public static String merge(String backGroundImageUrl
, String qrCodeUrl
, String imageName
, String title
) {Font font
= new Font("黑體", Font.BOLD
, 120);try {BufferedImage backGroundImage
= ImageIO.read(new File(backGroundImageUrl
));BufferedImage imageCode
= ImageIO.read(new File(qrCodeUrl
));Graphics2D graphics
= backGroundImage
.createGraphics();graphics
.drawImage(imageCode
, 644, 1596, 1100, 1100, null);graphics
.setFont(font
);graphics
.setColor(Color.BLACK
);FontMetrics fontMetrics
= graphics
.getFontMetrics(font
);int textWidth
= fontMetrics
.stringWidth(title
);int widthX
= (backGroundImage
.getWidth() - textWidth
) / 2;graphics
.drawString(title
, widthX
, 2950);graphics
.dispose();File outPutFile
= new File(imageName
);ImageIO.write(backGroundImage
, "png", outPutFile
);} catch (Exception e
) {e
.printStackTrace();}return imageName
;}
}
五、實踐
接下里我們通過上述兩個工具類完成我們的小需求,具體如下:
@Testpublic void test2() {String qrCodeContent
= "https://blog.csdn.net/Alian_1223";System.out
.println("二維碼的內容:" + qrCodeContent
);String qrCodeImagePath
= "C:\\myFile\\CSDN";String qrCodeUrl
= QRCodeUtil.createImage(qrCodeContent
, 300, 300, "qrCode_Alian", "png", qrCodeImagePath
);System.out
.println("生成的二維碼地址:" + qrCodeUrl
);String backGroundUrl
= "C:\\myFile\\CSDN\\background.png";System.out
.println("背景圖片地址:" + backGroundUrl
);String imageName
= "C:\\myFile\\CSDN\\result.png";String title
= "嘉禾嘉寧PaPa";System.out
.println("要添加的文字信息:" + title
);ImageUtil.merge(backGroundUrl
, qrCodeUrl
, imageName
, title
);System.out
.println("合成圖片完成:" + imageName
);}
運行結果:
生成的二維碼地址:C:\myFile\CSDN\alian_qrCode.png
背景圖片地址:C:\myFile\CSDN\background.png
要添加的文字信息:嘉禾嘉寧PaPa
合成圖片完成:C:\myFile\CSDN\result.png
在C:\myFile\CSDN目錄下,我們可以看到已經合成的新的圖片result.png
一個關注我博客的碼牌就制作好了,感興趣的同學也可以試試
結語
以上就是今天要講的內容,本文針對現實場景下的碼牌簡單介紹了使用zxing生成二維碼,同時實現了兩個圖片的合成及文字的繪制,如果有什么疑問,歡迎大家評論交流。如果覺得不錯,可以幫忙一鍵三連,謝謝。
總結
以上是生活随笔為你收集整理的Java实现二维码生成及图片合成的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。