1.zxing
1).生成二维码
需要引入zxing core,zxing javase extensions 两个jar包
//设置图片宽度,高度,格式,内容
int width=300;
int height=300;
String format="png";
String content="老哥稳";
content=new String(content.getBytes("utf-8"),"iso-8859-1");
HashMap hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//hints.put(EncodeHintType.MARGIN, 2);
try {
BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
//写入文件
Path file=new File("D:\\嘻嘻.png").toPath();
MatrixToImageWriter.writeToPath(encode, format, file);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2).解析二维码
BufferedImage bi=ImageIO.read(new File("D://嘻嘻.png"));
LuminanceSource ls=new BufferedImageLuminanceSource(bi);
BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(ls) );
HashMap hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatReader mfw=new MultiFormatReader();
Result result = mfw.decode(bb,hints);
System.out.println("二维码读取结果:"+result.toString());
2.jquery.qrcode 生成二维码
感觉这个还是比较方便的,后台传入前台一个字符串或者URL 然后生成一个二维码。方法呢很简单。引入jquery文件一样 引入 jquery.qrcode.min.js ,然后写入如下代码,就生成一个百度的二维码吧。
<div id="code"></div>
<script type="text/javascript" src="${basePath }/resource/js/jquery.min.js"></script>
<script type="text/javascript" src="${basePath }/resource/js/jquery.qrcode.min.js"></script>
<script type="text/javascript">
$(function(){
$("#code").qrcode("https://www.baidu.com");
});