服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现的生成二维码和解析二维码URL操作示例

Java实现的生成二维码和解析二维码URL操作示例

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

这篇文章主要介绍了Java实现的生成二维码和解析二维码URL操作,结合实例形式分析了Java创建与解析二维码,以及文件读写等相关操作技巧,需要的朋友可以参考下

本文实例讲述了java实现的生成二维码和解析二维码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);
  /**
   * 创建二维码
   * @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图片的数据模式写入流。
      byte b[] = os.tobytearray();//从流中获取数据数组。
      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("解析结果:"+result.tostring());
    logger.info("解析格式:"+result.getbarcodeformat());
    } catch (ioexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }finally {
      deletefileutil.delete(filedirectory);
    }
  }
}

希望本文所述对大家java程序设计有所帮助。

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

延伸 · 阅读

精彩推荐