之所要压缩图片,是由于若是图片太大了,ZXING会解析失败。java
另一点就是,一维码二维码这个图片必须是水平放置,其余方向极可能会致使解析失败。google
##首先使用这个类来压缩图片:code
CompressPicDemo mypic = new CompressPicDemo(); //建立压缩图对象 //compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) mypic.compressPic("c:\\原图片路径", "c:\\输出图片路径", "原图片.后缀", "输出图.后缀", 600, 600, true);
##而后使用ZXING来解码 用到的包 core-2.0.jar javase-1.7.jar对象
//解码 BufferedImage image = null; Result result = null; //com.google.zxing.Result 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)); result = new MultiFormatReader().decode(bitmap, null); String decodeContent = result.getText(); System.out.println("解码内容以下:" + decodeContent);