java 上传图片压缩图片
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 图片识别 tess4j_JAVA使用Tess4J进行ocr识别
- java.lang.RuntimeException: Can not toast on a thread that has not called Looper.prepare()终极解决方案
- MySQL字段类型如何转为java_Java JDBC中,MySQL字段类型到JAVA类型的转换
- 一致性哈希算法 Java实现
- java 文件上传到服务器_Java上传文件到服务器端的方法「建议收藏」
- JAVA实验室设备管理系统代码_java做一个简单学生管理系统
- JAVA为啥要用stream流处理_java哪个部分最难学
- vscode配置java环境变量_配置Java
- 杨辉三角形(二维坐标基础题)——Java-二维数组版本
- 图像处理Java工具类详解编程语言
- java JNI: C 语言调用 Java 方法示例详解编程语言
- Java上传带图片的Http请求详解编程语言
- Java学习笔记之三java中的变量和常量详解编程语言
- Java ftp上传文件方法效率对比详解编程语言
- 开发开创Linux更美好的Java开发环境(linux集成java)
- 数据处理Java程序实现Redis中过期数据的处理(redisjava过期)
- 数据处理Java操作Redis处理过期数据的方法(redisjava过期)
- 使用Java连接MySQL实现查询功能(java连接mysql查询)
- Java如何在Linux下运行?(java执行linux)
- 管理Linux下Java版本管理:轻松实现多版本切换(linux下java版本)
- Java和Oracle技术融合,汇聚智能新大陆(java oracle包)
- JAVA/JSP学习系列之一
- Java中一些基础概念的使用详解
- java从输入流中获取数据并返回字节数组示例