java图片缩放
package com.rubekid.springmvc.utils; import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection; import javax.imageio.ImageIO; import net.sf.jmimemagic.MagicException;
import net.sf.jmimemagic.MagicMatchNotFoundException;
import net.sf.jmimemagic.MagicParseException; import org.apache.commons.io.FileUtils;
import org.summercool.image.AnimatedGifEncoder;
import org.summercool.image.GifDecoder;
import org.summercool.image.Scalr;
import org.summercool.image.Scalr.Method;
import org.summercool.image.Scalr.Mode; import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder; import eu.medsea.mimeutil.MimeUtil; /**
* 图片压缩处理
* @author Rubekid
*
*/
public class ImageScale { public static final String TYPE_JPEG = "jpg"; public static final String TYPE_PNG = "png"; public static final String TYPE_GIF = "gif"; public static final String TYPE_BMP = "bmp"; /**
* 图片缩放
* @param srcFile
* @param destFile
* @param maxWidth
* @param maxHeight
* @param type
* @param quality
* @throws IOException
*/
public static void resize(File srcFile, File destFile, int maxWidth, int maxHeight, String type, float quality) throws IOException{
if(TYPE_GIF.equals(type)){
resizeGif(srcFile, destFile, maxWidth, maxHeight);
}
else if(TYPE_PNG.equals(type)){
resizePng(srcFile, destFile, maxWidth, maxHeight);
}
else{
resizeJpeg(srcFile, destFile, maxWidth, maxHeight, quality);
}
} /**
* 图片缩放
* @param srcFile
* @param destFile
* @param maxWidth
* @param maxHeight
* @param type
* @throws IOException
*/
public static void resize(File srcFile, File destFile, int maxWidth, int maxHeight, String type) throws IOException{
resize(srcFile, destFile, maxWidth, maxHeight, type, 0.8f);
} /**
* 图片缩放
* @param srcFile
* @param destFile
* @param maxWidth
* @param maxHeight
* @throws Exception
*/
public static void resize(File srcFile, File destFile, int maxWidth, int maxHeight, float quality) throws Exception{
resize(srcFile, destFile, maxWidth, maxHeight, getType(srcFile), quality);
} /**
* 图片缩放
* @param srcFile
* @param destFile
* @param maxWidth
* @param maxHeight
* @throws Exception
*/
public static void resize(File srcFile, File destFile, int maxWidth, int maxHeight) throws Exception{
resize(srcFile, destFile, maxWidth, maxHeight, getType(srcFile));
} /**
* JPEG图片缩放
* @param srcFile
* @param destFile
* @param maxWidth
* @param maxHeight
* @throws IOException
* @throws ImageFormatException
*/
public static void resizeJpeg(File srcFile, File destFile, int maxWidth, int maxHeight, float quality) throws IOException{
BufferedImage image = ImageIO.read(srcFile); double scale = 0.8f;
AffineTransform atf = AffineTransform.getTranslateInstance(scale, scale); //AffineTransformOp affineTransformOp = new AffineTransformOp(xform, interpolationType) double rate = getResizeRate(srcFile, maxWidth, maxHeight);
int width = (int)(image.getWidth() * rate);
int height =(int) (image.getHeight() * rate);
image = Scalr.resize(image, Method.AUTOMATIC, Mode.AUTOMATIC, width, height);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.drawImage(image, 0, 0, null);
image = bufferedImage; FileOutputStream out = new FileOutputStream(destFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(quality, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(image);
} /**
* PNG图片缩放
* @param srcFile
* @param destFile
* @param maxWidth
* @param maxHeight
* @throws IOException
*/
public static void resizePng(File srcFile, File destFile, int maxWidth, int maxHeight) throws IOException{
BufferedImage bufferedImage = ImageIO.read(srcFile);
double rate = getResizeRate(srcFile, maxWidth, maxHeight);
if(rate == 1){
FileUtils.copyFile(srcFile, destFile);
}
else{
int width = (int)(bufferedImage.getWidth() * rate);
int height =(int) (bufferedImage.getHeight() * rate);
BufferedImage result = Scalr.resize(bufferedImage,Method.AUTOMATIC, Mode.AUTOMATIC, width, height);
ImageIO.write(result, "png", destFile);
}
} /**
* GIF图片缩放
* @param srcFile
* @param destFile
* @param maxWidth
* @param maxHeight
* @throws IOException
*/
public static void resizeGif(File srcFile, File destFile, int maxWidth, int maxHeight) throws IOException{
GifDecoder gd = new GifDecoder();
int status = gd.read(new FileInputStream(srcFile));
if (status != GifDecoder.STATUS_OK) {
return;
}
double rate = getResizeRate(srcFile, maxWidth, maxHeight);
if(rate == 1){
FileUtils.copyFile(srcFile, destFile);
}
else{
AnimatedGifEncoder ge = new AnimatedGifEncoder();
ge.start(new FileOutputStream(destFile));
ge.setRepeat(0);
for (int i = 0; i < gd.getFrameCount(); i++) {
BufferedImage frame = gd.getFrame(i);
int width = frame.getWidth();
int height = frame.getHeight();
width = (int) (width * rate);
height = (int) (height * rate); BufferedImage rescaled = Scalr.resize(frame, Mode.FIT_EXACT, width, height);
int delay = gd.getDelay(i);
ge.setDelay(delay);
ge.addFrame(rescaled);
}
ge.finish();
}
} /**
* 获取缩放比例
* @param srcFile
* @param MaxWidth
* @param MaxHeight
* @return
* @throws IOException
*/
public static double getResizeRate( File srcFile, int maxWidth, int maxHeight) throws IOException{
double rate = 1;
if(maxWidth==0 && maxHeight == 0){
return rate;
}
BufferedImage bImage = ImageIO.read(srcFile);
int width = bImage.getWidth();
int height = bImage.getHeight();
if(maxWidth == 0 && height > maxHeight){
rate = (double)maxHeight / height;
}
else if(maxHeight == 0 && width > maxWidth){
rate = (double)maxWidth / width;
}
else if((width > maxWidth || height > maxHeight) && maxWidth > 0 && maxHeight > 0){
rate = (double)maxWidth / width > (double)maxHeight / height ? (double)maxHeight / height : (double)maxWidth / width ;
}
if(rate > 1){
rate = 1;
}
return rate;
}
/**
*
* @param file
* @return
* @Exception
*/
public static String getMimeType(File file) throws Exception{
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
Collection<?> mimeTypes = MimeUtil.getMimeTypes(file);
return mimeTypes.toString();
} /**
* 获取图片类型(后缀)
* @param file
* @return
* @throws MagicException
* @throws MagicMatchNotFoundException
* @throws MagicParseException
*/
public static String getType(File file) throws Exception{
String mimeType = getMimeType(file);
return getType(mimeType);
} /**
* 获取图片类型(后缀)
* @param mimeType
* @return
*/
public static String getType(String mimeType){
if("image/gif".equals(mimeType)){
return TYPE_GIF;
}
else if("image/png".equals(mimeType) || "image/x-png".equals(mimeType)){
return TYPE_PNG;
}
else if("image/bmp".equals(mimeType) || "image/x-ms-bmp".equals(mimeType)){
return TYPE_BMP;
}
return TYPE_JPEG;
}
}
<dependency>
<groupId>com.mortennobel</groupId>
<artifactId>java-image-scaling</artifactId>
<version>0.8.5</version>
</dependency> <dependency>
<groupId>eu.medsea.mimeutil</groupId>
<artifactId>mime-util</artifactId>
<version>2.1.3</version>
</dependency> <dependency>
<groupId>jmimemagic</groupId>
<artifactId>jmimemagic</artifactId>
<version>0.1.2</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>simpleimage</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
java图片缩放的更多相关文章
- java图片缩放与裁剪
import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io ...
- java 图片缩放
使用java自带的图片处理api,也可以使用(GraphicsMagick + im4j) import java.awt.Image; import java.awt.image.BufferedI ...
- Java图片工具类,完成图片的截取和任意缩放
package com.common.util; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Renderin ...
- java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。
java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...
- java 图片压缩 缩放
废话不多说,直接上代码,静态方法可直接调用,中间用流来处理的 /** * 图片缩放(未考虑多种图片格式和等比例缩放) * @param filePath 图片路径 * @param height 高度 ...
- java 图片处理工具类
import java.awt.Image; import java.awt.Rectangle; import java.awt.geom.AffineTransform; import ja ...
- Java 图片转换为字符图 CharMaps (整理)
/* * Java 图片转换成字符图 CharMaps (整理) * * 2016-1-2 深圳 南山平山村 曾剑锋 * * @(#)CharMaps.java 2014/1/16 * 1.这个一 ...
- java,图片压缩,略缩图
在网上找了两个图片的缩放类,在这里分享一下: package manager.util; import java.util.Calendar; import java.io.File; import ...
- android关于图片缩放
网上有许多关于图片缩放的demo,本人都感觉不怎么好用,最近在github看到了 一个简单的支持多指缩放图片的Android View类 gesture-imageview (地址:https://g ...
随机推荐
- 在Linux-0.11中实现基于内核栈切换的进程切换
原有的基于TSS的任务切换的不足 进程切换的六段论 1 中断进入内核 2 找到当前进程的PCB和新进程的PCB 3 完成PCB的切换 4 根据PCB完成内核栈的切换 5 切换运行资源LDT 6 利用I ...
- 仿猪八戒一个提示(jQuery插件) v0.1 beta
先看下效果 js jQuery.extend({ prompt: function (text, type, times) { var prompt = $(['<div class=" ...
- python文件处理
python中对文件处理需要涉及到os模块和shutil模块得到当前工作目录路径:os.getcwd()获取指定目录下的所有文件和目录名:os.listdir(dir)删除文件:os.remove(f ...
- 初窥struts2(二)OGNL表达式
Struts2总结 Struts2完整的处理流程: 1 客户端发送请求,交给struts2控制器(StrutsPrepareAndExecuteFilter). 2 Filter控制器进行请求过滤 ...
- 构建高可用web站点学习(三)
分布式的构建 做为网站访问的生命线(数据访问),当然也可以采用分布式的方法来减轻单台服务器的访问压力.之前有讲过Memcached的分布式,但是Memcached服务器互不通信,所以我们也提过redi ...
- Redis系列(1)之安装
Redis系列(1)之安装 由于项目的需要,最近需要研究下Redis.Redis是个很轻量级的NoSql内存数据库,它有多轻量级的呢,用C写的,源码只有3万行,空的数据库只占1M内存.它的功能很丰富, ...
- Chapter 2. OpenSSL的安装和配置学习笔记
Chapter 2. OpenSSL的安装和配置学习笔记 2.1 在linux上面安装OpenSSL我还是做点No paper事情比较在行,正好和老师的课程接轨一下.以前尝试过在Windows上面安装 ...
- Java 保留两位小数
在实际项目开发中,经常会存在浮点数四舍五入保留几位小数的问题,故收集了几种常用方法: 直接上代码(保留两位小数). Format.java: import java.math.BigDecimal; ...
- 【Oracle】Windows 7下完全卸载Oracle 11g数据库
闲来无事,想把Oracle 11g重装一下,记录如下: (1)首先在服务中停止所有的Oracle服务: (2)开始 -> 程序 -> Oracle-OraDb11g_home1 - ...
- 最小费用最大流MCMF zkw费用流
稀疏图慢死了...但是稠密图效果还是很好的 struct MCMF{ struct tedge{int x,y,cap,w,next;}adj[maxm];int ms,fch[maxn]; int ...