条形码解析工具类

 条形码解析工具类


package com.wuss;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

/** 
 * 条形码解析工具类
 * @author wuss
 * @date 2015-09-09 
 */  
public class Barcode {
	/** 
     * @param imgPath 
     * @return String 
     */  
    public static String decode(String imgPath) throws Exception {  
        BufferedImage image = null;  
        Result result = null;  
        try {  
            image = ImageIO.read(new File(imgPath));  
            if (image == null) {  
                System.out.println("the decode image may be not exit.");  
            }  
            LuminanceSource source = new BufferedImageLuminanceSource(image);  
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
  
            Hashtable hints = new Hashtable();  
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");  
  
            result = new MultiFormatReader().decode(bitmap, hints);  
            return result.getText();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  

    
    public static void main(String[] args) throws Exception {
		System.out.println(decode("D:/test/1.jpeg"));
	}
}

jar包下载地址: http://download.csdn.net/detail/u012104497/9091803

你可能感兴趣的:(JAVAEE,javase)