Java获取图片的宽高等信息

Java获取图片的宽高等信息
 
import zzvcom.netvideo.common.toolkit.exception.EvalException;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* 图片信息获取测试
*
* @author leizhimin 2009-6-30 16:03:05
*/

public class TestImg {
         /**
         * 计算图片尺寸大小等信息:w宽、h高、s大小。异常时返回null。
         *
         * @param imgpath 图片路径
         * @return 图片信息map
         */

         public static Map<String, Long> getImgInfo(String imgpath) {
                Map<String, Long> map = new HashMap<String, Long>(3);
                File imgfile = new File(imgpath);
                 try {
                        FileInputStream fis = new FileInputStream(imgfile);
                        BufferedImage buff = ImageIO.read(imgfile);
                        map.put( "w", buff.getWidth() * 1L);
                        map.put( "h", buff.getHeight() * 1L);
                        map.put( "s", imgfile.length());
                        fis.close();
                } catch (FileNotFoundException e) {
                        System.err.println( "所给的图片文件" + imgfile.getPath() + "不存在!计算图片尺寸大小信息失败!");
                        map = null;
                } catch (IOException e) {
                        System.err.println( "计算图片" + imgfile.getPath() + "尺寸大小信息失败!");
                        map = null;
                }
                 return map;
        }


         public static void main(String[] args) throws EvalException {
                String p = "c:\\111.jpg";
                Map<String, Long> m = getImgInfo(p);
                 for (Map.Entry<String, Long> entry : m.entrySet()) {
                        System.out.println(entry.getKey() + " " + entry.getValue());
                }
        }
}
 
w 120
s 8148
h 90

Process finished with exit code 0
 
还可以做更多的处理,比如剪切图片,获取颜色等属性等等。
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值