欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java二维码生成的方法

程序员文章站 2023-12-20 22:29:52
本文实例为大家分享了java二维码的实现代码,供大家参考,具体内容如下 这次用到的jar包是zxing,没有用到core的jar包 先导入zxing.jar包 生...

本文实例为大家分享了java二维码的实现代码,供大家参考,具体内容如下

这次用到的jar包是zxing,没有用到core的jar包
先导入zxing.jar包

生成二维码

package cn.huse.erweima;

import java.io.file;
import java.util.hashmap;

import com.google.zxing.barcodeformat;
import com.google.zxing.encodehinttype;
import com.google.zxing.multiformatwriter;
import com.google.zxing.client.j2se.matrixtoimagewriter;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.qrcode.decoder.errorcorrectionlevel;

/**
 * 生成二维码
 *
 */
public class createqrcode {

 public static void main(string[] args) {
 int width = 300;
 int height = 300;
 
 string format = "gif";
 string content = "www.baidu.com";
 
 //定义二维码的参数
 hashmap hints = new hashmap<>();
 hints.put(encodehinttype.character_set, "utf-8");
 hints.put(encodehinttype.error_correction, errorcorrectionlevel.m);
 try {
  bitmatrix matrix = new multiformatwriter().encode(content, barcodeformat.qr_code, width, height,hints);
  file file = new file("e:"+file.separator+"new.gif");
  matrixtoimagewriter.writetofile(matrix, format, file);
 } catch (exception e) {
  e.printstacktrace();
 }
 }

}


解析二维码

package cn.huse.erweima;

import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.util.hashmap;
import javax.imageio.imageio;
import com.google.zxing.binarybitmap;
import com.google.zxing.encodehinttype;
import com.google.zxing.multiformatreader;
import com.google.zxing.notfoundexception;
import com.google.zxing.result;
import com.google.zxing.client.j2se.bufferedimageluminancesource;
import com.google.zxing.common.hybridbinarizer;

//解析二维码
public class readqrcode {

 public static void main(string[] args) {
 
 multiformatreader formatreader = new multiformatreader();
 file file = new file("e:"+file.separator+"new.gif");
 try {
  bufferedimage image = imageio.read(file);
  binarybitmap binarybitmap = new binarybitmap(new hybridbinarizer(new bufferedimageluminancesource(image)));
  hashmap hints = new hashmap<>();
  hints.put(encodehinttype.character_set, "utf-8");
  
  result result = formatreader.decode(binarybitmap,hints);
  system.out.println(result.tostring());
  system.out.println(result.getbarcodeformat());
  system.out.println(result.gettext());
 } catch (notfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 }
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: