首先:进入这个网址
github.com/zxing/。
将他复制成功后,就让他导出
就成这样啦
然后就新建个项目把刚才的包导入新建的项目
就像这样
接下来开始进行包的配置
、
这样大部分就弄完啦
接下来代码:
制作二维码
package com._520_make;
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.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class CreatCode {
public static void main(String[] args) {
/*
* 定义二维码的长,宽,高;
*/
int width = 300;
int height = 300;
String format = "png";
String content = "https://www.50s.cc/";
//定义二维码主要的参数
HashMap hints = new HashMap();
//用来设置字体的内容
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//用来设置纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//用来设置边距
hints.put(EncodeHintType.MARGIN, 2);
//生成二维码
try {
//运用MultiFormatWriter().encode()需要在后面加上hints
BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
//生成的二维码存放路径
Path file = new File("D:/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* 在这个最后需要抛出异常
*/
}
}
读取二维码
package com._520_make;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
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 ReadCode {
public static void main(String[] args) throws Exception {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("D:/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");
try {
Result result = formatReader.decode(binaryBitmap, hints);
System.out.println("解析结果:"+result);
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
另外如果要设置直接扫码就能进网址就把:
String content = “https://+网址”;
大致就这样拉。