用JAVA从图像中读取条形码
我有一个java应用程序,需要将条形码从图像读取到java程序。我对这个库印象深刻,它能够检索条形码,但不是所有的图像(我的意思是,如果图像质量稍差的话) 我的问题是,阅读条形码最理想的图像格式是什么?JPEG还是PNG?我目前正在尝试JPEG图像 还有一个问题,从图像中检索条形码最可靠的JavaAPI/SDK是什么。我已经试过了,这是付费版本。并且已经通过了一些公开来源,比如。 但我仍然在寻找一个JavaAPI/SDK,它可以在条形码读取中提供准确的结果用JAVA从图像中读取条形码,java,image,Java,Image,我有一个java应用程序,需要将条形码从图像读取到java程序。我对这个库印象深刻,它能够检索条形码,但不是所有的图像(我的意思是,如果图像质量稍差的话) 我的问题是,阅读条形码最理想的图像格式是什么?JPEG还是PNG?我目前正在尝试JPEG图像 还有一个问题,从图像中检索条形码最可靠的JavaAPI/SDK是什么。我已经试过了,这是付费版本。并且已经通过了一些公开来源,比如。 但我仍然在寻找一个JavaAPI/SDK,它可以在条形码读取中提供准确的结果 您关于条形码阅读器JavaAPI/SD
您关于条形码阅读器JavaAPI/SDK的信息对我非常有用 > JavaBar是一个可以被认为是开源的,并且有很好的评论 最近,我发现这个软件在阅读条形码方面取得了很好的效果。 它有一个从命令提示符解码条形码的选项。因为,它不是SDK或API。所以,我做了一个技巧,用java程序从图像中读取条形码
import java.io.*;
public class BarCodeReader {
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("C:/Program Files/ZBar/bin/zbarimg D:/jpeg/006.jpg");
p.waitFor();
BufferedReader reader=new BufferedReader(
new InputStreamReader(p.getInputStream())
);
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}
System.out.println("Done");
}
}
希望,这对任何试图用JAVA从图像中读取条形码的人都有帮助
注意:它也适用于Linux。我测试过了。
对于linux环境,只需运行以下命令
sudo-apt-get-install-zbar工具
在Java程序中,像这样更改代码
Process p=Runtime.getRuntime().exec("zbarimg /opt/images/006.jpg");// your file path.
而且效果很好
若你们遇到任何其他可以在命令行上运行的条形码读取SDK、API或软件,请留下答案 JPEG是一种有损压缩,根据压缩级别、图像分辨率和图像质量(照明、对比度等),JPEG压缩引入的伪影可能会干扰条形码的读取 PNG是无损的。这将导致一个更大的文件,但不会为条形码引擎的读取增加额外的挑战(工件) 对于条形码引擎的准确性,这一点差别很大。某些引擎在某些类型的条形码(例如128码与QR码)和图像质量问题(低分辨率与低对比度)上表现更好。如果由于无法控制图像来源而导致各种图像质量问题,则可能需要使用商业引擎才能获得最高的效果。
改善结果的一个选项是在发送到引擎之前进行一些图像处理。在从灰色/彩色变为黑白之前,请尝试放大图像。比引擎中提供的更好的二值化(灰色到黑白)也会有所帮助
完全公开,我为Accusoft工作,Accusoft是一家条形码SDK提供商。@Tom Setzer的解决方案非常棒,如果您不介意为您的项目支付一点额外费用的话。然而,如果你没有预算购买这样的软件,我还是建议你听听汤姆的答案 改善结果的一个选项是在发送到引擎之前进行一些图像处理。在从灰色/彩色变为黑白之前,请尝试放大图像。比引擎中提供的更好的二值化(灰色到黑白)也会有所帮助 他是对的,但我还在用ZXing。只有在尝试读取条形码之前进行了一些图像处理,它才会非常有效 我正在使用OpenCV进行图像处理。这是一个很棒的本机库,既适用于Linux和Windows,也适用于其他一些平台(我还没有研究过) 我就是这样做的
资源:
org.apache.camel
骆驼条码
2.21.1
一些示例(如果需要,可旋转)
导入com.google.zxing.BinaryBitmap;
导入com.google.zxing.multiformatrader;
导入com.google.zxing.Reader;
导入com.google.zxing.Result;
导入com.google.zxing.client.j2se.BufferedImageLuminanceSource;
导入com.google.zxing.common.HybridBinarizer;
进口龙目吸气剂;
导入javax.imageio.imageio;
导入java.io.IOException;
导入java.io.InputStream;
公共类条码图像解码器{
公共条码信息解码图像(InputStream InputStream)引发条码解码异常{
试一试{
BinaryBitmap位图=新的BinaryBitmap(新的混合二进制程序(
新的BufferedImageLuminanceSource(ImageIO.read(inputStream));
if(bitmap.getWidth()
<
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-barcode -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-barcode</artifactId>
<version>2.21.1</version>
</dependency>
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import lombok.Getter;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.io.InputStream;
public class BarcodeImageDecoder {
public BarcodeInfo decodeImage(InputStream inputStream) throws BarcodeDecodingException {
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(ImageIO.read(inputStream))));
if (bitmap.getWidth() < bitmap.getHeight()) {
if (bitmap.isRotateSupported()) {
bitmap = bitmap.rotateCounterClockwise();
}
}
return decode(bitmap);
} catch (IOException e) {
throw new BarcodeDecodingException(e);
}
}
private BarcodeInfo decode(BinaryBitmap bitmap) throws BarcodeDecodingException {
Reader reader = new MultiFormatReader();
try {
Result result = reader.decode(bitmap);
return new BarcodeInfo(result.getText(), result.getBarcodeFormat().toString());
} catch (Exception e) {
throw new BarcodeDecodingException(e);
}
}
public static class BarcodeInfo {
@Getter
private final String text;
@Getter
private final String format;
BarcodeInfo(String text, String format) {
this.text = text;
this.format = format;
}
}
public static class BarcodeDecodingException extends Exception {
BarcodeDecodingException(Throwable cause) {
super(cause);
}
}
}
<dependency>
<groupId>net.imagej</groupId>
<artifactId>ij</artifactId>
<version>1.52g</version>
</dependency>
Convert to the image to grayscale.
Resize barcode up to 4 times in height and 8 times in width.
Apply gaussian blur with the size of 17x17 pixels.
Apply binary threshold with the threshold value of 225 and maximum value of 255.
public BufferedImage preProcessBufferedImage (BufferedImage bufferedImage)throws IOException{
//get subimage that cuts out the QR code, speeds up the QR recognition process
BufferedImage subImage = bufferedImage.getSubimage(x, y,width,height);
//gaussian blur the result , leads to better QR code recognition
ImagePlus imagePlus = new ImagePlus("process-qr-code", subImage);
imagePlus.getProcessor().blurGaussian(2);
return imagePlus.getBufferedImage();
}