最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java生成二維碼的實(shí)例代碼

 更新時(shí)間:2020年09月09日 10:04:59   作者:崔笑顏  
這篇文章主要介紹了Java生成二維碼的實(shí)例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下

使用開(kāi)源的一維/二維碼圖形處理庫(kù)zxing GitHub地址

引入依賴(lài)

<!-- 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>

封裝工具類(lèi)

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 生成二維碼工具類(lèi)
 * @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 二維碼寬度,默認(rèn)為200
   * @param height 二維碼高度,默認(rèn)為200
   * @param content 二維碼內(nèi)容,必填
   * @param logoPath logo圖片路徑,若為空則生成不帶logo的二維碼
   * @param imgPath 生成二維碼文件夾路徑
   * @param imgName 生成二維碼圖片名稱(chēng),必填
   * @param suffix 生成二維碼圖片后綴類(lèi)型,例如: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<>();
    // 內(nèi)容編碼格式
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 指定糾錯(cuò)等級(jí)
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    // 設(shè)置二維碼邊的空度,非負(fù)數(shù)
    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<>();
    // 內(nèi)容編碼格式
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 指定糾錯(cuò)等級(jí)
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    // 設(shè)置二維碼邊的空度,非負(fù)數(shù)
    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
  {
    // 讀取二維碼圖片,并構(gòu)建繪圖對(duì)象
    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;
  }
}

測(cè)試生成二維碼

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

以上就是Java生成二維碼的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java生成二維碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • springboot如何添加task任務(wù)執(zhí)行隊(duì)列

    springboot如何添加task任務(wù)執(zhí)行隊(duì)列

    這篇文章主要介紹了springboot如何添加task任務(wù)執(zhí)行隊(duì)列問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SpringBoot通過(guò)@MatrixVariable進(jìn)行傳參詳解

    SpringBoot通過(guò)@MatrixVariable進(jìn)行傳參詳解

    這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • Java關(guān)于IO流的全面介紹

    Java關(guān)于IO流的全面介紹

    下面小編就為大家?guī)?lái)一篇Java關(guān)于IO流的全面介紹。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-08-08
  • Java調(diào)用DeepSeek?api實(shí)現(xiàn)方法記錄

    Java調(diào)用DeepSeek?api實(shí)現(xiàn)方法記錄

    這篇文章主要介紹了如何在Java中調(diào)用DeepSeek?API,包括在官網(wǎng)獲取APIKeys、創(chuàng)建API請(qǐng)求工具類(lèi)以及處理返回結(jié)果的步驟,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • 深入了解Java GC的工作原理

    深入了解Java GC的工作原理

    下面小編就為大家?guī)?lái)一篇深入了解Java GC的工作原理。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • Java利用AQS實(shí)現(xiàn)自定義鎖

    Java利用AQS實(shí)現(xiàn)自定義鎖

    本文主要介紹了Java利用AQS實(shí)現(xiàn)自定義鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Spring Boot+Nginx實(shí)現(xiàn)大文件下載功能

    Spring Boot+Nginx實(shí)現(xiàn)大文件下載功能

    相信很多小伙伴,在日常開(kāi)放中都會(huì)遇到大文件下載的情況,大文件下載方式也有很多,比如非常流行的分片下載、斷點(diǎn)下載;當(dāng)然也可以結(jié)合Nginx來(lái)實(shí)現(xiàn)大文件下載,在中小項(xiàng)目非常適合使用,這篇文章主要介紹了Spring Boot結(jié)合Nginx實(shí)現(xiàn)大文件下載,需要的朋友可以參考下
    2024-05-05
  • Java?Date(日期)對(duì)象進(jìn)行格式化的思路詳解

    Java?Date(日期)對(duì)象進(jìn)行格式化的思路詳解

    Date類(lèi)是經(jīng)常會(huì)使用到的一個(gè)用來(lái)處理日期、時(shí)間的一個(gè)類(lèi)。Date類(lèi)是在java.util包下的Date類(lèi),這篇文章主要介紹了Java?Date(日期)對(duì)象如何進(jìn)行格式化呢,需要的朋友可以參考下
    2022-09-09
  • Java并發(fā)的CAS原理與ABA問(wèn)題的講解

    Java并發(fā)的CAS原理與ABA問(wèn)題的講解

    今天小編就為大家分享一篇關(guān)于Java并發(fā)的CAS原理與ABA問(wèn)題的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 基于紅黑樹(shù)插入操作原理及java實(shí)現(xiàn)方法(分享)

    基于紅黑樹(shù)插入操作原理及java實(shí)現(xiàn)方法(分享)

    下面小編就為大家分享一篇基于紅黑樹(shù)插入操作原理及java實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論

同江市| 镇沅| 姜堰市| 达尔| 彭水| 汝阳县| 康乐县| 宜川县| 聂荣县| 闽侯县| 自治县| 禄劝| 赫章县| 建宁县| 枣强县| 甘德县| 吉首市| 东港市| 大渡口区| 科技| 哈巴河县| 永德县| 双牌县| 疏附县| 神农架林区| 盘锦市| 蒙自县| 昆山市| 吴川市| 巴林右旗| 延川县| 上思县| 乌拉特中旗| 沿河| 鄂尔多斯市| 历史| 宝应县| 玛纳斯县| 慈利县| 丘北县| 中西区|