java解析二维码

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.sinocom.util.PubParameters;
import com.sinosoft.lis.pubfun.PubFun;

/**
 * 解析二维码
 *
 * @author liuli
 *
 */
public class ParsingQrCode {
 private static Log log = LogFactory.getLog(ParsingQrCode.class);

 
/**
 * 解析二维码内容
 * @param fileName  图片名称
 * @param FilePath  图片地址(在本系统中可以为空 去系统默认的)
 * @return
 */
 public static String parsingQrCode(String fileName, String FilePath) {
  String cotent ="";
        if ("".equals(FilePath) || FilePath ==null) {
         FilePath = PubFun.getUIPath()+PubParameters.QRCODEPATH;
  }
        MultiFormatReader formatReader = new MultiFormatReader();
        String filePath = FilePath+fileName;
        File file = new File(filePath);
        if (!file.exists()) {
         log.error("文件已不存在");
        }else{
         try {
       BufferedImage image = ImageIO.read(file);
       ;
       LuminanceSource source = new BufferedImageLuminanceSource(image);
       Binarizer binarizer = new HybridBinarizer(source);
       BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
       Map hints = new HashMap();
       hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
       Result result = formatReader.decode(binaryBitmap, hints);
       cotent = result.toString();
      } catch (Exception e) {
       log.error(e);
      }
        } 
  return cotent;
 }
 
 public static void main(String[] args) {
  System.out.println("==条形码内容=="+parsingQrCode("b2e.png",""));
 }
}

你可能感兴趣的:(java)