- 浏览: 483966 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (301)
- Swing技术 (1)
- Linux (1)
- Javascript (22)
- 数据结构和算法 (3)
- J2SE (36)
- workflow (5)
- 设计模式 (14)
- web service (19)
- Ajax (14)
- 中间件 & 服务器 (8)
- 多线程 (9)
- Oracle (52)
- sys & soft (10)
- JMS (3)
- sso (9)
- android (11)
- struts2 (10)
- web协议 (2)
- 分布式 (2)
- PM (2)
- OLAP (3)
- Redis (2)
- Hibernate (7)
- ibatis (2)
- SQLServer (1)
- maven (3)
- Spring (7)
- Jsp (2)
- slf4j (1)
- jQuery (15)
- 权限 (1)
- 系统集成 (1)
- 笔记 (1)
- Freemarker (2)
- 项目管理 (1)
- eclipse (3)
- GIS (1)
- NoSql (3)
- win10 (1)
- win10网络 (2)
- 底层 (3)
- 数据库 (0)
最新评论
-
kabuto_v:
请问那种图,uml图是怎么画出来的呢?是您自己手工画的,还是有 ...
FastJSON 序列化、反序列化实现 -
梦行Monxin商城系统:
电商实例、业务并发、网站并发及解决方法 -
rockethj8:
client 㓟有一个参数是可以忽略一些URL 不进行验证登录 ...
SSO 之 (单点登录)实施中遇到的几个问题 -
mengxiangfeiyan:
好啊。。。。。
Oracle删除表,删除数据以及恢复数据、利用现有表创建新表
http://shensy.iteye.com/blog/1621651
目前一些社交型互联网应用都有一些上传图片(例如头像,照片等)对预览图进行剪裁的功能。前一段时间在工作也遇到这个问题,总结一下基本实现步骤及代码(包含图片放大,缩小,设置品质,对指定点区域剪裁功能),使用JPEG格式图片测试通过,其它格式图片尚未验证。
一、基本步骤:
1.将图片文件的InputStream转换为ImageReader,并从ImageReader中读取BufferedImage信息.
2.然后使用javax.image包以及Java image scaling开源项目对图片进行缩放.
3.使用java.awt.image类对java.awt.BufferedImage进行剪裁.
4.最后写入文件,如果是JPG图片可以设置图片品质(压缩比)即JPEGEncodeParam.setQuality.
二、程序相关:
Java代码
/**
* 剪裁图片.
*
* @param file 要剪裁的图片
* @param scale 放大缩小比率
* @param cropX x轴起点坐标
* @param cropY y轴起点坐标
* @param targetWidth 目标图片的长
* @param targetHeight 目标图片的宽
*/
public static File crop(File file, Double scale, int cropX, int cropY, int targetWidth, int targetHeight) throws IOException {
BufferedImage source;
String format;
InputStream is = null;
try {
is = new FileInputStream(file);
// 从InputStream中读取图片流信息
ImageInputStream iis = ImageIO.createImageInputStream(is);
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
return null;
}
ImageReader reader = (ImageReader) iter.next();
ImageReadParam param = reader.getDefaultReadParam();
reader.setInput(iis, true, true);
try {
source = reader.read(0, param);
format = reader.getFormatName();
} finally {
reader.dispose();
iis.close();
}
} finally {
IOUtils.closeQuietly(is);
}
//调整放大缩小比率
int width = Double.valueOf(scale * source.getWidth()).intValue();
int height = Double.valueOf(scale * source.getHeight()).intValue();
BufferedImage scaled = scale(source, width, height);
//剪裁图片
ImageFilter filter = new CropImageFilter(cropX, cropY, targetWidth, targetHeight);
Image cropped = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(scaled.getSource(), filter));
//渲染新图片
BufferedImage image = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.drawImage(cropped, 0, 0, null);
g.dispose();
//写入文件
return writeToTempFile(image, format);
}
其中用到了Java image scaling开源工具,对图片进行缩放。
Java代码
/**
* 放大缩小图片到指定宽和高
*
* @param image Image to scale
* @param width Width of image
* @param height Height of image
* @return Scaled image file
*/
public static BufferedImage scale(BufferedImage image, int width, int height) {
ResampleOp resampleOp = new ResampleOp(width, height);
resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal);
return resampleOp.filter(image, null);
}
最后写入临时文件:
Java代码
/**
* 将图片写入临时文件
*/
public static File writeToTempFile(BufferedImage image, Format type) {
if (Format.JPEG != type) {
return writeToTempFileWithoutCompress(image, type);
} else {
try {
return compress(image, JPG_DEFAULT_QUALITY);
} catch (IOException e) {
return writeToTempFileWithoutCompress(image, type);
}
}
}
不是JPEG格式不压缩:
Java代码
/**
* 不压缩将图片写入文件
*/
public static File writeToTempFileWithoutCompress(BufferedImage image, Format type) {
File destination = generateTempFile(type);
try {
ImageIO.write(image, type.toString(), destination);
} catch (IOException e) {
throw new RuntimeException(e);
}
return destination;
}
/**
* 压缩图片到指定的压缩比率
*/
public static File compress(BufferedImage image, float quality) throws IOException {
// Build param
JPEGEncodeParam param = null;
try {
param = JPEGCodec.getDefaultJPEGEncodeParam(image);
param.setQuality(quality, false);
} catch (RuntimeException e) {
// Ignore
param = null;
}
// Build encoder
File destination = generateTempFile(Format.JPEG);
FileOutputStream os = null;
try {
os = FileUtils.openOutputStream(destination);
JPEGImageEncoder encoder;
if (param != null) {
encoder = JPEGCodec.createJPEGEncoder(os, param);
} else {
encoder = JPEGCodec.createJPEGEncoder(os);
}
encoder.encode(image);
} finally {
IOUtils.closeQuietly(os);
}
return destination;
}
其中还用到了Apache的commons-io工具集。
测试时发现设置0.9以上的压缩比后会使有些JPG图片的大小不减小反而比原图更大了,具体原因还不太清楚。
希望对看到的人有所帮助。
目前一些社交型互联网应用都有一些上传图片(例如头像,照片等)对预览图进行剪裁的功能。前一段时间在工作也遇到这个问题,总结一下基本实现步骤及代码(包含图片放大,缩小,设置品质,对指定点区域剪裁功能),使用JPEG格式图片测试通过,其它格式图片尚未验证。
一、基本步骤:
1.将图片文件的InputStream转换为ImageReader,并从ImageReader中读取BufferedImage信息.
2.然后使用javax.image包以及Java image scaling开源项目对图片进行缩放.
3.使用java.awt.image类对java.awt.BufferedImage进行剪裁.
4.最后写入文件,如果是JPG图片可以设置图片品质(压缩比)即JPEGEncodeParam.setQuality.
二、程序相关:
Java代码
/**
* 剪裁图片.
*
* @param file 要剪裁的图片
* @param scale 放大缩小比率
* @param cropX x轴起点坐标
* @param cropY y轴起点坐标
* @param targetWidth 目标图片的长
* @param targetHeight 目标图片的宽
*/
public static File crop(File file, Double scale, int cropX, int cropY, int targetWidth, int targetHeight) throws IOException {
BufferedImage source;
String format;
InputStream is = null;
try {
is = new FileInputStream(file);
// 从InputStream中读取图片流信息
ImageInputStream iis = ImageIO.createImageInputStream(is);
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
return null;
}
ImageReader reader = (ImageReader) iter.next();
ImageReadParam param = reader.getDefaultReadParam();
reader.setInput(iis, true, true);
try {
source = reader.read(0, param);
format = reader.getFormatName();
} finally {
reader.dispose();
iis.close();
}
} finally {
IOUtils.closeQuietly(is);
}
//调整放大缩小比率
int width = Double.valueOf(scale * source.getWidth()).intValue();
int height = Double.valueOf(scale * source.getHeight()).intValue();
BufferedImage scaled = scale(source, width, height);
//剪裁图片
ImageFilter filter = new CropImageFilter(cropX, cropY, targetWidth, targetHeight);
Image cropped = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(scaled.getSource(), filter));
//渲染新图片
BufferedImage image = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.drawImage(cropped, 0, 0, null);
g.dispose();
//写入文件
return writeToTempFile(image, format);
}
其中用到了Java image scaling开源工具,对图片进行缩放。
Java代码
/**
* 放大缩小图片到指定宽和高
*
* @param image Image to scale
* @param width Width of image
* @param height Height of image
* @return Scaled image file
*/
public static BufferedImage scale(BufferedImage image, int width, int height) {
ResampleOp resampleOp = new ResampleOp(width, height);
resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal);
return resampleOp.filter(image, null);
}
最后写入临时文件:
Java代码
/**
* 将图片写入临时文件
*/
public static File writeToTempFile(BufferedImage image, Format type) {
if (Format.JPEG != type) {
return writeToTempFileWithoutCompress(image, type);
} else {
try {
return compress(image, JPG_DEFAULT_QUALITY);
} catch (IOException e) {
return writeToTempFileWithoutCompress(image, type);
}
}
}
不是JPEG格式不压缩:
Java代码
/**
* 不压缩将图片写入文件
*/
public static File writeToTempFileWithoutCompress(BufferedImage image, Format type) {
File destination = generateTempFile(type);
try {
ImageIO.write(image, type.toString(), destination);
} catch (IOException e) {
throw new RuntimeException(e);
}
return destination;
}
/**
* 压缩图片到指定的压缩比率
*/
public static File compress(BufferedImage image, float quality) throws IOException {
// Build param
JPEGEncodeParam param = null;
try {
param = JPEGCodec.getDefaultJPEGEncodeParam(image);
param.setQuality(quality, false);
} catch (RuntimeException e) {
// Ignore
param = null;
}
// Build encoder
File destination = generateTempFile(Format.JPEG);
FileOutputStream os = null;
try {
os = FileUtils.openOutputStream(destination);
JPEGImageEncoder encoder;
if (param != null) {
encoder = JPEGCodec.createJPEGEncoder(os, param);
} else {
encoder = JPEGCodec.createJPEGEncoder(os);
}
encoder.encode(image);
} finally {
IOUtils.closeQuietly(os);
}
return destination;
}
其中还用到了Apache的commons-io工具集。
测试时发现设置0.9以上的压缩比后会使有些JPG图片的大小不减小反而比原图更大了,具体原因还不太清楚。
希望对看到的人有所帮助。
发表评论
-
底层读写-NIO实战
2016-03-04 10:51 792最近参与实现底层高并发处理的项目,接收socket ... -
底层网络读写
2016-03-04 10:49 528Java NIO:NIO概述 http://w ... -
java方法返回多个值
2016-01-22 09:11 722http://www.iteye.com/topic/114 ... -
模拟form表单上传图片
2016-01-19 17:23 714转自:http://blog.csdn.net/5iasp ... -
java包装类的几点注意
2016-01-18 10:52 761java中8种基本类型变量对应的包装类: ... -
JAVA调用聚合天气api接口示例
2016-01-15 09:42 3974http://www.iteye.com/topic/114 ... -
java对象序列化过程
2016-01-05 10:22 694一、 http://bbs.csdn.net/topi ... -
Java6 枚举常见7种用法
2014-02-09 23:02 810http://blog.csdn.net/shimiso/a ... -
异常 之 异常体系
2013-10-07 23:00 864J2EE系统异常的处理准则 ... -
异常 之 finally的特殊处理
2013-10-07 22:26 863http://java.chinaitlab.com/expe ... -
cookie 之 读写操作
2013-08-25 12:07 856//写cookie Cookie nameco ... -
让frameset居中
2013-07-25 19:59 0JSPHTML 平常我个人开发页面时,一般用width ... -
session生命周期
2013-07-23 08:40 0http://blog.sina.com.cn/s/blog_ ... -
转发和重定向的区别
2013-07-23 08:30 0http://www.2cto.com/kf/201107/9 ... -
NIO 之 BIO和NIO机制和socket
2013-07-14 18:38 1369https://www.ibm.com/developerwo ... -
win7下安装配置tomcat,java运行环境
2013-06-30 19:39 0http://www.cnblogs.com/pannysp/ ... -
JSP之 操作Cookie
2013-06-12 18:09 0Cookie应该是一 ... -
Socket实战之二 多线程通信
2013-05-31 15:15 0http://blog.csdn.net/kongxx/art ... -
Socket实战之七 使用Socket通信传输文件
2013-05-31 15:10 838http://blog.csdn.net/kongxx/art ... -
Socket实战之六 使用NIO包实现Socket通信
2013-05-31 15:06 1114本文地址:http://blog.csdn.net/kongx ...
相关推荐
Java和C#的实现图片头像裁剪功能(含Flex、Flash、js、JQuery版)Java和C#的实现图片头像裁剪功能(含Flex、Flash、js、JQuery版)Java和C#的实现图片头像裁剪功能(含Flex、Flash、js、JQuery版)Java和C#的实现图片头像...
NULL 博文链接:https://qihaha.iteye.com/blog/2068868
java上传并裁剪图片,功能已经实现,非常完美
用java jsp实现图片上床功能,及对图片的剪裁、显示,代码清晰易懂,初学者可以借鉴一下。
jquery Jcrop 是一个功能强大的 jQuery 图像裁剪插件,结合后端程序(例如:java)可以快速的实现图片裁剪的功能
华为图像服务场景智能设计Java示例代码,可实现24种滤镜效果、图文智能排版、贴纸花字、图像主题标签、图片剪裁功能。场景智能设计的优势 :提供24种独具特色的颜色滤镜,实现场景化智能设计。提供图文智能排版功能,...
Java简单实现类似“全能扫描王”的功能,使用OpenCV实现。项目中包含了opencv_java343.dll动态链接库,在dll文件夹中,需要在eclipse中引入
之前做用户档案的照片上传时一直在研究做一个可以在线编辑图片的功能,网上看了很多资料都是flash+php实现的,java还没有比较好的实现。找了很多资料,整理了...这里提供了前台的图片预览和后台java图片剪裁的代码。
github上第一个第三方开源的图片修剪截取利器,功能强大,设计良好,提供了非常丰富的图片截取修剪功能,涵盖常用的基本需求功能,如图片的按比例截取(4:3,16:9,7:5等等)、截取成圆形、自由裁剪、锁定比例裁剪、...
通常注册账户上传用户图像时需要进行预览,这篇文章就是教我们如何用 Java 实现图片裁剪预览功能,需要的朋友可以参考下
主要介绍了java实现的图片裁剪功能,涉及java针对图片的读取、转换、保存等相关操作技巧,需要的朋友可以参考下
图片浏览小程序是一个基于Java和Applet技术实现的简单图片查看器,旨在提供用户浏览本地图片的功能。以下是该小程序的一些关键特点和功能: 系统架构: 单机应用:该小程序为单机应用,不涉及客户端-服务器通信,...
主要介绍了使用Java代码在Android中实现图片裁剪功能,许多应用都需要此类从相册中选取图片然后编辑的功能,需要的朋友可以参考下
java实现的图片处理,主要功能图片缩放,按比例缩放,截剪
华为图像服务场景智能设计Kotlin示例代码,可实现24种滤镜效果、图文智能排版、贴纸花字、图像主题标签、图片剪裁功能。场景智能设计的优势 :提供24种独具特色的颜色滤镜,实现场景化智能设计。提供图文智能排版功能...
Thumbnailator 是一个优秀的图片处理的Google开源Java类库。运用最少的代码实现你想要的功能,支持的处理操作:图片缩放,区域裁剪,水印,旋转,保持比例等,支持对一个目录的所有图片进行批量处理操作
Jcrop实现图片上传裁剪功能,包含前后端代码,可直接发布运行,后端代码为java
主要介绍了Java实现的不同图片居中剪裁生成同一尺寸缩略图功能,涉及java针对图片的读取、属性修改等相关操作技巧,需要的朋友可以参考下
框架自研了一套个性化的组件,实现了可插拔的组件式开发方式:单图上传、多图上传、下拉选择、开关按钮、单选按钮、多选按钮、图片裁剪、富文本编辑器等等一系列个性化、轻量级的组件,是一款真正意义上实现组件化...
源码中有说明文档,按照文档...源码中包括有视频合成,视频加背景音乐,多图片+音频合并为视频,音频合并,视频加字幕,视频去声音,视频格式转换,视频播放,视频裁剪,获取音频或视频信息,定义视频大小,视频截图