load进一个图片,如果对它进行放大缩小呢?
查找了很多地方的资料,找到以下的方法可以解决放大缩小图片问题:
public static ImageIcon imageIconZoom(ImageIcon orgImageIcon, int newWidth,
int newHeight) {
BufferedImage tag = new BufferedImage(newWidth, newHeight,
BufferedImage.TYPE_INT_ARGB);
tag.getGraphics().drawImage(orgImageIcon.getImage(), 0, 0, newWidth,
newHeight, null);
return new ImageIcon(tag);
}
如何对图片着色呢?
static final float CRITICAL_BAND_MATRIX[][] = { { 1.0f, 0.0f, 0.0f , 0.0f},
{ 0.0f, 0.0f, 0.0f , 0.0f}, { 0.0f, 0.0f, 0.0f,0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } };
static final float MAJOR_BAND_MATRIX[][] = { { 1.0f, 0.0f, 0.0f , 0.0f},
{ 0.0f, 0.5f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f , 0.0f}, { 1.0f, 0.5f, 0.0f, 0.0f } };
static final float MINOR_BAND_MATRIX[][] = { { 1.0f, 0.0f, 0.0f , 0.0f},
{ 0.0f, 1.0f, 0.0f , 0.0f}, { 0.0f, 0.0f, 0.0f, 0.0f } , { 1.0f, 1.0f, 0.0f, 0.0f }};
static final float WARNING_BAND_MATRIX[][] = { { 0.5f, 0.0f, 0.0f , 0.0f},
{ 0.0f, 0.0f, 0.0f , 0.0f}, { 0.0f, 0.0f, 1.0f , 0.0f} , { 0.5f, 0.0f, 1.0f, 0.0f }};
static final float CLEAR_BAND_MATRIX[][] = { { 0.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f, 0.0f } };
static final float UNKNOWN_BAND_MATRIX[][] = { { 0.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f , 0.0f}, { 0.0f, 0.0f, 1.0f, 0.0f } , { 0.0f, 0.0f, 1.0f, 0.0f }};
public static ImageIcon ColorImage(ImageIcon orgImage, int alarmStatus ){
float[][] targetMatrix ;
switch(alarmStatus){
case CommonConstants.ALARM_UNKNOWN:
targetMatrix = UNKNOWN_BAND_MATRIX;
break;
case CommonConstants.ALARM_CLEAR:
targetMatrix = CLEAR_BAND_MATRIX;
break;
case CommonConstants.ALARM_MINOR:
targetMatrix = MINOR_BAND_MATRIX;
break;
case CommonConstants.ALARM_MAJOR:
targetMatrix = MAJOR_BAND_MATRIX;
break;
case CommonConstants.ALARM_CRITICAL:
targetMatrix = CRITICAL_BAND_MATRIX;
case CommonConstants.ALARM_WARNING:
targetMatrix = WARNING_BAND_MATRIX;
break;
default:
targetMatrix = UNKNOWN_BAND_MATRIX;
}
BufferedImage b1 ;
BufferedImage biSrc = new BufferedImage(orgImage.getIconWidth(), orgImage.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
// biSrc.getGraphics().setColor(Color.white);
// biSrc.getGraphics().fillRect(0, 0, orgImage.getIconWidth(), orgImage.getIconHeight());
biSrc.getGraphics().drawImage(orgImage.getImage(),0,0,null);
BufferedImage biTar = new BufferedImage(orgImage.getIconWidth(), orgImage.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Raster srcRaster = biSrc.getRaster();
WritableRaster dstRaster = (WritableRaster) biTar.getRaster();
BandCombineOp bandCombineOp = new BandCombineOp(targetMatrix, null);
bandCombineOp.filter(srcRaster, dstRaster);
b1 = biTar;
return new ImageIcon(b1);
}
其中值得一说,却又说不清楚的地方是:BufferedImage.TYPE_INT_ARGB, 为什么要用TYPE_INT_ARGB,而不用TYPE_INT_RGB,之前我是用的TYPE_INT_RGB,而且matrix也是3*3的一个矩阵,后来发现,着色前,图片透明的地方,着色后,变成了黑色的了,于是猜想ARGB中的A应该是alphe的意思,就摸索构造了矩阵,并且着了实验,用是能用,不知道对不对啊。。。。
posted @
2006-12-29 16:38 托托姆 阅读(948) |
评论 (1) |
编辑 收藏