久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Java生成二維碼的實例代碼

瀏覽:75日期:2022-08-25 11:04:30

使用開源的一維/二維碼圖形處理庫zxing GitHub地址

引入依賴

<!-- https://mvnrepository.com/artifact/com.google.zxing/core --><dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version></dependency><!-- https://mvnrepository.com/artifact/com.google.zxing/javase --><dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version></dependency>

封裝工具類

package com.app.utils; import java.awt.Color;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map; import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.client.j2se.MatrixToImageConfig;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * @title 生成二維碼工具類 * @author zch * @discribtion * @Date 2020年1月3日 下午4:26:05 * @vision V1.0 */public class QRCodeUtil{ private static final int width = 200; // 圖像寬度 private static final int height = 200; // 圖像高度 private static final int ON_COLOR = 0xFF000001; private static final int OFF_COLOR = 0xFFFFFFFF; /** * @title 生成二維碼圖片 * @discribtion * @author zch * @Date 2020年1月3日 下午3:27:21 * @param width 二維碼寬度,默認為200 * @param height 二維碼高度,默認為200 * @param content 二維碼內容,必填 * @param logoPath logo圖片路徑,若為空則生成不帶logo的二維碼 * @param imgPath 生成二維碼文件夾路徑 * @param imgName 生成二維碼圖片名稱,必填 * @param suffix 生成二維碼圖片后綴類型,例如:gif,必填 * @vision V1.0 */ public static boolean generateQRImage(Integer width, Integer height, String content, String logoPath, String imgPath, String imgName, String suffix) { if (content == null || imgName == null || suffix == null) { return false; } try { width = width == null ? QRCodeUtil.width : width; height = height == null ? QRCodeUtil.height : height; if (logoPath != null && !''.equals(logoPath.trim())) {QREncode(width, height, content, logoPath, imgPath, imgName, suffix); } else {QREncode(width, height, content, imgPath, imgName, suffix); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * @title 生成二維碼 * @discribtion * @author zch * @Date 2020年1月3日 下午3:27:21 * @vision V1.0 */ private static void QREncode(int width, int height, String content, String imgPath, String imgName, String suffix) throws Exception { File filePath = new File(imgPath); if (!filePath.exists()) { filePath.mkdirs(); } File imageFile = new File(imgPath, imgName); Map<EncodeHintType, Object> hints = new HashMap<>(); // 內容編碼格式 hints.put(EncodeHintType.CHARACTER_SET, 'UTF-8'); // 指定糾錯等級 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 設置二維碼邊的空度,非負數 hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToPath(bitMatrix, suffix, imageFile.toPath());// 輸出原圖片 } /** * @title 生成帶logo的二維碼 * @discribtion * @author zch * @Date 2020年1月3日 下午3:27:21 * @vision V1.0 */ private static void QREncode(int width, int height, String content, String logoPath, String imgPath, String imgName, String suffix) throws Exception { File filePath = new File(imgPath); if (!filePath.exists()) { filePath.mkdirs(); } File imageFile = new File(imgPath, imgName); Map<EncodeHintType, Object> hints = new HashMap<>(); // 內容編碼格式 hints.put(EncodeHintType.CHARACTER_SET, 'UTF-8'); // 指定糾錯等級 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 設置二維碼邊的空度,非負數 hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(ON_COLOR, OFF_COLOR); BufferedImage bufferedImage = LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig), new File(logoPath)); ImageIO.write(bufferedImage, suffix, imageFile);// 輸出帶logo圖片 } /** * @title 二維碼圖片添加logo * @discribtion * @author zch * @Date 2020年1月3日 下午3:27:21 * @param matrixImage 源二維碼圖片 * @param logoFile logo圖片 * @vision V1.0 */ private static BufferedImage LogoMatrix(BufferedImage matrixImage, File logoFile) throws IOException { // 讀取二維碼圖片,并構建繪圖對象 Graphics2D gs = matrixImage.createGraphics(); int matrixWidth = matrixImage.getWidth(); int matrixHeigh = matrixImage.getHeight(); int ratioWidth = matrixWidth * 2 / 10; int ratioHeight = matrixHeigh * 2 / 10; // 讀取Logo圖片 BufferedImage logo = ImageIO.read(logoFile); int logoWidth = logo.getWidth(null) > ratioWidth ? ratioWidth : logo.getWidth(null); int logoHeight = logo.getHeight(null) > ratioHeight ? ratioHeight : logo.getHeight(null); int x = (matrixWidth - logoWidth) / 2; int y = (matrixHeigh - logoHeight) / 2;// 繪制 gs.drawImage(logo, x, y, logoWidth, logoHeight, null); gs.setColor(Color.BLACK); gs.setBackground(Color.WHITE); gs.dispose(); matrixImage.flush(); return matrixImage; }}

測試生成二維碼

QRCodeUtil.generateQRImage(null, null, 'https://blog.csdn.net/qq_34928194', null, 'E:/', 'test.gif', 'gif');

以上就是Java生成二維碼的實例代碼的詳細內容,更多關于Java生成二維碼的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 中国一级免费毛片 | www.国产.com | 国产情侣自拍啪啪 | 福利亚洲 | 91啪影院 | 1级毛片 | 日韩视频中文字幕 | 成人免费在线观看 | 激情婷婷综合 | 一区二区三区免费网站 | 不卡一区二区三区视频 | 国产一区二区免费视频 | 四虎永久| 亚洲欧洲精品视频在线观看 | 97爱爱视频| 99精品国产热久久91蜜凸 | 黄色国产一级视频 | 中文字幕乱码一区二区三区 | 久草网站 | 亚洲精品在线网站 | 国产成人精品一区二区三区视频 | 国产免费一区二区三区四区五区 | 日本电影www| 欧美日韩在线视频免费 | 亚洲人成在线播放 | 午夜影院男女 | 免费日本视频 | 午夜精品亚洲日日做天天做 | 九九热精品免费视频 | 中文在线一区 | 久草视频在线播放 | 亚洲人成人一区二区在线观看 | 精品成人免费一区二区在线播放 | 另类亚洲专区 | 亚洲欧美日韩精品 | 91精品国产91久久综合桃花 | 国产成人午夜视频 | 毛片a级片 | 久久久久久九九 | 中国人xxxx片99ww| 中文一区二区 |