java二维码生成的方法
更新时间:2017年06月07日 09:21:14 作者:ITqingliang
这篇文章主要为大家详细介绍了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 = "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();
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
maven的pom.xml中repositories和distributionManagement使用
这篇文章主要介绍了maven的pom.xml中repositories和distributionManagement使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-03-03
用intellij Idea加载eclipse的maven项目全流程(图文)
这篇文章主要介绍了用intellij Idea加载eclipse的maven项目全流程(图文),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12
Spring Boot中使用Activiti的方法教程(二)
工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,下面这篇文章主要给大家介绍了关于Spring Boot中使用Activiti的相关资料,需要的朋友可以参考下2018-08-08


最新评论