package com.lz.hr_picture.helper;
import java.awt.Graphics2D;
import java.awt.Rectangle;
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.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
/**
* 图片工具类,完成图片的截取
*
* @author penghuaiyi
* @date 2010-11-10
*/
public class ImageHepler {
/**
* 实现图像的等比缩放
* @param source
* @param targetW
* @param targetH
* @return
*/
private static BufferedImage resize(BufferedImage source, int targetW,
int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
// 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
// 则将下面的if else语句注释即可
if (sx < sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { // handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
// smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
/**
* 实现图像的等比缩放和缩放后的截取
* @param inFilePath 要截取文件的路径
* @param outFilePath 截取后输出的路径
* @param width 要截取宽度
* @param hight 要截取的高度
* @param proportion
* @throws Exception
*/
public static void saveImageAsJpg(String inFilePath, String outFilePath,
int width, int hight, boolean proportion)throws Exception {
File file = new File(inFilePath);
InputStream in = new FileInputStream(file);
File saveFile = new File(outFilePath);
BufferedImage srcImage = ImageIO.read(in);
if (width > 0 || hight > 0) {
// 原图的大小
int sw = srcImage.getWidth();
int sh = srcImage.getHeight();
// 如果原图像的大小小于要缩放的图像大小,直接将要缩放的图像复制过去
if (sw > width && sh > hight) {
srcImage = resize(srcImage, width, hight);
} else {
String fileName = saveFile.getName();
String formatName = fileName.substring(fileName
.lastIndexOf('.') + 1);
ImageIO.write(srcImage, formatName, saveFile);
return;
}
}
// 缩放后的图像的宽和高
int w = srcImage.getWidth();
int h = srcImage.getHeight();
// 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取
if (w == width) {
// 计算X轴坐标
int x = 0;
int y = h / 2 - hight / 2;
saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
}
// 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取
else if (h == hight) {
// 计算X轴坐标
int x = w / 2 - width / 2;
int y = 0;
saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
}
in.close();
}
/**
* 实现缩放后的截图
* @param image 缩放后的图像
* @param subImageBounds 要截取的子图的范围
* @param subImageFile 要保存的文件
* @throws IOException
*/
private static void saveSubImage(BufferedImage image,
Rectangle subImageBounds, File subImageFile) throws IOException {
if (subImageBounds.x < 0 || subImageBounds.y < 0
|| subImageBounds.width - subImageBounds.x > image.getWidth()
|| subImageBounds.height - subImageBounds.y > image.getHeight()) {
System.out.println("Bad subimage bounds");
return;
}
BufferedImage subImage = image.getSubimage(subImageBounds.x,subImageBounds.y, subImageBounds.width, subImageBounds.height);
String fileName = subImageFile.getName();
String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
ImageIO.write(subImage, formatName, subImageFile);
}
public static void main(String[] args)throws Exception{
ImageHepler.saveImageAsJpg("E:\\证书图片\\1.jpg", "E:\\pics\\1.jpg", 600, 400, true);
}
}
分享到:
相关推荐
java 字符串截取工具类java 字符串截取工具类
java裁剪、缩放图片工具类。含工具类和一个测试类。
C# 图片工具类 上传 缩放 裁剪 水印 生成缩略图
图片缩放工具图片缩放工具图片缩放工具图片缩放工具
java 处理图片大小等比例缩放;图片裁剪;支持图片压缩不失真;支持图片添加水印;支持动态图的处理;支持自定义图片大小的转换 等 压缩包中内附使用说明
用于图片的双击放大或缩小,以及两指滑动缩放。
MP3截取工具MP3截取工具MP3截取工具MP3截取工具MP3截取工具
两种方式进行图片的裁剪、缩放以及图形验证码,从项目抽取,所以验证是没问题的
本工具类(Android)通过传入ImageView来简单实现单指拖动图片和双指放大缩小的功能,代码中几乎对每行代码都有注释。
阿里巴巴 simpleimage 图片处理工具类 功能 等比例缩放 裁切 加水印 复合操作例子和jar包
图片切割,图片缩放,图片黑白处理,gif图片制作,gif图片分解,等图片处理工具类 在 OperateImage 类中包含所有工具方法
图片处理工具类,能缩放图片,给图片打水印等。
图片压缩缩放处理工具,支持自定义缩放功能。可以批量操作
将视频一帧截取为一张图片用于用于预览
实现功能: 叠加图片;图片压缩(支持MultipartFile),添加文本–可根据textWidth参数自动换行,支持行间距、字间距设置;...图片截取;图片高质量缩放;图片png格式缩放;图片克隆副本;读取图片对象;图片输出 ……
基于jquery的图片截取工具
这是个工具类,可以实现图片的等比缩放,坑根据图片百分比,或者固定尺寸等进行缩放,还可以给图片增加水印,很好用
FastStone是一款极好用的图像浏览、编辑和截屏工具,支持 BMP、JPG、JPEG、GIF、PNG、TIFF、WMF、ICO 和 TGA 在内的主流图片格式,其独有的光滑和毛刺处理技术让图片更加清晰,提供缩放、旋转、剪切、颜色调整功能。...
用过数码相机的人都知道,数码相机拍的相片像素都...今天给大家推荐的就是图片批量缩放工具,本人花了很长时间才做好,希望能给好些朋友带来方便。 介绍一下它的使用方法吧:启动程序,在工具菜单里有个图片批量缩放。