Java 在图像中找到二维码并使用Zxing解码
首先,我通读了所有关于如何在Java中使用Zxing的主题,但总是在缺少com.google.Zxing.client.j2se时出错。*(我在eclipse中加载了Zxing core-3.2.1.jar,所有其他Zxing包都可以工作,除非j2se),或者只是找到了创建qr图像的解决方案 我的目标是编写一个获取图像文件的方法,在该图像中找到qr码,解码qr码并返回字符串,基本上应该如下所示:Java 在图像中找到二维码并使用Zxing解码,java,qr-code,zxing,Java,Qr Code,Zxing,首先,我通读了所有关于如何在Java中使用Zxing的主题,但总是在缺少com.google.Zxing.client.j2se时出错。*(我在eclipse中加载了Zxing core-3.2.1.jar,所有其他Zxing包都可以工作,除非j2se),或者只是找到了创建qr图像的解决方案 我的目标是编写一个获取图像文件的方法,在该图像中找到qr码,解码qr码并返回字符串,基本上应该如下所示: import com.google.zxing.*; public class QRCode {
import com.google.zxing.*;
public class QRCode {
/*
* ...
*/
public String getDecodedString(SomeStandardImageType photo){
// detect the qr code in a photo
// create qr image from detected area in photo
// decode the new created qr image and return the string
return "This is the decoded dataString from the qr code in the photo";
}
}
总之,该方法应该得到如下所示的图像文件
并应返回url,如果失败,则仅返回“”
代码应与Zxing 3.2.1兼容
编辑:问题已解决。对于其他对此感兴趣的人,我想说,将外部jar
core-3.2.1.jar
和javase-3.2.1.jar
添加到外部jar中是很重要的。我的答案在没有后者的情况下有效,但取决于android映像库 这段代码对我来说很好用。希望它有助于导入必要的包,它应该可以工作
public class QR_Reader extends JFrame implements Runnable, ThreadFactory {
private static final long serialVersionUID = 6441489157408381878L;
private Executor executor = Executors.newSingleThreadExecutor(this);
private Webcam webcam = null;
private WebcamPanel panel = null;
String s;
public QR_Reader() {
super();
setLayout(new FlowLayout());
setTitle("Reading QR Code");
Dimension size = WebcamResolution.QVGA.getSize();
webcam = Webcam.getWebcams().get(0);
webcam.setViewSize(size);
panel = new WebcamPanel(webcam);
panel.setPreferredSize(size);
add(panel);
pack();
setVisible(true);
setResizable(false);
executor.execute(this);
}
@Override
public void run() {
do {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Result result = null;
BufferedImage image = null;
if (webcam.isOpen()) {
if ((image = webcam.getImage()) == null) {
continue;
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
result = new MultiFormatReader().decode(bitmap);
} catch (NotFoundException e) {
// fall thru, it means there is no QR code in image
}
}
if (result != null) {
String time_then=result.getText(); //this is the text extracted from QR CODE
webcam.close();
this.setVisible(false);
this.dispose();
try {
new Compare().C_Main(time_then);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} while (true);
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "example-runner");
t.setDaemon(true);
return t;
}
void QRC_Main() {
new QR_Reader();
}
}
我现在更深入地阅读了Zxing,下面的代码将与zxingv3.2.1一起使用(此代码不需要
javase
lib)
以下是创建二维码并从二维码中读取信息的代码
package com.attendance.mark;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCode {
/**
*
* @param args
* @throws WriterException
* @throws IOException
* @throws NotFoundException
*/
public static void main(String[] args) throws WriterException, IOException,
NotFoundException {
String qrCodeData = "student3232_2015_12_15_10_29_46_123";
String filePath = "F:\\Opulent_ProjectsDirectory_2015-2016\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\AttendanceUsingQRCode\\QRCodes\\student3232_2015_12_15_10_29_46_123";
String charset = "UTF-8"; // or "ISO-8859-1"
Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
System.out.println("QR Code image created successfully!");
System.out.println("Data read from QR Code: "
+ readQRCode(filePath, charset, hintMap));
}
/***
*
* @param qrCodeData
* @param filePath
* @param charset
* @param hintMap
* @param qrCodeheight
* @param qrCodewidth
* @throws WriterException
* @throws IOException
*/
public static void createQRCode(String qrCodeData, String filePath,
String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
throws WriterException, IOException {
BitMatrix matrix = new MultiFormatWriter().encode(
new String(qrCodeData.getBytes(charset), charset),
BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
.lastIndexOf('.') + 1), new File(filePath));
}
/**
*
* @param filePath
* @param charset
* @param hintMap
*
* @return Qr Code value
*
* @throws FileNotFoundException
* @throws IOException
* @throws NotFoundException
*/
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap);
return qrCodeResult.getText();
}
}
package com.attention.mark;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入javax.imageio.imageio;
导入com.google.zxing.BarcodeFormat;
导入com.google.zxing.BinaryBitmap;
导入com.google.zxing.EncodeHintType;
导入com.google.zxing.multiformatrader;
导入com.google.zxing.MultiFormatWriter;
导入com.google.zxing.NotFoundException;
导入com.google.zxing.Result;
导入com.google.zxing.writereException;
导入com.google.zxing.client.j2se.BufferedImageLuminanceSource;
导入com.google.zxing.client.j2se.MatrixToImageWriter;
导入com.google.zxing.common.BitMatrix;
导入com.google.zxing.common.HybridBinarizer;
导入com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
公共类QR码{
/**
*
*@param args
*@WriterException
*@抛出异常
*@NotFoundException
*/
公共静态void main(字符串[]args)引发WriterException、IOException、,
NotFoundException{
字符串qrCodeData=“student3232\u 2015\u 12\u 15\u 10\u 29\u 46\u 123”;
String filePath=“F:\\pourlent\u ProjectsDirectory\u 2015-2016\\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\attentindaceusingqrcode\\QRCodes\\student3232\u 2015\u 12\u 15\u 10\u 29\u 46\u 123”;
字符串charset=“UTF-8”;//或“ISO-8859-1”
Map hintMap=newhashmap();
hintMap.put(EncodeHintType.ERROR\u CORRECTION,ErrorCorrectionLevel.L);
createQRCode(qrCodeData,filePath,charset,hintMap,200200);
System.out.println(“二维码图像创建成功!”);
System.out.println(“从二维码读取的数据:”
+readQRCode(文件路径、字符集、hintMap));
}
/***
*
*@param qrCodeData
*@param文件路径
*@param字符集
*@param hintMap
*@param qrCodeheight
*@param qrCodewidth
*@WriterException
*@抛出异常
*/
公共静态void createQRCode(字符串qrCodeData、字符串filePath、,
字符串字符集,映射hintMap,int-qrCodeheight,int-qrCodewidth)
引发WriterException,IOException{
BitMatrix矩阵=新的多格式编写器()。编码(
新字符串(qrCodeData.getBytes(字符集),字符集),
条形码格式.QR_码、qrCodewidth、qrCodeheight);
MatrixToImageWriter.writeToFile(矩阵,文件路径。子字符串(文件路径
.lastIndexOf('.')+1),新文件(filePath));
}
/**
*
*@param文件路径
*@param字符集
*@param hintMap
*
*@返回二维码值
*
*@抛出FileNotFoundException
*@抛出异常
*@NotFoundException
*/
公共静态字符串readQRCode(字符串文件路径、字符串字符集、映射hintMap)
抛出FileNotFoundException、IOException、NotFoundException{
BinaryBitmap BinaryBitmap=新的BinaryBitmap(新的混合二进制程序(
新的BufferedImageLuminanceSource(
ImageIO.read(新的FileInputStream(filePath()())));
结果qrCodeResult=新的MultiFormatReader().decode(二进制位图,hintMap);
返回qrCodeResult.getText();
}
}
你的问题是什么?问题是如何让算法与Zxing 3.2.1一起工作;此处显示了另一个方向:感谢您的回答,但在
Zxing 3.2.1
中,至少BufferedImageLuminanceSource
不适用于我,我用于读取qrcode,而[那个]()用于使用网络摄像头进行图像捕获。它必须包括所有的类这是版本2.2.0而不是3.2.1尽管如此,我感谢你的回答,因为你想要帮助。事实上,在完整的Stackoverflow上,3.2.1版本并不起作用。创建二维码的唯一有效代码来自crunshify,但我想要相反的方向…如果没有解决方案,我将使用2.2.0,但我仍然希望有人具备编写3.2.1个二维码解决方案的技能。您的代码是eclipse开发的良好基础,因为您没有使用android LIB。如果还将javase-3.2.1.jar
添加到外部jar中,则可以使用Zxing 3.2.1。
package com.attendance.mark;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCode {
/**
*
* @param args
* @throws WriterException
* @throws IOException
* @throws NotFoundException
*/
public static void main(String[] args) throws WriterException, IOException,
NotFoundException {
String qrCodeData = "student3232_2015_12_15_10_29_46_123";
String filePath = "F:\\Opulent_ProjectsDirectory_2015-2016\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\AttendanceUsingQRCode\\QRCodes\\student3232_2015_12_15_10_29_46_123";
String charset = "UTF-8"; // or "ISO-8859-1"
Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
System.out.println("QR Code image created successfully!");
System.out.println("Data read from QR Code: "
+ readQRCode(filePath, charset, hintMap));
}
/***
*
* @param qrCodeData
* @param filePath
* @param charset
* @param hintMap
* @param qrCodeheight
* @param qrCodewidth
* @throws WriterException
* @throws IOException
*/
public static void createQRCode(String qrCodeData, String filePath,
String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
throws WriterException, IOException {
BitMatrix matrix = new MultiFormatWriter().encode(
new String(qrCodeData.getBytes(charset), charset),
BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
.lastIndexOf('.') + 1), new File(filePath));
}
/**
*
* @param filePath
* @param charset
* @param hintMap
*
* @return Qr Code value
*
* @throws FileNotFoundException
* @throws IOException
* @throws NotFoundException
*/
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap);
return qrCodeResult.getText();
}
}