您的位置:首页 > 编程语言 > Java开发

Java实现二维码制作

2017-08-06 22:41 169 查看

二维码概述

二维码又称QR Code,QR全称Quick Response,是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的。

zxing

1、引入pom文件

<!-- zxing -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>


2、生成和读取二维码

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

public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "png";
String content = "www.shuai.com";
//定义二维码的参数
HashMap map = new HashMap();
//设置编码
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
//设置纠错等级
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
map.put(EncodeHintType.MARGIN, 2);

try {
//生成二维码
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
Path file = new File("E:/develop/qrcode.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 读取二维码
*/
public class ReadQrCode {

public static void main(String[] args) {
try {
MultiFormatReader multiFormatReader = new MultiFormatReader();
File file = new File("E:/develop/qrcode.png");
BufferedImage image = ImageIO.read(file);
//定义二维码参数
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");

//获取读取二维码结果
BinaryBitmap bin
4000
aryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Result result = multiFormatReader.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();
}
}
}


qrcodejar

1、引入qrcode的jar

2、生成二维码,解析二维码

/**
* 二维码图片
*/
public class MyQrCodeImage implements QRCodeImage{
BufferedImage bufferedImage;

public MyQrCodeImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}

public int getHeight() {
return bufferedImage.getHeight();
}

public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}

public int getWidth() {
return bufferedImage.getWidth();
}

}

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

public static void main(String[] args) throws IOException {
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect('M');//设置纠错等级
x.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其它字符
x.setQrcodeVersion(7);//设置版本
String qrData = "www.shuai.com";
int width = 67 + 12 * (7 - 1);
int height = 67 + 12 * (7 - 1);
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("gbk");
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[j][i]) {
//往画板中填充内容
gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("E:/develop/code.png"));
}
}

/**
* 读取二维码
*/
public class ReadQrCode {

public static void main(String[] args) throws IOException {
File file = new File("E:/develop/code.png");
BufferedImage bufferedImage = ImageIO.read(file);
Qrcode qrcode = new Qrcode();
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new MyQrCodeImage(bufferedImage)),"GBK");
System.out.println(result);
}
}


jquery.qrcode.js

1、引入jquery.min.js、jquery.qrcode.min.js

2、页面

<%@ 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>
<%
String path = request.getContextPath();
String bathPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
pageContext.setAttribute("path", path);
pageContext.setAttribute("bathPath", bathPath);
%>
<script type="text/javascript" src="${path }/js/jquery.min.js"></script>
<script type="text/javascript" src="${path }/js/jquery.qrcode.min.js"></script>
</head>
<body>
生成的二维码: <br/>
<div id="qrcode"></div>
<script type="text/javascript">
$('#qrcode').qrcode("www.shuai.cn");
</script>
</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: