package com.bitspace.flame.util;

import java.io.File;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageZipUtil{

/**
* 等比例压缩图片文件<br> 先保存原文件,再压缩、上传
* @param oldFile 要进行压缩的文件
* @param newFile 新文件
* @param width 宽度 //设置宽度时(高度传入0,等比例缩放)
* @param height 高度 //设置高度时(宽度传入0,等比例缩放)
* @param quality 质量
* @return 返回压缩后的文件的全路径
*/
public static String zipImageFile(File oldFile, File newFile, int width, int height, float quality) {
if (oldFile == null) {
return null;
}
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(oldFile);
int w = srcFile.getWidth(null);
// System.out.println(w);
int h = srcFile.getHeight(null);
// System.out.println(h);
double bili;
if(width>0){
bili=width/(double)w;
height = (int) (h*bili);
}else{
if(height>0){
bili=height/(double)h;
width = (int) (w*bili);
}
}
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
//String filePrex = oldFile.getName().substring(0, oldFile.getName().indexOf('.'));
/** 压缩后的文件名 */
//newImage = filePrex + smallIcon+ oldFile.getName().substring(filePrex.length());

/** 压缩之后临时存放位置 */
FileOutputStream out = new FileOutputStream(newFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}

/**
* 按宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传
* @param oldFile 要进行压缩的文件全路径
* @param newFile 新文件
* @param width 宽度
* @param height 高度
* @param quality 质量
* @return 返回压缩后的文件的全路径
*/
public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height, float quality) {
if (oldFile == null) {
return null;
}
String newImage = null;
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(oldFile);
int w = srcFile.getWidth(null);
// System.out.println(w);
int h = srcFile.getHeight(null);
// System.out.println(h);

/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
//String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
/** 压缩后的文件名 */
//newImage = filePrex + smallIcon+ oldFile.substring(filePrex.length());

/** 压缩之后临时存放位置 */
FileOutputStream out = new FileOutputStream(newFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
}

public static void main(String args[]) throws IOException {
System.out.println(ImageZipUtil.zipWidthHeightImageFile(new File("E:/图片/1.jpg"),new File("E:/图片/1-1.jpg"), 300, 480, 5f));
}

}

java 上传图片压缩图片的更多相关文章

  1. 使用java来压缩图片

    使用java来压缩图片,简单几句,清清爽爽 使用0.3的压缩比得到的结果如下(从2.8M压缩到268K,且图片的清晰度看不出明显差别): package carlspringtest; import ...

  2. java流压缩图片

    整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享.首先,要压缩的图片格式不能说动态图片,你可以使用bmp.png.gif等,至于压缩质量,可以通过BufferedImage来指定 ...

  3. Java 后端压缩图片

    import java.io.*;import java.util.Date;import java.awt.*;import java.awt.image.*;import javax.imagei ...

  4. java 上传图片压缩

    public static void uploadFile(MultipartFile multfile, String filePath) throws Exception { File targe ...

  5. java 上传图片 并压缩图片大小

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...

  6. java 上传图片 并压缩图片大小(转)

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...

  7. java上传图片并压缩图片大小

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...

  8. java实现上传图片并压缩图片大小功能

    缩略图压缩文件jar包 <!-- 图片缩略图 --> <dependency> <groupId>net.coobird</groupId> <a ...

  9. Java压缩图片

    阅读目录 前言 压缩的要求 实现 优点 其他功能 前言 作为靠谱的java服务端程序员,图片这个事情一直是个头疼的事情. 现在很多网站上,都有上传图片这个功能,而图片对于现在的很多手机来说,拍摄出来的 ...

随机推荐

  1. 使用CefSharp在.Net程序中嵌入Chrome浏览器(六)——调试

    chrome强大的调试功能令许多开发者爱不释手,在使用cef的时候,我们也可以继承这强大的开发者工具. 集成调试: 我们可以使用如下函数直接使用集成在chrome里的开发者工具 _chrome.Sho ...

  2. LPC43xx SGPIO Experimentation

    LPC4350 SGPIO Experimentation The NXP LPC43xx microcontrollers have an interesting, programmable ser ...

  3. .Net Discovery 系列之一--string从入门到精通(上)

    string是一种很特殊的数据类型,它既是基元类型又是引用类型,在编译以及运行时,.Net都对它做了一些优化工作,正式这些优化工作有时会迷惑编程人员,使string看起来难以琢磨,这篇文章分上下两章, ...

  4. Noip2005谁拿了最多的奖学金题解

    题解 题目本身没什么好说的. 只是一道普及组的题让我领悟到scanf()读字符的真谛.scanf()函数最奇异的功能就是控制串里除格式化字符串之外的字符.若匹配成功则舍去. 所以我们能够"精 ...

  5. 在ASP.NET MVC4中使用Quartz.NET执行定时任务

    本篇在ASP.NET MVC下实践使用Quartz.NET执行定时任务. 首先通过NuGet安装Quartz.NET. 使用Quartz.NET的大致思路是:1.实现IJob接口,定义具体要做的事情2 ...

  6. CentOS MongoDB 高可用实战

    原文:https://www.sunjianhua.cn/archives/centos-mongodb.html 一.MongoDB 单节点 1.1.Windows 版安装 1.1.1 获取社区版本 ...

  7. 清理tomcat服务器缓存

    据悉,2014年最流行的应用服务器排行榜揭晓Tomcat仍然处于领先位置.41%的部署使用的是Tomcat,和2013年的43%的市场份额数据一 致.下面还是我们的热门选择Jetty和JBoss/Wi ...

  8. window消息机制

    剖析Windows消息处理机制 前一段,帮人写了个小控件,又温习了一遍Windows消息处理机制,现在把一些知识点总结出来,供大家参考.1.窗口    Windows程序是由一系列的窗口构成的,每个窗 ...

  9. MySql错误处理(二) - Condition & Handle

    20.2.10. 条件和处理程序 20.2.10.1. DECLARE条件 20.2.10.2. DECLARE处理程序 特定条件需要特定处理.这些条件可以联系到错误,以及子程序中的一般流程控制. 2 ...

  10. 实用ExtJS教程100例-005:自定义对话框Ext.MessageBox.show

    我们对ExtJS对话框进行了三篇演示: MessageBox的三种用法 进度条对话框Ext.MessageBox.progress 等待对话框Ext.MessageBox.wait 通过上面三篇内容的 ...