javat图片缩小代码(测试通过)
2007-08-21 20:43
459 查看
最近用到的
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
import java.awt.Image;
public class PictureZoom {
public Icon getFixedBoundIcon(String filePath, int height, int width)
throws Exception {
//缩放比例
double Ratio = 0.0;
File F = new File(filePath);
if (!F.isFile())
throw new Exception(F
+ " is not image file error in getFixedBoundIcon!");
Icon ret = new ImageIcon(filePath);
BufferedImage Bi = ImageIO.read(F);
//当原始比例比目标比例大的时候执行
if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
//取长度和宽度中较大的为准,确定缩放比例
if (Bi.getHeight() > Bi.getWidth()) {
Ratio = (new Integer(height)).doubleValue() / Bi.getHeight();
} else {
Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
}
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0, lastLength);
String fileType = filePath.substring(lastLength);
File zoomFile = new File(subFilePath
+ fileType);
Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, "jpg", zoomFile);
ret = new ImageIcon(zoomFile.getPath());
} catch (Exception ex) {
System.out.println("######## here error : " + ex);
}
}
return ret;
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
import java.awt.Image;
public class PictureZoom {
public Icon getFixedBoundIcon(String filePath, int height, int width)
throws Exception {
//缩放比例
double Ratio = 0.0;
File F = new File(filePath);
if (!F.isFile())
throw new Exception(F
+ " is not image file error in getFixedBoundIcon!");
Icon ret = new ImageIcon(filePath);
BufferedImage Bi = ImageIO.read(F);
//当原始比例比目标比例大的时候执行
if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
//取长度和宽度中较大的为准,确定缩放比例
if (Bi.getHeight() > Bi.getWidth()) {
Ratio = (new Integer(height)).doubleValue() / Bi.getHeight();
} else {
Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
}
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0, lastLength);
String fileType = filePath.substring(lastLength);
File zoomFile = new File(subFilePath
+ fileType);
Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, "jpg", zoomFile);
ret = new ImageIcon(zoomFile.getPath());
} catch (Exception ex) {
System.out.println("######## here error : " + ex);
}
}
return ret;
}
}
相关文章推荐
- 通过Java代码实现图片的放大和缩小
- 通过Java代码实现图片的放大和缩小
- 破解图片防盗链的代码(asp/php)测试通过
- java 实现BufferedImage和ImageReader两种方式获取图片宽高、判断图片类型、获取图片大小工具类代码以及测试响应结果
- Wpf图片变换一:通过WPF实现图片的扩大、缩小、翻转、移动实例代码
- web版用户通过浏览器下载图片java后台代码
- java 通过ssh连接linux服务器的测试代码
- 将图片转换为base64字符串Java代码(未测试)
- 通过Java测试几种压缩算法的性能(附测试代码下载)
- 把重设新闻中图片尺寸的代码贴出来了,ie8测试通过
- 通过java代码生成图片、word文档、pdf文档
- JAVA 比较两张图片的相似度的代码(未测试)
- Java图片缩小后不失真代码
- 破解图片防盗链的代码(asp/php)测试通过
- windows下使用ffmpeg进行视频转码,图片拉取的Java测试代码
- Java阶段性测试--第四五六大题参考代码
- MongoDB之Java测试代码(DAO层)
- js+CSS 图片等比缩小并垂直居中实现代码
- 分别用Java、Scala、spark-shell开发wordcount程序及测试代码
- Java 利用jquery库cropper完成图片裁剪功能,经测试可用!