读取
Java API
方式一:
File picture = new File("C:\\Users\\Administrator\\Desktop\\normal_face/1.pgm");
BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture) );
System.out.println(String.format("%.1f",picture.length()/1024.0));// 源图大小
System.out.println(sourceImg.getWidth()); // 源图宽度
System.out.println(sourceImg.getHeight()); // 源图高度
裁剪
Java API
BufferedImage类有一个getSubimage()方法,以下来自API
public BufferedImage getSubimage(int x,int y,int w,int h)
返回由指定矩形区域定义的子图像。返回的 BufferedImage 与源图像共享相同的数据数组。
参数:
x - 指定矩形区域左上角的 X 坐标
y - 指定矩形区域左上角的 Y 坐标
w - 指定矩形区域的宽度
h - 指定矩形区域的高度
返回:
BufferedImage,它是此 BufferedImage 的子图像。
抛出:
RasterFormatException - 如果指定区域不包含在此 BufferedImage 中。
其他
获取灰度(字符画)
private static int getGray(Color rgb) {
int blue = rgb.getBlue();
int green = rgb.getGreen();
int red = rgb.getRed();
return (red*299 + green*587 + blue*114 + 500) / 1000;
}
注:网络上流行的字符画就是采用这种方式获得图片灰度值,并输出到文件中