JAVA生成二維碼,圖片合成,圖片新增文字
阿新 • • 發佈:2018-12-17
首先引入zxing用於生成二維碼
<!-- https://mvnrepository.com/artifact/com.google.zxing/core --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.zxing/javase --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
直接上程式碼
package com.yabo.core.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.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Objects; /** * 二維碼以及圖片合成工具類 * * @author taiyi */ public class ZxingUtils { public static BufferedImage enQRCode(String contents, int width, int height) throws WriterException { //定義二維碼引數 final Map<EncodeHintType, Object> hints = new HashMap(8) { { //編碼 put(EncodeHintType.CHARACTER_SET, "UTF-8"); //容錯級別 put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //邊距 put(EncodeHintType.MARGIN, 0); } }; return enQRCode(contents, width, height, hints); } /** * 生成二維碼 * * @param contents 二維碼內容 * @param width 圖片寬度 * @param height 圖片高度 * @param hints 二維碼相關引數 * @return BufferedImage物件 * @throws WriterException 編碼時出錯 * @throws IOException 寫入檔案出錯 */ public static BufferedImage enQRCode(String contents, int width, int height, Map hints) throws WriterException { // String uuid = UUID.randomUUID().toString().replace("-", ""); //本地完整路徑 // String pathname = path + "/" + uuid + "." + format; //生成二維碼 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); // Path file = new File(pathname).toPath(); //將二維碼儲存到路徑下 // MatrixToImageWriter.writeToPa(bitMatrix, format, file); // return pathname; return MatrixToImageWriter.toBufferedImage(bitMatrix); } /** * 將圖片繪製在背景圖上 * * @param backgroundPath 背景圖路徑 * @param zxingImage 圖片 * @param x 圖片在背景圖上繪製的x軸起點 * @param y 圖片在背景圖上繪製的y軸起點 * @return */ public static BufferedImage drawImage(String backgroundPath, BufferedImage zxingImage, int x, int y) throws IOException { //讀取背景圖的圖片流 BufferedImage backgroundImage; //Try-with-resources 資源自動關閉,會自動呼叫close()方法關閉資源,只限於實現Closeable或AutoCloseable介面的類 try (InputStream imagein = new FileInputStream(backgroundPath)) { backgroundImage = ImageIO.read(imagein); } return drawImage(backgroundImage, zxingImage, x, y); } /** * 將圖片繪製在背景圖上 * * @param backgroundImage 背景圖 * @param zxingImage 圖片 * @param x 圖片在背景圖上繪製的x軸起點 * @param y 圖片在背景圖上繪製的y軸起點 * @return * @throws IOException */ public static BufferedImage drawImage(BufferedImage backgroundImage, BufferedImage zxingImage, int x, int y) throws IOException { Objects.requireNonNull(backgroundImage, ">>>>>背景圖不可為空"); Objects.requireNonNull(zxingImage, ">>>>>二維碼不可為空"); //二維碼寬度+x不可以超過背景圖的寬度,長度同理 if ((zxingImage.getWidth() + x) > backgroundImage.getWidth() || (zxingImage.getHeight() + y) > backgroundImage.getHeight()) { throw new IOException(">>>>>二維碼寬度+x不可以超過背景圖的寬度,長度同理"); } //合併圖片 Graphics2D g = backgroundImage.createGraphics(); g.drawImage(zxingImage, x, y, zxingImage.getWidth(), zxingImage.getHeight(), null); return backgroundImage; } /** * 將文字繪製在背景圖上 * * @param backgroundImage 背景圖 * @param x 文字在背景圖上繪製的x軸起點 * @param y 文字在背景圖上繪製的y軸起點 * @return * @throws IOException */ public static BufferedImage drawString(BufferedImage backgroundImage, String text, int x, int y,Font font,Color color) { //繪製文字 Graphics2D g = backgroundImage.createGraphics(); //設定顏色 g.setColor(color); //消除鋸齒狀 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); //設定字型 g.setFont(font); //繪製文字 g.drawString(text, x, y); return backgroundImage; } public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage) throws IOException { return bufferedImageToInputStream(backgroundImage, "png"); } /** * backgroundImage 轉換為輸出流 * * @param backgroundImage * @param format * @return * @throws IOException */ public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage, String format) throws IOException { ByteArrayOutputStream bs = new ByteArrayOutputStream(); try ( ImageOutputStream imOut = ImageIO.createImageOutputStream(bs)) { ImageIO.write(backgroundImage, format, imOut); InputStream is = new ByteArrayInputStream(bs.toByteArray()); return is; } } /** * 儲存為檔案 * * @param is * @param fileName * @throws IOException */ public static void saveFile(InputStream is, String fileName) throws IOException { try (BufferedInputStream in = new BufferedInputStream(is); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) { int len; byte[] b = new byte[1024]; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } } } public static void main(String[] args) { //二維碼寬度 int width = 293; //二維碼高度 int height = 293; //二維碼內容 String contcent = "http://baidu.com"; BufferedImage zxingImage = null; try { //二維碼圖片流 zxingImage = ZxingUtils.enQRCode(contcent, width, height); } catch (WriterException e) { e.printStackTrace(); } //背景圖片地址 String backgroundPath = "C:/Users/saer/Desktop/background.png"; InputStream inputStream = null; try { //合成二維碼和背景圖 BufferedImage image = ZxingUtils.drawImage(backgroundPath, zxingImage, 224, 754); //繪製文字 Font font = new Font("微軟雅黑", Font.BOLD, 35); String text = "17000"; image = ZxingUtils.drawString(image, text, 375, 647,font,new Color(244,254,189)); //圖片轉inputStream inputStream = ZxingUtils.bufferedImageToInputStream(image); } catch (IOException e) { e.printStackTrace(); } //儲存的圖片路徑 String originalFileName = "C:/Users/saer/Desktop/99999.png"; try { //儲存為本地圖片 ZxingUtils.saveFile(inputStream, originalFileName); } catch (IOException e) { e.printStackTrace(); } } }
背景圖片自己隨便找一張就可以,儲存的圖片路徑設定為桌面路徑方便檢視