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

时间:2021-07-16 23:39:06

本文实例讲述了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