国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

Java 生成帶Logo和文字的二維碼

瀏覽:57日期:2022-08-13 17:58:57

ZXing 是一個開放源碼的,用Java實現的多種格式的1D/2D條碼圖像處理庫,它包含了聯系到其他語言的端口。Zxing 可以實現使用手機的內置的攝像頭完成條形碼的掃描及解碼。本章講解用 ZXing 生成和掃碼二維碼。

依賴

在Java項目中pom.xml加入:

<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>${version}</version></dependency><dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>${version}</version></dependency>

當前最新版本是3.4.1,如果是Java開發的Android項目則引入 android-core 。

生成二維碼生成普通二維碼

// 二維碼內容String text = 'https://engr-z.com';// 二維碼大小int width = 500, height = 500;// 二維碼輸出文件File file = new File('/home/engr-z/qrcode.png');QRCodeWriter writer = new QRCodeWriter();BitMatrix m = writer.encode(text, BarcodeFormat.QR_CODE, width, height);MatrixToImageWriter.writeToPath(m, 'png', file.toPath());

如果內容較多,需要增大二維碼尺寸。尺寸小內容多,二維碼圖形越復雜越難識別。

生成帶Logo二維碼

// 二維碼內容String text = 'https://engr-z.com';// 二維碼大小int width = 500, height = 500;// 二維碼參數CodeStyle style = new CodeStyle();style.setWidth(width);style.setHeight(height);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);// 生成二維碼圖片QRCodeWriter writer = new QRCodeWriter();BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, style.getWidth(), style.getHeight(), hints);int margin = style.getMargin();int tempM = margin*2;int[] rec = bm.getEnclosingRectangle(); //獲取二維碼圖案的屬性int resWidth = rec[2] + tempM;int resHeight = rec[3] + tempM;BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定義邊框生成新的BitMatrixresMatrix.clear();for (int i = margin; i < resWidth - margin; i++) { //循環,將二維碼圖案繪制到新的bitMatrix中 for (int j = margin; j < resHeight - margin; j++){if (bm.get(i - margin + rec[0], j - margin + rec[1])){ resMatrix.set(i, j);} }}bm = resMatrix;int w = bm.getWidth();int h = bm.getHeight();BufferedImage qrcodeBuffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);// 開始利用二維碼數據創建Bitmap圖片for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) {qrcodeBuffImg.setRGB(x, y, bm.get(x, y) ? style.getCodeColor() : style.getBackgroundColor()); }}/** * 讀取Logo圖片 */File logoFile = new File('/home/engr-z/logo.png');BufferedImage logo = ImageIO.read(logoFile);/** * 設置logo的大小,設置為二維碼圖片的20% */int widthLogo = logo.getWidth(null) > qrcodeBuffImg.getWidth() * 3 / 10 ? (qrcodeBuffImg.getWidth() * 3 / 10): logo.getWidth(null),heightLogo = logo.getHeight(null) > qrcodeBuffImg.getHeight() * 3 / 10 ? (qrcodeBuffImg.getHeight() * 3 / 10) : logo.getWidth(null);/** * logo在二維碼的位置 */int x = (qrcodeBuffImg.getWidth() - widthLogo) / 2;int y = (qrcodeBuffImg.getHeight() - heightLogo) / 2;Graphics2D qrcodeOutg = qrcodeBuffImg.createGraphics();// 把logo寫到二維碼圖片中間qrcodeOutg.drawImage(logo, x, y, widthLogo, heightLogo, null);qrcodeOutg.dispose();qrcodeBuffImg.flush();// 新的圖片,把帶logo的二維碼下面加上文字String desc = 'https://engr-z.com';int textHeight = 26;int textMargin = 10;BufferedImage outImage = new BufferedImage(qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight() + textHeight + (textMargin * 2), BufferedImage.TYPE_4BYTE_ABGR);Graphics2D outg = outImage.createGraphics();// 畫二維碼到新的面板outg.drawImage(qrcodeBuffImg, 0, 0, qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight(), null);outg.setFont(new Font('宋體', Font.BOLD, 26)); // 字體、字型、字號int strWidth = outg.getFontMetrics().stringWidth(desc);outg.setColor(Color.BLACK);outg.drawString(desc, (outImage.getWidth() - strWidth) / 2, outImage.getHeight() - textMargin);outg.dispose();// 二維碼輸出文件File file = new File('/home/engr-z/qrcode.png');ImageIO.write(outImage, 'png', file);

CodeStyle是我封裝的類,用來設置二維碼樣式:

/** * @author Engr-Z * @since 2020/12/18 */@Datapublic class CodeStyle { /** * 背景顏色,如:0xFFFFFFFF */ private int backgroundColor = 0xFFFFFFFF; /** * 碼顏色,如:0xFF000000 */ private int codeColor = 0xFF000000; /** * 二維碼寬,px */ private int width; /** * 二維碼高,px */ private int height; /** * 邊框大小 */ private int margin = 5;}

以下是我執行生成的二維碼:

Java 生成帶Logo和文字的二維碼

讀取二維碼

File qrcodeFile = new File('D:/qrcode.png');BufferedImage qrcodeImg = ImageIO.read(qrcodeFile);MultiFormatReader formatReader = new MultiFormatReader();//讀取指定的二維碼文件BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(qrcodeImg)));//定義二維碼參數Map<DecodeHintType, Object> hints= new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET, 'utf-8');// 讀取Result result = formatReader.decode(binaryBitmap, hints);log.info('格式類型:{}', result.getBarcodeFormat());log.info('二維碼內容:{}', result.getText());鏈接

ZXing GitHub:https://github.com/zxing/zxing

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

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲精品国产男人的天堂 | 久草在在线视频 | 欧美一级在线免费观看 | 亚洲国产欧美一区二区欧美 | 老王午夜69精品影院 | 日韩精品久久久免费观看夜色 | 亚洲图片一区二区 | 国产片一级aaa毛片视频 | 性视频亚洲 | 国产91免费| 日韩美女免费视频 | 久草中文网| 99精品国产成人一区二区在线 | 99在线视频观看 | 91精品欧美综合在线观看 | 最新国产三级在线观看不卡 | a级片免费在线播放 | 在线观看视频中文字幕 | 久久精品国产免费一区 | 国语一级毛片 | 亚洲国产精品激情在线观看 | 亚洲第五色综合网啪啪 | 国产欧美一区二区成人影院 | 成人在线视频国产 | 国产成人精品三区 | 欧美成人免费观看的 | 国产成人18黄网站在线观看网站 | 久久青草视频 | 国产成人综合亚洲一区 | 干女人逼视频 | 日韩一区二区天海翼 | 18videosex性欧美69超高清 | 性感美女视频免费网站午夜 | 国产欧美在线视频 | 久草草视频在线观看免费高清 | 国产亚洲精品午夜高清影院 | 国产精品一区二区三区四区五区 | 曰韩美女一级视频 | 亚洲影院手机版777点击进入影院 | 特级做a爰片毛片免费看一区 | 久久一区二区三区免费播放 |