激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作示例

Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作示例

2021-05-17 14:22Rest探路者 Java教程

這篇文章主要介紹了Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作,結(jié)合實(shí)例形式分析了Java創(chuàng)建與解析二維碼,以及文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了java實(shí)現(xiàn)的生成二維碼和解析二維碼url操作。分享給大家供大家參考,具體如下:

二維碼依賴jar包,zxing

?
1
2
3
4
5
6
<!-- 二維碼依賴 start -->
<dependency>
  <groupid>com.google.zxing</groupid>
  <artifactid>javase</artifactid>
  <version>3.0.0</version>
</dependency>

createqrcode生成二維碼,anlysisqrcode解析二維碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.xgt.util;
import com.google.zxing.*;
import com.google.zxing.client.j2se.bufferedimageluminancesource;
import com.google.zxing.client.j2se.matrixtoimagewriter;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.common.hybridbinarizer;
import com.google.zxing.qrcode.decoder.errorcorrectionlevel;
import org.apache.commons.io.ioutils;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import sun.misc.base64encoder;
import javax.imageio.imageio;
import java.awt.image.bufferedimage;
import java.io.*;
import java.nio.file.path;
import java.util.hashtable;
public class qrcodeutil {
  private static final logger logger = loggerfactory.getlogger(qrcodeutil.class);
  /**
   * 創(chuàng)建二維碼
   * @param url
   * @param filename
   * @return
   * @throws ioexception
   */
  public static string createqrcode(string url,string filedirectory,string filename) throws ioexception {
    int width = 500;
    int height = 500;
    string format = "png";
    hashtable hints = new hashtable();
    hints.put(encodehinttype.character_set, "utf-8");
    hints.put(encodehinttype.error_correction, errorcorrectionlevel.m);
    hints.put(encodehinttype.margin, 2);
    try {
      bitmatrix bitmatrix = new multiformatwriter().encode(url, barcodeformat.qr_code, width, height, hints);
      file filedir = new file(filedirectory);
      filetoolutil.judedirexists(filedir);
      path file = new file(filedirectory,filename+".png").topath();//在d盤生成二維碼圖片
      matrixtoimagewriter.writetopath(bitmatrix, format, file);
      bufferedimage image = matrixtoimagewriter.tobufferedimage(bitmatrix);
      bytearrayoutputstream os = new bytearrayoutputstream();//新建流。
      imageio.write(image, format, os);//利用imageio類提供的write方法,將bi以png圖片的數(shù)據(jù)模式寫入流。
      byte b[] = os.tobytearray();//從流中獲取數(shù)據(jù)數(shù)組。
      string str = new base64encoder().encode(b);
      ioutils.closequietly(os);
      return str;
    } catch (exception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }finally {
      //deletefileutil.delete(filedirectory);
    }
    return "null";
  }
  /**
   * 解析出二維碼的url
   * 無用
   * @param file
   * @param filedirectory
   * @throws notfoundexception
   */
  public static void anlaysisqrcode(file file,string filedirectory) throws notfoundexception {
    multiformatreader formatreader=new multiformatreader();
    bufferedimage image=null;
    try {
      image = imageio.read(file);
    binarybitmap binarybitmap =new binarybitmap(new hybridbinarizer(new bufferedimageluminancesource(image)));
    hashtable hints=new hashtable();
    hints.put(encodehinttype.character_set, "utf-8");
    result result=formatreader.decode(binarybitmap,hints);
    logger.info("解析結(jié)果:"+result.tostring());
    logger.info("解析格式:"+result.getbarcodeformat());
    } catch (ioexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }finally {
      deletefileutil.delete(filedirectory);
    }
  }
}

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

原文鏈接:http://www.cnblogs.com/Java-Starter/p/9283513.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本高清无遮挡 | 国产精品免费av一区二区三区 | 免费一级在线视频 | av在线播放电影 | 国内精品久久久久久久星辰影视 | 亚洲乱妇19p| 污片在线观看视频 | 一级裸体视频 | 成人性生活视频在线观看 | 91精品国产日韩91久久久久久360 | 激情久久婷婷 | 亚洲va久久久噜噜噜久久男同 | 亚洲午夜免费 | 91美女啪啪 | 欧美一级电影网站 | 成av人在线观看 | 国产精品视频自拍 | 国产精品99精品 | 国产资源在线免费观看 | 国产亚洲在 | 4p嗯啊巨肉寝室调教男男视频 | 日韩视频二区 | 91成人一区二区三区 | 牛牛视频在线 | 色999中文字幕 | 国产精品区一区二区三区 | 欧美日比视频 | 久久99精品久久久久久园产越南 | 国产一国产一级毛片视频在线 | 日本在线高清 | 欧美一区中文字幕 | 自拍偷拍999| 久久精品亚洲欧美日韩精品中文字幕 | 中文字幕在线播放不卡 | 国产精品麻豆一区二区三区 | 免费观看国产精品视频 | 未成年人在线观看 | 一区二区三区欧洲 | 成人午夜视频在线观看免费 | 偿还电影免费看 | 黄色aaa视频|