public static BufferedImage convert(Image image, int scale)
{
int width = image.getWidth(null), height = image.getHeight(null);
//1.image是抽象类,怎么可以getWidth???
BufferedImage bufimg = new BufferedImage(width / scale, height / scale,
BufferedImage.TYPE_INT_RGB);
bufimg.getGraphics().drawImage(image, 0, 0, width, height, null);
return bufimg;
}
如果有高手的话,请每句注释一下,这个代码,对我很重要,我必须搞明白。谢谢
{
int width = image.getWidth(null), height = image.getHeight(null);
//1.image是抽象类,怎么可以getWidth???
BufferedImage bufimg = new BufferedImage(width / scale, height / scale,
BufferedImage.TYPE_INT_RGB);
bufimg.getGraphics().drawImage(image, 0, 0, width, height, null);
return bufimg;
}
如果有高手的话,请每句注释一下,这个代码,对我很重要,我必须搞明白。谢谢
你可以加一句话System.out.println(image.getClass().getName());
看看image指向的到底是什么类型的对象。
abstract int getWidth(ImageObserver observer) 确定图像的宽度。 BufferedImage 子类描述具有可访问图像数据缓冲区的 Image。static int TYPE_INT_BGR 表示一个具有 8 位 RGB 颜色分量的图像,对应于 Windows 或 Solaris 风格的 BGR 颜色模型,具有打包为整数像素的 Blue、Green 和 Red 三种颜色。Graphics getGraphics() 此方法返回 Graphics2D abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) 绘制指定图像中已缩放到适合指定矩形内部的图像。
abstract int getHeight(ImageObserver observer) 确定图像的高度。
abstract int getWidth(ImageObserver observer) 确定图像的宽度。 BufferedImage 子类描述具有可访问图像数据缓冲区的 Image。static int TYPE_INT_BGR
表示一个具有 8 位 RGB 颜色分量的图像,对应于 Windows 或 Solaris 风格的 BGR 颜色模型,具有打包为整数像素的 Blue、Green 和 Red 三种颜色。Graphics getGraphics() 此方法返回 Graphics2D abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
绘制指定图像中已缩放到适合指定矩形内部的图像。
{
//ZangXT已经解释了,传递进来的是Image的子类,子类里实现了这些getWidth之类的方法
int width = image.getWidth(null), height = image.getHeight(null);
BufferedImage bufimg = new BufferedImage(width / scale, height / scale, //新建可访问图像数据缓冲区的 Image
BufferedImage.TYPE_INT_RGB); // 表示一个图像,该图像具有打包成整数像素的 8 位 RGB 颜色分量。
//Graphics 类是所有图形上下文的抽象基类,允许应用程序可以在组件(已经在各种设备上实现),以及闭屏图像上,进行绘制
//下面这个就是绘制指定图像中已缩放到适合指定矩形内部的图像。
bufimg.getGraphics().drawImage(image, 0, 0, width, height, null);
return bufimg;
}
public static BufferedImage convert(Image image, int scale)
{
int width = image.getWidth(null), height = image.getHeight(null);
//1.image是抽象类,怎么可以getWidth??? 这个Image image肯定是继承java.awt.Image类,是它的一个子类。
//实现了抽象类方法。你看下那里定义了Image image.
//abstract int getWidth(ImageObserver observer) 确定图像的宽度。
//abstract int getHeight(ImageObserver observer)
确定图像的高度
BufferedImage bufimg = new BufferedImage(width / scale, height / scale,
BufferedImage.TYPE_INT_RGB);
//BufferedImage(int width, int height, int imageType, IndexColorModel cm)
// 构造一个类型为预定义图像类型之一的 BufferedImage:TYPE_BYTE_BINARY 或 TYPE_BYTE_INDEXED。
bufimg.getGraphics().drawImage(image, 0, 0, width, height, null);
// public Graphics getGraphics()此方法返回 Graphics2D,但此处是出于向后兼容性的考虑。createGraphics 更为方便,因为它被声明为返回//Graphics2D。
//abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
// 呈现一个图像,在绘制前进行从图像空间到用户空间的转换。
return bufimg;
}