批量识别条形码和二维码

该代码示例展示了如何使用barcode4j-light、ZXing库进行条形码和二维码的生成与识别。主要功能包括:生成条形码图片、识别单个条形码、识别多个条形码以及识别多个二维码。代码中包含了读取文件夹下所有图片的辅助方法。
摘要由CSDN通过智能技术生成

依赖

<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());
		
		}
	}
	
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值