方法一:ZXing
使用ZXing识别,可以返回二维码识别内容,还可以找到二维码的位置坐标。
1、引入依赖
implementation group: 'com.google.zxing', name: 'core', version: '3.5.3'
implementation group: 'com.google.zxing', name: 'javase', version: '3.5.3'
2、识别代码
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhengqian
* @since 2024.12.26
*/
public class ZXingQRCode {
public static void main(String[] args) {
String urlPath = "https://url";
zxing(urlPath);
}
public static void zxing(String urlPath) {
try {
// 读取二维码图片文件
// File file = new File("二维码图片1.png");
// BufferedImage image = ImageIO.read(file);
URL url = new URL(urlPath);
BufferedImage image = ImageIO.read(url);
// 使用LuminanceSource和Binarizer将图片转化为二进制数据
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// 创建一个解码器
Result qrCodeResult = new MultiFormatReader().decode(bitmap);
// 输出解码结果
System.out.println("二维码内容: " + qrCodeResult.getText());
// 解码
ResultPoint[] resultPoint = qrCodeResult.getResultPoints();
System.out.println("二维码坐标:\npoints1: " + resultPoint[0] + ",\npoints2: " + resultPoint[1] + ",\npoints3: "
+ resultPoint[2] + ",\npoints4: " + resultPoint[3]);
// 获得二维码坐标
float point1X = resultPoint[0].getX();
float point1Y = resultPoint[0].getY();
float point2X = resultPoint[1].getX();
float point2Y = resultPoint[1].getY();
// 宽高
final int w = (int) Math
.sqrt(Math.abs(point1X - point2X) * Math.abs(point1X - point2X) + Math.abs(point1Y - point2Y) * Math.abs(point1Y - point2Y))
+ 12 * (7 - 1);
final int h = w;
System.out.println("二维码宽高:" + h);
} catch (NotFoundException e) {
System.out.println("二维码未找到");
// 这里判断如果识别不了带LOGO的图片,重新添加上一个属性
try {
URL url = new URL(urlPath);
BufferedImage image = ImageIO.read(url);
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<>(3);
// 设置编码格式
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
// 设置优化精度
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
// 设置复杂模式开启
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TYPE);
// 解码
Result result = new MultiFormatReader().decode(binaryBitmap, hints);
System.out.println("图片中内容: ");
System.out.println("content: " + result.getText());
} catch (IOException e1) {
e1.printStackTrace();
} catch (NotFoundException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
System.out.println("二维码读取错误");
e.printStackTrace();
}
}
}
方法二 使用opencv
1、引入依赖
// opencv
implementation group: 'org.bytedeco', name: 'opencv', version: '4.10.0-1.5.11'
implementation group: 'org.bytedeco', name: 'javacpp', version: '1.5.11'
implementation group: 'org.bytedeco', name: 'openblas', version: '0.3.28-1.5.11'
implementation group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.11'
2、识别代码
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.StringVector;
import org.bytedeco.opencv.opencv_wechat_qrcode.WeChatQRCode;
import java.nio.charset.StandardCharsets;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
/**
* @author zhengqian
* @since 2024.12.26
*/
public class OpenCVQRCode {
public static void main(String[] args) {
String filePath = "二维码图片1.png";
wechat(filePath);
}
public static void wechat(String filePath) {
Mat img = imread(filePath);
WeChatQRCode we = new WeChatQRCode();
StringVector stringVector = we.detectAndDecode(img);
if (stringVector.empty()) {
System.out.println("not found");
}
System.out.println("识别内容:" + stringVector.get(0).getString(StandardCharsets.UTF_8));
// 不过暂时不太清楚怎么输出二维码坐标。。。
}
}
标签:Java,resultPoint,opencv,二维码,new,println,import,识别
From: https://www.cnblogs.com/ylty/p/18633946