来源:http://blog.javaeye.com/blog/167954
最近做手机页面,因为手机的缓存很小,需要图片显示时,要将图片的大小进行调整。所以研究了一下,查了些
文章,以这个http://www.webjx.com/htmldata/2005-04-25/1114436950.html为基础进行了一些修改可以自定义大小。
下面是代码:
import
java.io.File;
import java.io.FileOutputStream;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class MakeBreviaryImage ... {
public void MakeBreviaryImage(File image, int formatWideth,
int formatHeight, String savePath) throws Exception ...{
File _file = image;// get image file
Image src = javax.imageio.ImageIO.read(_file); // construct image
int imageWideth = src.getWidth(null); // get wideth of image
int imageHeight = src.getHeight(null); // get height of image
int changeToWideth = 0;
int changeToHeight = 0;
// set Breviary image's size
if (imageWideth > 0 && imageHeight > 0) ...{
// flag=true;
if (imageWideth / imageHeight >= formatWideth / formatHeight) ...{
if (imageWideth > formatWideth) ...{
changeToWideth = formatWideth;
changeToHeight = (imageHeight * formatWideth) / imageWideth;
} else ...{
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
} else ...{
if (imageHeight > formatHeight) ...{
changeToHeight = formatHeight;
changeToWideth = (imageWideth * formatHeight) / imageHeight;
} else ...{
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
}
}
// ---------
BufferedImage bufferedImage = new BufferedImage(changeToWideth,
changeToHeight, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(src, 0, 0, changeToWideth,
changeToHeight, null); // chage image size
FileOutputStream out = new FileOutputStream(savePath); // out put the
// image
JPEGImageEncoder encoder = null;
encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufferedImage); // like JPEG encoding
// System.out.print(width+"*"+height);
out.close();
}
}
import java.io.FileOutputStream;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class MakeBreviaryImage ... {
public void MakeBreviaryImage(File image, int formatWideth,
int formatHeight, String savePath) throws Exception ...{
File _file = image;// get image file
Image src = javax.imageio.ImageIO.read(_file); // construct image
int imageWideth = src.getWidth(null); // get wideth of image
int imageHeight = src.getHeight(null); // get height of image
int changeToWideth = 0;
int changeToHeight = 0;
// set Breviary image's size
if (imageWideth > 0 && imageHeight > 0) ...{
// flag=true;
if (imageWideth / imageHeight >= formatWideth / formatHeight) ...{
if (imageWideth > formatWideth) ...{
changeToWideth = formatWideth;
changeToHeight = (imageHeight * formatWideth) / imageWideth;
} else ...{
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
} else ...{
if (imageHeight > formatHeight) ...{
changeToHeight = formatHeight;
changeToWideth = (imageWideth * formatHeight) / imageHeight;
} else ...{
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
}
}
// ---------
BufferedImage bufferedImage = new BufferedImage(changeToWideth,
changeToHeight, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(src, 0, 0, changeToWideth,
changeToHeight, null); // chage image size
FileOutputStream out = new FileOutputStream(savePath); // out put the
// image
JPEGImageEncoder encoder = null;
encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufferedImage); // like JPEG encoding
// System.out.print(width+"*"+height);
out.close();
}
}
调用时
public
class
TestMakeBreviaryImage
...
{
/** *//**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception ...{
File testImage = new File("需要进行转换的图片文件路径");
MakeBreviaryImage test = new MakeBreviaryImage();
test.MakeBreviaryImage(testImage,60,50,"C:/newfile.jpg");//参数一次为:图片文件,需要的宽度,需要的高度,输出路径
}
}
/** *//**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception ...{
File testImage = new File("需要进行转换的图片文件路径");
MakeBreviaryImage test = new MakeBreviaryImage();
test.MakeBreviaryImage(testImage,60,50,"C:/newfile.jpg");//参数一次为:图片文件,需要的宽度,需要的高度,输出路径
}
}