java 图片解析

Zxing读取图片的条码

注意点:

     PDF文件转义为jpg

    jpg文件的像素为:1080(自己测试得到)

   1080解析错误,就可以截图处理。基本都可以解析成功

第一步:导包:pom.xml

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>

第二步: 解决代码

    /**
     * @param imgPath
     * @return String
     */
    public static String decode(String imgPath) {
        try {
            BufferedImage image = ImageIO.read(new File(imgPath));
            if (image == null) {
                return "b";
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
 
            Map<DecodeHintType, Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "GBK");
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
 
            Result result = new MultiFormatReader().decode(bitmap, hints);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "a";
    }

测试代码:

   读取指定目录下的jpg文件

   读取jpg文件的目录:C:\\Users\\admin\\Desktop\\D24\\jpg\\screenshots\\jpg3

  解析后,解决的结果存在txt文档中目录:C:\\Users\\admin\\Desktop\\D24\\txt\\screenshots\\

public static void main(String[] args) throws IOException {        
        List<String> fileNames1 = new ArrayList<String>();
       findFileList(new File("C:\\Users\\admin\\Desktop\\D24\\jpg\\screenshots\\jpg3"),fileNames1 );
       for (String value :  fileNames1) {
            int begin= value.indexOf("jpg3");
           int last=value.length();
           File file = new File("C:\\Users\\admin\\Desktop\\D24\\txt\\screenshots\\"+value.substring(begin,last)+".txt");
           String imageBarcode = decode(value);
            FileParsingWriter(imageBarcode,file);
         }
      }

 

/**
     * 读取目录下的所有文件
     * 
     * @param dir
     *            目录
     * @param fileNames
     *            保存文件名的集合
     * @return
     */
    public static void findFileList(File dir, List<String> fileNames) {
        if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
            return;
        }
        String[] files = dir.list();// 读取目录下的所有目录文件信息
        for (int i = 0; i < files.length; i++) {// 循环,添加文件名或回调自身
            File file = new File(dir, files[i]);
            if (file.isFile()) {// 如果文件
                fileNames.add(dir + "\\" + file.getName());// 添加文件全路径名
            } else {// 如果是目录
                findFileList(file, fileNames);// 回调自身继续查询
            }
        }
    }

public static void FileParsingWriter(String imageBarcode,File file) {
        FileWriter fw = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file);
            fw.write(imageBarcode.toUpperCase()+"|");//向文件中写内容
            fw.flush();
            System.out.println("写数据成功!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值