`
- 浏览:
49423 次
- 性别:
- 来自:
福州
-
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileNotFoundException;
import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** *//**
* @author zsy
*
*/
public class ZoomImage ...{
private static final Log log = LogFactory.getLog(ZoomImage.class);
public static String fileSeparator = System.getProperty("file.separator");
/** *//**
*
* @param filePath(E:\cfimg\userImg\NBCANH5PIOWX\9GZJO8QN1QWX.jpg)
* @param outPutDir(E:\cfimg\userImg\NBCANH5PIOWX\9GZJO8QN1QWX-60-60.jpg)
* @param height
* @param width
* @param replace 是否覆盖已有文件
* @return 生成文件名
* @throws Exception
* @throws FileNotFoundException
*/
public static void zoomImage(String filePath, String outPutFile,
int width, int height, boolean replace)
throws Exception, FileNotFoundException ...{
File inPutFile = new File(filePath);
File outPut = new File(outPutFile);
zoomImage(inPutFile, outPut, width, height, replace);
}
public static void zoomImage(File inPutFile, File outPutFile,
int width, int height, boolean replace)
throws Exception, FileNotFoundException ...{
if (!inPutFile.isFile()) ...{
log.error("文件不存在:" + inPutFile);
throw new FileNotFoundException("文件不存在:" + inPutFile);
}
if (!outPutFile.exists() || replace) ...{
zoomImage(inPutFile, outPutFile, height, width);
}
}
/** *//**
* 按指定大小缩放图片
* @param inPutFile
* @param outPutFile
* @param height
* @param width
* @throws Exception
*/
public static void zoomImage(File inPutFile, File outPutFile,
int width, int height) throws Exception ...{
BufferedImage source = ImageIO.read(inPutFile);
if (source == null) ...{
return;
}
double hx = (double)height / source.getHeight();
double wy = (double)width / source.getWidth();
if (hx < wy) ...{
wy = hx;
width = (int)(source.getWidth() * wy);
} else ...{
hx = wy;
height = (int)(source.getHeight() * hx);
}
int type = source.getType();
BufferedImage target = null;
if (type == BufferedImage.TYPE_CUSTOM) ...{ // handmade
ColorModel cm = source.getColorModel();
WritableRaster raster =
cm.createCompatibleWritableRaster(width, height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else ...{
target = new BufferedImage(width, height, type);
}
Graphics2D g = target.createGraphics();
// smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(wy, hx));
g.dispose();
try ...{
ImageIO.write(target, "JPEG", outPutFile);
} catch (Exception ex) ...{
ex.printStackTrace();
}
}
public static void main(String arg[]) throws Exception ...{
zoomImage("c:/V.gif", "c:/V_50_50.gif", 50, 50, false);
}
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
用ImageMagick java实现图片的缩放
图片处理继续 实现等比缩放 大小控制 使用java实现 希望对大家有帮助
单独放大缩小图片和移动图片的很多,很难找到能缩放和移动一起实现的例子。由于项目的需要就自己写了个java实现图片放大(以视图中心放大)、缩小、移动、鼠标拖移。
采用双线性算法实现图片的缩放,今天,经过多次实现终于完成了图片的缩放,来给大家分享一下
java实现图片的上传,根据需要缩小图片的宽度和高度显示出来!
这是个Java编写的批量图片缩放工具,自己觉得很实用,因为有时候相机照的图片太多,想缩放一下用电子邮件发送又不想劳烦ps这样的大师,用这款软件就可以轻松实现了,只要给它个装满图像文件的文件夹的地址,你就可以...
该程序可以实现指定文件夹下的全部图片大小的重新设定,可自己修改指定大小。
java 实现的图片浏览器 打开 上一张 下一张 删除等功能
图片操作类源代码,该类主要用于给图片进行缩放、加水印和得到图片的高、宽及类型。 具体操作请自己参照方法说明自己测试改写。
java实现的图片处理,主要功能图片缩放,按比例缩放,截剪
主要为大家详细介绍了java实现图片缩放、旋转和马赛克化,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
实现图片切割功能,适合初步认识使用
Java实现图片的拖拉变形放大缩小,四个方向都可以拉大缩小图片,技术方面,使用java.awt.geom 包中的AffineTransform 类来实现图片拖拉缩放功能。
这个缩放网上一堆,这里我下载下来了,也用了一下,挺不错和大家分享下
Java 压缩图片资源的算法demo,采用基本的java流操作实现!
完整的例子,对YUV图片进行缩放,代码里有几个缩放方法
这个小项目主要是利用java Thumbnaillator类库实现对图片的缩放功能,代码参考自慕课网Java实现图片等比例缩略图教学视频,目的为需求源码的小伙伴提供一个参考,如有问题大家再一起交流(附:资源积分是必选项,...
使用步骤和例子,压缩包中有,可以将大量图片转成pdf文件,用扫描仪扫到的图片转成pdf必用