一. 通过使用zxing方式实现:
jar准备: https://github.com/zxing/zxing 下载源代码,将core/src/main/Java/下的所有文件和javase/src/main/java/下的所有文件一起打成jar文件zxing.jar
创建二维码:
@SuppressWarnings({"rawtypes", "unchecked"})
private static void createZxing() throws WriterException, IOException {
int width=300;
int hight=300;
String format="png";
String content="www.baidu.com";
HashMap hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, hints);
Path file=new File("D:/download/imag.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
}
读取二维码:
private static void readZxing() throws IOException, NotFoundException {
MultiFormatReader read = new MultiFormatReader();
File file=new File("D:/download/imag.png");
BufferedImage image=ImageIO.read(file);
Binarizer binarizer=new HybridBinarizer(new BufferedImageLuminanceSource(image));
BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer);
Result res=read.decode(binaryBitmap);
System.out.println(res.toString());
System.out.println(res.getBarcodeFormat());
System.out.println(res.getText());
}
二. 使用QRCode方式实现二维码
jar包准备:Qrcode_A.jar,qrcode_B.jar
创建二维码:
private static void createORcode() throws UnsupportedEncodingException, IOException {
Qrcode qrcode=new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
int version = 7;
qrcode.setQrcodeVersion(version);
String qrData="www.baidu.com";
byte data[]=qrData.getBytes("gb2312");
int width = 67+12*(version-1);
int hight=67+12*(version-1);
BufferedImage bufferedImage=new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d=bufferedImage.createGraphics();
graphics2d.setBackground(Color.white);
graphics2d.setColor(Color.black);
graphics2d.clearRect(0, 0, width, hight);
int pixoff=2;
if (data.length>0 && data.length<120) {
boolean[][] s=qrcode.calQrcode(data);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if (s[j][i]) {
graphics2d.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
}
}
}
}
graphics2d.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("D:/download/imag.png"));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
读取二维码:两种方式:1.上诉readZxing()的方式,2.如下
private static void readQRcode() throws IOException, NotFoundException {
File file=new File("D:/download/imag.png");
BufferedImage bufferedImage=ImageIO.read(file);
QRCodeDecoder codeDecoder=new QRCodeDecoder();
String res=new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");
System.out.println(res);
}
三. 使用js插件实现二维码
js文件准备:jQuery.min.js,jquery.qrcode.min.js
导入js文件,然后编写如下代码,访问该页面。
<div id="qrcode"></div>
<script type="text/javascript">
$("#qrcode").qrcode("www.baidu.com");
</script>