//缩略图实现,将图片(jpg,gif,bmp等等)真实的变成想要的大小
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon; //Java图片缩小后不失真的代码(缩略图)
public class thumbs {
/**
* Resize a JPEG file and save it.
* @param inputfile complete path to original jpeg file
* @param outputfile complete save path
* @param newWidth width in pixel
* @param newHeight height in pixel
* @return success of resize and save
*/
boolean isDeleted=false;
public String resize(String inputfile, String outputfile, int newWidth, int newHeight,boolean del) {
this.isDeleted=del;
try {
Image img = new ImageIcon(ImageIO.read(new File(inputfile))).getImage();
if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return "no";
}
if((img.getHeight(null)<newHeight)&&(img.getWidth(null)<newWidth)) //如果图片高度和宽度小于设定,则不处理
{ return "small pic,not to deal with!";}
else
{ if (this.isDeleted) //确定是否要删除原文件
{
File file1 = new File(inputfile);
if(file1.exists() && file1.isFile())
{ file1.delete(); }
}
}
if((img.getHeight(null)>img.getWidth(null))&&(img.getHeight(null)>480))
{ newWidth=480;newHeight=640;} double rate1 = ((double) img.getWidth(null)) / (double)newWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double)newHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
BufferedImage outImg = new BufferedImage(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);
Graphics g = outImg.getGraphics();
g.drawImage(scaledImage, 0, 0, null);
g.dispose();
JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(outputfile));
JPEGEncodeParam param = jpegImageEncoder.getDefaultJPEGEncodeParam(outImg);
param.setQuality(0.7F, true);
jpegImageEncoder.encode(outImg, param);
return "OK!";
} catch (IOException e) {
e.printStackTrace();
return "failed!";
} } }
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon; //Java图片缩小后不失真的代码(缩略图)
public class thumbs {
/**
* Resize a JPEG file and save it.
* @param inputfile complete path to original jpeg file
* @param outputfile complete save path
* @param newWidth width in pixel
* @param newHeight height in pixel
* @return success of resize and save
*/
boolean isDeleted=false;
public String resize(String inputfile, String outputfile, int newWidth, int newHeight,boolean del) {
this.isDeleted=del;
try {
Image img = new ImageIcon(ImageIO.read(new File(inputfile))).getImage();
if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return "no";
}
if((img.getHeight(null)<newHeight)&&(img.getWidth(null)<newWidth)) //如果图片高度和宽度小于设定,则不处理
{ return "small pic,not to deal with!";}
else
{ if (this.isDeleted) //确定是否要删除原文件
{
File file1 = new File(inputfile);
if(file1.exists() && file1.isFile())
{ file1.delete(); }
}
}
if((img.getHeight(null)>img.getWidth(null))&&(img.getHeight(null)>480))
{ newWidth=480;newHeight=640;} double rate1 = ((double) img.getWidth(null)) / (double)newWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double)newHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
BufferedImage outImg = new BufferedImage(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);
Graphics g = outImg.getGraphics();
g.drawImage(scaledImage, 0, 0, null);
g.dispose();
JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(outputfile));
JPEGEncodeParam param = jpegImageEncoder.getDefaultJPEGEncodeParam(outImg);
param.setQuality(0.7F, true);
jpegImageEncoder.encode(outImg, param);
return "OK!";
} catch (IOException e) {
e.printStackTrace();
return "failed!";
} } }
解决方案 »
- hashCode返回值的问题?急
- 问个 struts2 session的问题
- 请问jdk 1.5中的bug 6760149在哪个版本中修复?
- char类型的变量能存放一个汉字吗?谢谢!
- 请问uitl类中properties的set/getProperity与System.get/setProperty有什么区别啊???
- 急问关于Date的一个问题(在线等)
- 继“关于java socket的问题”
- 急急,急需InstallAnywhere5.0.5,或5.0.4的注册文件,请大虾帮忙
- 在线播放
- 有没有和在下一样开发IBM电子商务平台的,有的请进来!!
- 实际开发问题,高手帮忙一下,谢了!!!!!!!!!!!!!!!
- JFreeChart传值乱码的问题
未释放文件请加入
finally{
//关闭流;
if(file !=null ){
file.close();
}
}