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

基于Java實(shí)現(xiàn)二維碼的生成和解析

 更新時(shí)間:2022年08月22日 08:38:36   作者:java奧斯卡  
二維碼其實(shí)就是一種編碼技術(shù),只是這種編碼技術(shù)是用在圖片上了,將給定的一些文字,數(shù)字轉(zhuǎn)換為一張經(jīng)過特定編碼的圖片。本文將利用Java實(shí)現(xiàn)二維碼的生成和解析,需要的可以參考一下

最近因個(gè)人需求需要對(duì)根據(jù)內(nèi)容生成二維碼和進(jìn)行解析!記錄一下!二維碼其實(shí)就是一種編碼技術(shù),只是這種編碼技術(shù)是用在圖片上了,將給定的一些文字,數(shù)字轉(zhuǎn)換為一張經(jīng)過特定編碼的圖片。這里利用的是 google 公司的 zxing使用方便,可以操作條形碼或者二維碼等

導(dǎo)入相關(guān)jar包

<!-- 二維碼 -->
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>core</artifactId>
			<version>3.3.0</version>
		</dependency>
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>javase</artifactId>
			<version>3.3.3</version>
		</dependency>

二維碼工具類編

 創(chuàng)建二維碼圖片

public static BufferedImage createImage(String charSet, String content, int qrWidth, int qrHeight) {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, charSet);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, // 修改二維碼底部高度
                    hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        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);
            }
        }
        return image;
    }

二維碼設(shè)置logo

public static void insertLogoImage(BufferedImage source, Image logo, int logoWidth, int logoHeight) {
        Graphics2D graph = source.createGraphics();
        int qrWidth = source.getWidth();
        int qrHeight = source.getHeight();
        int x = (qrWidth - logoWidth) / 2;
        int y = (qrHeight - logoHeight) / 2;
        graph.drawImage(logo, x, y, logoWidth, logoHeight, null);
        Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

將文明說明增加到二維碼上

 public static BufferedImage textToImage(String str, int width, int height, int fontSize) {
        BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) textImage.getGraphics();
        //開啟文字抗鋸齒
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, width, height);
        g2.setPaint(Color.BLACK);
        FontRenderContext context = g2.getFontRenderContext();
        Font font = new Font("微軟雅黑", Font.PLAIN, fontSize);
        g2.setFont(font);
        LineMetrics lineMetrics = font.getLineMetrics(str, context);
        FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
        float offset = (width - fontMetrics.stringWidth(str)) / 2;
        float y = (height + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
        g2.drawString(str, (int) offset, (int) y);
        return textImage;
    }

解析二維碼

 /*
     * 解析二維碼
     */
    public static String decode(File file, DecodeHintType cherSet) 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 hints = new Hashtable();
        hints.put(DecodeHintType.CHARACTER_SET, cherSet);
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

        result = new MultiFormatReader().decode(bitmap, hints);
        String resultStr = result.getText();
        return resultStr;
    }

main方法測(cè)試類

  public static void main(String[] args) {
        String content="用戶攝影作品版權(quán)信息";
        BufferedImage image = QRCodeUtil.createImage( "utf-8", content, 400, 400 );
        QRCodeUtil.addUpFont( image,"用戶攝影作品版權(quán)信息" );
        String formatName="png";
        String imagePath="D:\temp\java二維碼.png";
        File file=new File(imagePath);
        try {
            ImageIO.write(image, formatName, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String decode = null;
        try {
            decode = QRCodeUtil.decode(file, DecodeHintType.CHARACTER_SET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(decode);
        System.out.println("執(zhí)行完成");

    }

這樣就實(shí)現(xiàn)了 !二維碼里面的內(nèi)容根據(jù)實(shí)際數(shù)據(jù)來就行!用手機(jī)掃描后查看。

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

相關(guān)文章

  • MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟

    MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟

    這篇文章主要介紹了MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟,這次使用的是LambdaQueryWrapper,使用QueryWrapper相對(duì)來說簡(jiǎn)單點(diǎn)就不寫了,本文分步驟給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2023-12-12
  • JavaScript中的closest方法示例詳解

    JavaScript中的closest方法示例詳解

    這篇文章主要介紹了JavaScript中closest方法的相關(guān)資料,closest()是JavaScript中的一個(gè)非常實(shí)用的?DOM?方法,用于查找與當(dāng)前元素匹配的最近的祖先元素,它支持?CSS?選擇器,可以應(yīng)用于事件委托、動(dòng)態(tài)內(nèi)容處理等場(chǎng)景,需要的朋友可以參考下
    2025-02-02
  • Java中的TreeMap底層源碼分析

    Java中的TreeMap底層源碼分析

    這篇文章主要介紹了Java中的TreeMap底層源碼分析,TreeMap與Hashmap、LinkedHashMap不同,他的底層不再是數(shù)組,而是一顆紅黑樹,在插入、刪除或者替換元素時(shí),TreeMap能按照事先約定的順序來對(duì)key進(jìn)行排序和迭代查詢,需要的朋友可以參考下
    2023-12-12
  • javaweb Servlet開發(fā)總結(jié)(一)

    javaweb Servlet開發(fā)總結(jié)(一)

    Servlet是sun公司提供的一門用于開發(fā)動(dòng)態(tài)web資源的技術(shù)。這篇文章主要介紹了javaweb Servlet開發(fā)的第一篇,感興趣的小伙伴們可以參考一下
    2016-05-05
  • SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn)

    SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn)

    本文主要介紹了SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次

    SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次

    本文主要介紹了SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • 詳解Java中使用泛型實(shí)現(xiàn)快速排序算法的方法

    詳解Java中使用泛型實(shí)現(xiàn)快速排序算法的方法

    這篇文章主要介紹了Java中使用泛型實(shí)現(xiàn)快速排序算法的方法,快速排序的平均時(shí)間復(fù)雜度為(n\log n),文中的方法立足于基礎(chǔ)而并沒有考慮優(yōu)化處理,需要的朋友可以參考下
    2016-05-05
  • java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法

    java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法

    這篇文章主要介紹了java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法的相關(guān)資料,在開發(fā)過程中經(jīng)常會(huì)用到String 這個(gè)類進(jìn)行操作,需要的朋友可以參考下
    2017-08-08
  • SpringBoot3 Spring WebFlux簡(jiǎn)介(推薦)

    SpringBoot3 Spring WebFlux簡(jiǎn)介(推薦)

    SpringWebFlux是Spring Framework 5中引入的響應(yīng)式Web框架,用于支持非阻塞異步通信和響應(yīng)式流處理,與傳統(tǒng)的SpringMVC相比,WebFlux提供了完全異步非阻塞的編程模型,適用高并發(fā)、微服務(wù)架構(gòu)和實(shí)時(shí)數(shù)據(jù)流,本文介紹SpringBoot3 Spring WebFlux簡(jiǎn)介,感興趣的朋友一起看看吧
    2024-10-10
  • java el簡(jiǎn)介及用法

    java el簡(jiǎn)介及用法

    EL簡(jiǎn)介語法結(jié)構(gòu) 運(yùn)算符等資料代碼。
    2009-04-04

最新評(píng)論

德安县| 垦利县| 邵东县| 临夏市| 阿拉善右旗| 滦南县| 金寨县| 汕尾市| 富平县| 华池县| 泸西县| 延川县| 揭东县| 遵义县| 赤水市| 新安县| 建始县| 菏泽市| 蒙阴县| 财经| 襄樊市| 唐山市| 夏河县| 定边县| 宁德市| 和硕县| 普洱| 仁怀市| 柳河县| 铜梁县| 吉首市| 望江县| 湟中县| 南华县| 星座| 板桥市| 怀集县| 同心县| 巴楚县| 商都县| 南华县|