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;
public class ZXingCreateQRCode {
public static void main(String[] args) {
int width=300;
int height =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);
//生成二维码
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height);
//设置路径
Path file = new File("E:/a.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);//输出图像
} catch ( Exception e) {
e.printStackTrace();
}
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
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;
public class ZXingReadQRCode {
public static void main(String[] args) {
try {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("E:/a.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");
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();
}
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import com.sun.prism.Image;
import com.swetake.util.Qrcode;
public class CreateQRCode {
public static void main(String[] args) throws Exception {
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect('M');// 纠错等级
x.setQrcodeEncodeMode('B');// N代表数字,A代表a-z,B代表其他字符
x.setQrcodeVersion(7);// 版本
String qrData = "www.baidu.com";
int width = 67 + 12 * (7 - 1);
int height = 67 + 12 * (7 - 1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D gs = bufferedImage.createGraphics();
gs.setColor(Color.BLACK);
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, width, height);
int pixoff = 1;// 偏移量
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[j][i]) {
gs.fillRect(j * 3+ pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("E:/b.png"));
}
}
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:/b.png");
BufferedImage bufferedImage = ImageIO.read(file);
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new MYQRCodeImage(bufferedImage)), "gb2312");
System.out.println(result);
}
}
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() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
return bufferedImage.getHeight();
}
}
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() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
return bufferedImage.getHeight();
}
}