使用Map传出两个最大值和最小值
参数中 int conWidth,int conHeight 分别是存放图片窗口的长和宽源码:/**
* scaleImg(); 图像缩放方法,将图片缩放到适合控件的大小
* ImageIcon image为显示的图片
* int conWidth,int conHeight 分别是存放图片窗口的的长和宽
*/
public static Map scaleImage(ImageIcon image, double conWidth, double conHeight) {
Map imgWidthAndHeight = new HashMap();
//默认的边框间距
final double SMALL_SCALE = 0.95;
double imgWidth = image.getIconWidth();
double imgHeight = image.getIconHeight();
//原图的宽长比
double imgRatio = imgWidth/imgHeight;
//最终输出宽和长
double reImgWidth = 0;
double reImgHeight = 0;
//若原图的宽小于控件宽
if(imgWidth < conWidth){
if(imgHeight < conHeight){
reImgWidth = conWidth*SMALL_SCALE;
reImgHeight = reImgWidth/imgRatio;
}
else {
reImgHeight = conHeight*SMALL_SCALE;
reImgWidth = reImgHeight*imgRatio;
}
}
//若原图的宽大于控件宽
else {
if(imgHeight < conHeight){
reImgWidth = conWidth*SMALL_SCALE;
reImgHeight = reImgWidth/imgRatio;
}
//若原图的长宽同时大于控件的长宽,最复杂的情况
else {
//控件的长比宽大
double conRatio = conWidth/conHeight;
if (imgRatio < conRatio){
reImgHeight = conHeight*SMALL_SCALE;
reImgWidth = reImgHeight*imgRatio;
}
else {
reImgWidth = conWidth*SMALL_SCALE;
reImgHeight = reImgWidth/imgRatio;
}
}
}
imgWidthAndHeight.put("width", (int) reImgWidth);
imgWidthAndHeight.put("height", (int) reImgHeight);
return imgWidthAndHeight;
}
使用方法://图片等比缩放函数
Map imgWidthAndHeight = ImageProcess.scaleImage(image,IMG_WIDTH,IMG_HEIGHT);
scaleWidth = imgWidthAndHeight.get("width");
scaleHeight= imgWidthAndHeight.get("height");