三种二维码生成方式
二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。
一、 二维码发展史
二、 具体实现三种方式
首先建目录结构:
1.使用qrCode方式
CreateQRCode代码:
package qrcode;import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class CreateQRCode {
public static void main(String[] args) throws Exception {
Qrcode x=new Qrcode();
x.setQrcodeErrorCorrect('H');//纠错等级
x.setQrcodeEncodeMode('B'); //N代表数字;A代表a—Z;B代表其他字符
x.setQrcodeVersion(7); //版本号(1-40)
String qrData="https://blog.csdn.net/BlogsDemo/article/details/80089313";
int width= 67 + 12 * (7-1);
int height= 67 + 12 * (7-1);
//依托javaGUI的画图工具实现的
BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D gs=bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);
int pixoff=2;//偏移量
byte[] d=qrData.getBytes("gb2312");
if(d.length>0 && d.length<120){
boolean[][] s=x.calQrcode(d);
for(int i=0;i<s.length;i++){
for(int j=0;j<s.length;j++){
if(s[i][j]){
gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("D:/img.png"));
}
}
MyQRCodeImage代码:
package qrcode;import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class MyQRCodeImage implements QRCodeImage {
BufferedImage bufferedImage;
public MyQRCodeImage(BufferedImage bufferedImage){
this.bufferedImage=bufferedImage;
}
@Override
public int getHeight() {
// TODO Auto-generated method stub
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
// TODO Auto-generated method stub
return bufferedImage.getRGB(arg0,arg1);
}
@Override
public int getWidth() {
// TODO Auto-generated method stub
return bufferedImage.getWidth();
}
}
ReadQRCode代码:
package qrcode;import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
public class ReadQRCode {
public static void main(String[] args) throws Exception {
File file=new File("E:/code/img.png");
BufferedImage bufferedImage=ImageIO.read(file);
QRCodeDecoder coderDecoder=new QRCodeDecoder();
String result=new String (coderDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");
System.out.println(result);
}
}
2.使用google的zxing生成
CreateQRCode代码:
package zxing;import java.io.File;
import java.nio.file.Path;
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;
//2-dimensional bar code
public class CreateQRCode {
public static void main(String[] args) {
int width=300; //设置二维码的宽
int height=300; //设置二维码的长
String format="png"; //设置二维码图片的格式
String contents="https://blog.csdn.net/BlogsDemo/article/details/80089313"; //设置的连接地址
//二维码参数
HashMap hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //设置支持中文编码
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //纠错等级。
hints.put(EncodeHintType.MARGIN, 2); //边框
try{
BitMatrix bitmatrix=new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height);
File files=new File("E:/code/img.png"); //文件
Path file=files.toPath(); //文件路径
MatrixToImageWriter.writeToPath(bitmatrix, format, file);
}catch(Exception e){
e.printStackTrace();
}
}
}
ReadQRCode代码:
package zxing;import java.awt.image.BufferedImage;
import java.io.File;
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.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class ReadQRCode {
public static void main(String[] args) {
try{
MultiFormatReader formatReader =new MultiFormatReader();
File file=new File("E:/code/img.png");
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"); //设置支持中文编码
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //纠错等级。
hints.put(EncodeHintType.MARGIN, 2);
Result result=formatReader.decode(binaryBitmap,hints);
System.out.println("解析结果"+result.toString());
System.out.println("二维码格式"+result.getBarcodeFormat());
System.out.println("二维码格式"+result.getText());
}catch(Exception e){
e.printStackTrace();
}
}
}
3.最简单的方式使用jQuery的qrCode插件
qrcode.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>生成二维码</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.qrcode.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-3.3.1.min.js"></script>
</head>
<body>
生成的二维码如下:<br>
<div id="qrcode"></div>
<script type="text/javascript">
jQuery("#qrcode").qrcode("https://blog.csdn.net/BlogsDemo/article/details/80089313");
</script>
</body>
</html>
下载链接:https://download.csdn.net/download/blogsdemo/10380721