- 论坛徽章:
- 0
|
下面代码中用到的sourceImage是一个已经存在的Image对象
图像剪切
对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:
//import
java.awt.*;
//import java.awt.image.*;
Image
croppedImage;
ImageFilter cropFilter;
CropFilter =new
CropImageFilter(25,30,75,75); //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int
x,int y,int width,int height),详细情况请参考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new
FilteredImageSource(sourceImage.getSource(),cropFilter));
如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProducer对象。
图像缩放
对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:
Image scaledImage=sourceImage.
getScaledInstance(100,100, Image.SCALE_DEFAULT); [color="#000099"]//[color="#000099"]得到一个100X100[color="#000099"]的图像
Image
doubledImage=sourceImage.
getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2,
Image.SCALE_DEFAULT); [color="#000099"]//[color="#000099"]得到一个放大两倍的图像,[color="#000099"]这个程序一般在一个swing[color="#000099"]的组件中使用,而类Jcomponent[color="#000099"]实现了图像观察者接口ImageObserver[color="#000099"],所有可以使用this。
//[color="#000099"]其它情况请参考API
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/340/showart_60237.html |
|