您的位置:首页 > 编程语言 > Java开发

JAVA 压缩图片-解析一维码二维码-ZXING

2016-04-14 00:00 393 查看
之所要压缩图片,是因为如果图片太大了,ZXING会解析失败。

另外一点就是,一维码二维码这个图片必须是水平放置,其他方向很可能会导致解析失败。

##首先使用这个类来压缩图片:

CompressPicDemo

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息