依赖
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j-light</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
后台代码
package com.checkStart.controller;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import com.google.zxing.multi.qrcode.QRCodeMultiReader;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.*;
public class ScanBarcode {
public static void main(String[] args) throws Exception{
BarCode128C bc = new BarCode128C();
//bc.drawCode128C("123456789123456789123456789", 160, "D:\\12345678.jpg");
//bc.getCode128CPicture("1234546789abscxjabdcb", 100, "D:\\1.jpg");
// String code = "123456789";
// BufferedImage image = BarCodeUtil.insertWords(BarCodeUtil.getBarCode(code), code);
// ImageIO.write(image, "jpg", new File("D://123.jpg"));
//识别单个条形码
//analysisBarCode("D://img//CNJ-DD2-220100000011.png");
//识别多个条形码
//analysisBarCodeGroup("D://img//11.jpeg");
//识别多个二维码
//analysisQRcodeGroup("D://img//22.jpeg");
readFile("D://img");
}
public static void analysisBarCodeGroup(String path) throws FileNotFoundException, IOException, Exception{
String picode = "";
BufferedImage bi = ImageIO.read(new FileInputStream(new File(path)));
if (bi != null) {
Map<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
LuminanceSource source = new BufferedImageLuminanceSource(bi);
Hashtable ht = new Hashtable();
ht.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
MultiFormatReader mreader = new MultiFormatReader();
GenericMultipleBarcodeReader multireader = new GenericMultipleBarcodeReader(mreader);
Result[] result2 = multireader.decodeMultiple(bitmap,hints);
for(int i=0;i<result2.length;i++){
System.out.println(result2[i].getText());
}
}
}
public static String analysisBarCode(String path) throws FileNotFoundException, IOException, Exception{
String picode = "";
BufferedImage bi = ImageIO.read(new FileInputStream(new File(path)));
if (bi != null) {
Map<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
LuminanceSource source = new BufferedImageLuminanceSource(bi);
// 这里还可以是
//BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable ht = new Hashtable();
ht.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Result res = new MultiFormatReader().decode(bitmap, ht);
System.out.println("图片中内容: "+ res.getText());
picode = res.getText();
System.out.println("图片中格式: " + res.getBarcodeFormat());
}
// 这是条形码图片
return picode;
}
//同时解析多个二维码
public static String analysisQRcodeGroup(String path) throws FileNotFoundException, IOException, Exception{
String picode = "";
BufferedImage bi = ImageIO.read(new FileInputStream(new File(path)));
if (bi != null) {
Map<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
LuminanceSource source = new BufferedImageLuminanceSource(bi);
// 这里还可以是
//BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
QRCodeMultiReader qc = new QRCodeMultiReader();
Result[] r = qc.decodeMultiple(bitmap, hints);
for(int i=0;i<r.length;i++){
System.out.println("图片中内容: "+ r[i].getText());
}
//Result[] res = new MultiFormatReader().decode(bitmap, hints);
//for(int i=0;i<res.length;i++){
//System.out.println("图片中内容: "+ res[i].getText());
//}
//System.out.println("图片中内容: "+ res.getText());
//picode = res.getText();
//System.out.println("图片中格式: " + res.getBarcodeFormat());
}
return picode;
}
//读取文件夹下所有文件
public static void readFile(String filepath){
File file = new File(filepath);
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
System.out.println("path=" + readfile.getPath());
//System.out.println("absolutepath=" + readfile.getAbsolutePath());
//System.out.println("name=" + readfile.getName());
}
}
}