How to calculate Image Height and Width : Java Code Examples.
How to calculate Image Height and Width in java
// stdBoxWidth : Width of your box where you want to render the Image file.
// stdBoxHeight : Height of your box where you want to render the Image file.
public Map getImageHeightWith(String imagePath, String imageName, int stdBoxWidth, int stdBoxHeight){
Map ht=null;
/*
* this is the standard height & width of jsp box.
* */
int StdHeight=stdBoxHeight;
int StdWidth=stdBoxWidth;
/*
* this variable is for after compress height & width.
* */
float afterReduceWidthSize = 0f;
float afterReduceheightSize = 0f;
try {
ImageIcon imageI = new ImageIcon(imagePath+"/"+imageName);
/*
* This is the original height & width of image.
* */
int oriHeight=imageI.getIconHeight();
int oriWidth=imageI.getIconWidth();
if(oriHeight <= 0 || oriWidth <= 0){
ht=new Hashtable();
ht.put("height", new Float(stdBoxHeight));
ht.put("width", new Float(stdBoxWidth));
}else{
if((oriHeight > StdHeight) || (oriWidth > StdWidth)){
if(oriHeight > oriWidth){
/*
* This is used to find increased %of box image height.
*/
float inc_height = ((oriHeight-StdHeight) * 100f)/oriHeight;
afterReduceWidthSize = oriWidth - (oriWidth * inc_height/100f);
afterReduceheightSize = oriHeight - (oriHeight * inc_height/100f);
if(afterReduceWidthSize > StdWidth){
float inc_hi = ((afterReduceWidthSize - StdWidth) * 100f)/afterReduceWidthSize;
afterReduceWidthSize = afterReduceWidthSize - (afterReduceWidthSize * inc_hi/100f);
afterReduceheightSize = afterReduceheightSize - (afterReduceheightSize * inc_hi/100f);
}
ht=new Hashtable();
ht.put("height", new Float(afterReduceheightSize));
ht.put("width", new Float(afterReduceWidthSize));
} else {
/*
* This is used to find increased %of box image width.
*/
float inc_width = ((oriWidth - StdWidth) * 100f)/oriWidth;
afterReduceWidthSize = oriWidth - (oriWidth * inc_width/100f);
afterReduceheightSize = oriHeight - (oriHeight * inc_width/100f);
if(afterReduceheightSize > StdHeight){
float inc_wi = ((afterReduceheightSize - StdHeight) * 100f)/afterReduceheightSize;
afterReduceWidthSize = afterReduceWidthSize - (afterReduceWidthSize * inc_wi/100f);
afterReduceheightSize = afterReduceheightSize - (afterReduceheightSize * inc_wi/100f);
}
ht=new Hashtable();
ht.put("height", new Float(afterReduceheightSize));
ht.put("width", new Float(afterReduceWidthSize));
}
} else {
ht=new Hashtable();
ht.put("height", new Float(oriHeight));
ht.put("width", new Float(oriWidth));
}
}
} catch (Exception e) {
log.error("Exception in ArticleHelper - getImageHeightWith: "
+ e.getMessage());
e.printStackTrace();
}
return ht;
}
// stdBoxWidth : Width of your box where you want to render the Image file.
// stdBoxHeight : Height of your box where you want to render the Image file.
public Map getImageHeightWith(String imagePath, String imageName, int stdBoxWidth, int stdBoxHeight){
Map ht=null;
/*
* this is the standard height & width of jsp box.
* */
int StdHeight=stdBoxHeight;
int StdWidth=stdBoxWidth;
/*
* this variable is for after compress height & width.
* */
float afterReduceWidthSize = 0f;
float afterReduceheightSize = 0f;
try {
ImageIcon imageI = new ImageIcon(imagePath+"/"+imageName);
/*
* This is the original height & width of image.
* */
int oriHeight=imageI.getIconHeight();
int oriWidth=imageI.getIconWidth();
if(oriHeight <= 0 || oriWidth <= 0){
ht=new Hashtable();
ht.put("height", new Float(stdBoxHeight));
ht.put("width", new Float(stdBoxWidth));
}else{
if((oriHeight > StdHeight) || (oriWidth > StdWidth)){
if(oriHeight > oriWidth){
/*
* This is used to find increased %of box image height.
*/
float inc_height = ((oriHeight-StdHeight) * 100f)/oriHeight;
afterReduceWidthSize = oriWidth - (oriWidth * inc_height/100f);
afterReduceheightSize = oriHeight - (oriHeight * inc_height/100f);
if(afterReduceWidthSize > StdWidth){
float inc_hi = ((afterReduceWidthSize - StdWidth) * 100f)/afterReduceWidthSize;
afterReduceWidthSize = afterReduceWidthSize - (afterReduceWidthSize * inc_hi/100f);
afterReduceheightSize = afterReduceheightSize - (afterReduceheightSize * inc_hi/100f);
}
ht=new Hashtable();
ht.put("height", new Float(afterReduceheightSize));
ht.put("width", new Float(afterReduceWidthSize));
} else {
/*
* This is used to find increased %of box image width.
*/
float inc_width = ((oriWidth - StdWidth) * 100f)/oriWidth;
afterReduceWidthSize = oriWidth - (oriWidth * inc_width/100f);
afterReduceheightSize = oriHeight - (oriHeight * inc_width/100f);
if(afterReduceheightSize > StdHeight){
float inc_wi = ((afterReduceheightSize - StdHeight) * 100f)/afterReduceheightSize;
afterReduceWidthSize = afterReduceWidthSize - (afterReduceWidthSize * inc_wi/100f);
afterReduceheightSize = afterReduceheightSize - (afterReduceheightSize * inc_wi/100f);
}
ht=new Hashtable();
ht.put("height", new Float(afterReduceheightSize));
ht.put("width", new Float(afterReduceWidthSize));
}
} else {
ht=new Hashtable();
ht.put("height", new Float(oriHeight));
ht.put("width", new Float(oriWidth));
}
}
} catch (Exception e) {
log.error("Exception in ArticleHelper - getImageHeightWith: "
+ e.getMessage());
e.printStackTrace();
}
return ht;
}
Comments
Post a Comment