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

Java?Spring?boot實(shí)現(xiàn)生成二維碼

 更新時(shí)間:2022年02月09日 09:16:40   作者:weixin_43763430  
大家好,本篇文章主要講的是Java?Spring?boot實(shí)現(xiàn)生成二維碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下

一、引入spring boot依賴:

   <!--引入生成二維碼的依賴-->
   <!-- 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>

二、工具類代碼:

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

/**
 * 二維碼生成工具類
 */
public class QrCodeUtils {
    private static final String CHARSET = "utf-8";
    public static final String FORMAT = "JPG";
    // 二維碼尺寸
    private static final int QRCODE_SIZE = 300;
    // LOGO寬度
    private static final int LOGO_WIDTH = 60;
    // LOGO高度
    private static final int LOGO_HEIGHT = 60;

    /**
     * 生成二維碼
     *
     * @param content      二維碼內(nèi)容
     * @param logoPath     logo地址
     * @param needCompress 是否壓縮logo
     * @return 圖片
     * @throws Exception
     */
    public static BufferedImage createImage(String content, String logoPath, boolean needCompress) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
                hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        if (logoPath == null || "".equals(logoPath)) {
            return image;
        }
        // 插入圖片
        QrCodeUtils.insertImage(image, logoPath, needCompress);
        return image;
    }

    /**
     * 插入LOGO
     *
     * @param source       二維碼圖片
     * @param logoPath     LOGO圖片地址
     * @param needCompress 是否壓縮
     * @throws IOException
     */
    private static void insertImage(BufferedImage source, String logoPath,
                                    boolean needCompress) throws Exception {
        File file = new File(logoPath);
        if (!file.exists()) {
            System.err.println(""+logoPath+"   該文件不存在!");
            return;
        }
        Image src = ImageIO.read(new File(logoPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        if (needCompress) { // 壓縮LOGO
            if (width > LOGO_WIDTH) {
                width = LOGO_WIDTH;
            }
            if (height > LOGO_HEIGHT) {
                height = LOGO_HEIGHT;
            }
            Image image = src.getScaledInstance(width, height,
                    Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null); // 繪制縮小后的圖
            g.dispose();
            src = image;
        }
        // 插入LOGO
        Graphics2D graph = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

    /**
     * 生成二維碼(指定路徑保存)
     *
     * @param content 內(nèi)容
     * @param imgPath logo圖片地址(內(nèi)嵌圖片)
     * @param destPath 生成二維碼存放地址
     * @param needCompress 是否壓縮logo
     * @throws Exception
     */
    public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
        BufferedImage image = QrCodeUtils.createImage(content, imgPath, needCompress);
        mkdirs(destPath);
        // String file = new Random().nextInt(99999999)+".jpg";
        // ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
        ImageIO.write(image, FORMAT, new File(destPath));
    }

    /**
     * 生成二維碼(直接將二維碼以圖片輸出流返回)
     *
     * @param content 內(nèi)容
     * @param imgPath logo圖片地址(內(nèi)嵌圖片)
     * @param needCompress 是否壓縮logo
     * @return
     * @throws Exception
     */
    public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {
        BufferedImage image = QrCodeUtils.createImage(content, imgPath, needCompress);
        return image;
    }

    public static void mkdirs(String destPath) {
        File file = new File(destPath);
        // 當(dāng)文件夾不存在時(shí),mkdirs會(huì)自動(dòng)創(chuàng)建多層目錄,區(qū)別于mkdir.(mkdir如果父目錄不存在則會(huì)拋出異常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    /**
     * 生成二維碼(內(nèi)嵌LOGO)
     *
     * @param content      內(nèi)容
     * @param logoPath     LOGO地址
     * @param output       輸出流
     * @param needCompress 是否壓縮LOGO
     * @throws Exception
     */
    public static void encode(String content, String logoPath, OutputStream output, boolean needCompress)
            throws Exception {
        BufferedImage image = QrCodeUtils.createImage(content, logoPath, needCompress);
        ImageIO.write(image, FORMAT, output);
    }

    /**
     * 獲取指定文件的輸入流,獲取logo
     *
     * @param logoPath 文件的路徑
     * @return
     */
    public static InputStream getResourceAsStream(String logoPath) {
        return QrCodeUtils.class.getResourceAsStream(logoPath);
    }

    /**
     * 解析二維碼
     *
     * @param file
     *            二維碼圖片
     * @return
     * @throws Exception
     */
    public static String decode(File file) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        result = new MultiFormatReader().decode(bitmap, hints);
        String resultStr = result.getText();
        return resultStr;
    }

    /**
     * 解析二維碼
     *
     * @param path
     *            二維碼圖片地址
     * @return
     * @throws Exception
     */
    public static String decode(String path) throws Exception {
        return QrCodeUtils.decode(new File(path));
    }

	//測(cè)試一:
    public static void main(String[] args) throws Exception {
        String text = "https://blog.csdn.net/weixin_43763430";
        String logoPath = "D:\\qrCode\\logo.jpg";
        String destPath = "D:\\qrCode\\csdn.jpg";
        QrCodeUtils.encode(text,logoPath,destPath,true);
    }
}

三、調(diào)用工具類生成二維碼

1、將鏈接生成二維碼圖片并保存到指定路徑

工具類中的主方法是指定了二維碼鏈接的內(nèi)容是博客地址,并保存在D:\qrCode\csdn.jpg,二維碼嵌套了頭像的圖片,期望實(shí)現(xiàn)的是生成二維碼后被掃碼直接進(jìn)入到博客也沒。如若不嵌入頭像,直接將logoPath參數(shù)設(shè)為null。

	//測(cè)試一:
    public static void main(String[] args) throws Exception {
        String text = "https://blog.csdn.net/weixin_43763430";
        String logoPath = "D:\\qrCode\\logo.jpg";
        String destPath = "D:\\qrCode\\csdn.jpg";
        QrCodeUtils.encode(text,logoPath,destPath,true);
    }

運(yùn)行該主方法后,可在指定路徑中看到生成的二維碼圖片。

在這里插入圖片描述

2、將鏈接生成二維碼直接顯示在頁面

運(yùn)用spring boot生成二維碼無需將保存二維碼的圖片,只須前端調(diào)用springboot接口即可在頁面上顯示二維碼。實(shí)現(xiàn)了實(shí)時(shí)生成二維碼。Controller層接口代碼示例如下:

	@GetMapping("/anon/coupon/qrCodeTest")
    @ApiOperation(value = "獲取二維碼")
    public void qrCodeTest(HttpServletResponse response) throws Exception {
        String text = "https://blog.csdn.net/weixin_43763430";
        String logoPath = "D:\\qrCode\\logo.jpg";
        //String destPath = "D:\\qrCode\\csdn.jpg";
        QrCodeUtils.encode(text,logoPath,response.getOutputStream(),true);
    }

打開瀏覽器訪問該接口地址,頁面就會(huì)顯示生成的二維碼。掃描二維碼即可進(jìn)入到博客頁面。

在這里插入圖片描述

3、將以get請(qǐng)求傳參鏈接生成二維碼

二維碼運(yùn)用到各種業(yè)務(wù)中,通常需要根據(jù)不同用戶識(shí)別其相對(duì)應(yīng)的內(nèi)容,如以上示例是訪問的博客主頁面,如若想根據(jù)訪問者傳遞的參數(shù)訪問博客中特定的文章,文章訪問各篇文章是用的get請(qǐng)求方式,即可根據(jù)傳參實(shí)現(xiàn)get請(qǐng)求傳入不同參數(shù)生成二維碼的內(nèi)容不同。

	@GetMapping("/anon/coupon/qrCodeTest")
    @ApiOperation(value = "獲取二維碼")
    public void qrCodeTest(@RequestParam(value = "id") String id,HttpServletResponse response) throws Exception {
        String text = "https://blog.csdn.net/weixin_43763430/article/details/" + id;
        String logoPath = "D:\\qrCode\\logo.jpg";
        //String destPath = "D:\\qrCode\\csdn.jpg";
        QrCodeUtils.encode(text,logoPath,response.getOutputStream(),true);
    }

用瀏覽器訪問該接口地址,頁面生成二維碼,用手機(jī)掃描二維碼即可跳轉(zhuǎn)到博客中該篇文章頁面。

在這里插入圖片描述

總結(jié)

到此這篇關(guān)于Java Spring boot實(shí)現(xiàn)生成二維碼的文章就介紹到這了,更多相關(guān)Java Spring boot生成二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot零基礎(chǔ)入門之基本操作與概念

    SpringBoot零基礎(chǔ)入門之基本操作與概念

    這篇文章主要介紹了SpringBoot的概念、創(chuàng)建和運(yùn)行,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • springboot Actuator的指標(biāo)監(jiān)控可視化功能詳解

    springboot Actuator的指標(biāo)監(jiān)控可視化功能詳解

    SpringBoot Actuator是springboot為簡(jiǎn)化我們對(duì)微服務(wù)項(xiàng)目的監(jiān)控功能抽取出來的模塊,使得我們每個(gè)微服務(wù)快速引用即可獲得生產(chǎn)界別的應(yīng)用監(jiān)控、審計(jì)等功能。這篇文章主要介紹了springboot Actuator的指標(biāo)監(jiān)控可視化,需要的朋友可以參考下
    2021-08-08
  • springcloud?feign?接口指定接口服務(wù)ip方式

    springcloud?feign?接口指定接口服務(wù)ip方式

    這篇文章主要介紹了springcloud?feign?接口指定接口服務(wù)ip方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java 線程同步詳解

    Java 線程同步詳解

    這篇文章主要給大家詳細(xì)介紹的是Java 線程同步的相關(guān)問題及代碼示例,有需要的小伙伴可以參考下
    2016-03-03
  • 使用Spring開啟@Async異步方式(javaconfig配置)

    使用Spring開啟@Async異步方式(javaconfig配置)

    這篇文章主要介紹了使用Spring開啟@Async異步方式(javaconfig配置),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java實(shí)戰(zhàn)之在線寄查快遞系統(tǒng)的實(shí)現(xiàn)

    Java實(shí)戰(zhàn)之在線寄查快遞系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要介紹了如何利用Java制作一個(gè)在線寄查快遞系統(tǒng),文中采用的技術(shù)有java、SpringBoot、FreeMarker、Mysql,需要的可以參考一下
    2022-02-02
  • 怎么在AVD上安裝apk軟件

    怎么在AVD上安裝apk軟件

    執(zhí)行 Windows 開始菜單 => 所有程序 => 附件 => 命令提示符 或通過 Win+R 組合鍵調(diào)出 運(yùn)行 對(duì)話框輸入cmd 單擊確定即可
    2013-09-09
  • Hadoop多Job并行處理的實(shí)例詳解

    Hadoop多Job并行處理的實(shí)例詳解

    這篇文章主要介紹了Hadoop多Job并行處理的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • 解決springboot上傳圖片后無法立即訪問需重啟的問題

    解決springboot上傳圖片后無法立即訪問需重啟的問題

    這篇文章主要介紹了解決springboot上傳圖片后無法立即訪問需重啟的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 如何處理后臺(tái)向前臺(tái)傳遞的json數(shù)據(jù)

    如何處理后臺(tái)向前臺(tái)傳遞的json數(shù)據(jù)

    這篇文章主要介紹了如何處理后臺(tái)向前臺(tái)傳遞的json數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論

县级市| 垣曲县| 玉屏| 乌兰察布市| 恩施市| 遂川县| 揭阳市| 商都县| 蒙自县| 昭通市| 新巴尔虎左旗| 含山县| 诸城市| 鹿泉市| 安顺市| 昌平区| 梨树县| 出国| 新龙县| 德令哈市| 湖北省| 化州市| 廊坊市| 洪雅县| 永嘉县| 瑞昌市| 普安县| 中宁县| 万荣县| 金乡县| 福泉市| 武强县| 宁夏| 饶阳县| 瑞安市| 米易县| 江川县| 高尔夫| 米泉市| 射阳县| 明光市|