java二维码生成的方法

网友投稿 258 2023-05-09


java二维码生成的方法

本文实例为大家分享了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 = "baidu.com";

//定义二维码的参数

HashMap hints = new HashMap<>();

hints.put(EncodeHintType.CHARACTER_SET, "utf-8OwxFzaNFg");

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

try {

BitMatrix matrix = new MultiFormatWriter().encode(content, Barcodhttp://eFormat.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;

/http:///解析二维码

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();

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot 与 mybatis配置方法
下一篇:Java concurrency之AtomicLong原子类_动力节点Java学院整理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~