Java读取图片像素和大小

来源:互联网 发布:淘师湾算法及其描述 编辑:程序博客网 时间:2024/05/18 19:20
package com.aa.promotion.dao.generalize;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;


import javax.imageio.ImageIO;


public class UmpGeneralizeDAO {


public static void main(String[] args) {
File file = new File("E:\\test.jpg");
FileChannel fc = null;
if(file.exists() && file.isFile()){
try {
FileInputStream fs = new FileInputStream(file);
fc = fs.getChannel();
System.out.println(fc.size() + "-----fc.size()");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(file.length() + "-----file.length  B");
System.out.println(file.length() * 1024 + "-----file.length  kb");
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}

int width = bi.getWidth();
int height = bi.getHeight();

System.out.println("宽:像素-----" + width + "高:像素"  + height);


}
}


获取到的图片大小默认为B
0 1
原创粉丝点击