识别图片中的某个位置的一维码

以一维码在页面的左上角为例

String orsrc=file.getAbsolutePath();
        BufferedImage sourceImg = null;
        try {
            sourceImg = ImageIO.read(new File(orsrc));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        int width=sourceImg.getWidth();
        int height=sourceImg.getHeight();
        orsrc=orsrc.replaceAll("\\\\", "/");
        int lastindex= orsrc.lastIndexOf("/");
        String srcone= orsrc.substring(0,lastindex+1);
        String srctwo= orsrc.substring(lastindex+1,orsrc.length());

        BufferedImage imgPath= CutPicture.cutPic(orsrc, 0, 0, width*2/3, height/4);

        //识别一维码中的内容 如 : HQ-101H000000001-1
        String content=ZxingDecoderHandler.decode(imgPath);

util类:

package com.util;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class CutPicture {
    
    /**
     * @param srcFile源文件
     * @param outFile输出文件
     * @param x坐标
     * @param y坐标
     * @param width宽度
     * @param height高度
     * @return
     * @描述 —— 裁剪图片
     */  
    public static BufferedImage cutPic(String srcFile, int x, int y,  
            int width, int height) {  
        FileInputStream is = null;  
        ImageInputStream iis = null;  
        try {  
            // 如果源图片不存在  
            if (!new File(srcFile).exists()) {  
                return null;  
            }  
 
            // 读取图片文件  
            is = new FileInputStream(srcFile);  
 
            // 获取文件格式  
            String ext = srcFile.substring(srcFile.lastIndexOf(".") + 1);  
 
            // ImageReader声称能够解码指定格式  
            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(ext);  
            ImageReader reader = it.next();  
 
            // 获取图片流  
            iis = ImageIO.createImageInputStream(is);  
 
            // 输入源中的图像将只按顺序读取  
            reader.setInput(iis, true);  
 
            // 描述如何对流进行解码  
            ImageReadParam param = reader.getDefaultReadParam();  
 
            // 图片裁剪区域  
            Rectangle rect = new Rectangle(x, y, width, height);  
 
            // 提供一个 BufferedImage,将其用作解码像素数据的目标  
            param.setSourceRegion(rect);  
 
            // 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象  
            BufferedImage bi = reader.read(0, param);  
 
            // 保存新图片  
           /* File tempOutFile = new File(outFile);  
            if (!tempOutFile.exists()) {  
                tempOutFile.mkdirs();  
            }  
            ImageIO.write(bi, ext, new File(outFile));  */
            return bi;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        } finally {  
            try {  
                if (is != null) {  
                    is.close();  
                }  
                if (iis != null) {  
                    iis.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
                return null;  
            }  
        }  
    }
}

package com.util;

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;

public class ZxingDecoderHandler {
    /**
     * @param imgPath
     * @return String
     */
    public static String decode(BufferedImage imgPath) {
        BufferedImage image = null;
        Result result = null;
        try {
            //image = ImageIO.read(new File(imgPath));
            image=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<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "utf-8");

            result = new MultiFormatReader().decode(bitmap, hints);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

需要core-3.3.2.jar、jai-imageio-core-1.4.0.jar、zxing-javase.jar这几个jar包

其中涉及图片的重命名、删除、移动等。

ps: 还有io流的输入输出记得关闭流

==================================更新(可能出现的bug)===============================

 sourceImg = ImageIO.read(new File(orsrc));

当读取流时,new File()内的orsc路径一定要包含图片具体名称 如:path+"\"+"xxxxx.jpg"

不然就会报没有权限访问的错误(具体是啥也记不太清了- - 找到错误原因最重要)

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值