java中判断图片格式并且等比例压缩图片
最近项目中需要判断上传的图片必须是png,jpg,gif三种格式的图片,并且当图片的宽度大于600px时,压缩图片至600px,并且等比例的压缩图片的高度。
具体的实现形式:
大致的思路是:
- 判断根据文件名判断图片的格式是否是png,jpg,gif三种图片文件 定义了 isImageFile 方法
- 如果是的,则进入到scaleImage(String imgSrc, String imgDist)方法中判断图片大小,如果图片大小合适,则直接利用copyFile(File fromFile, File toFile)方法复制图片
- 在缩放图片中利用到java.awt里面的几个类,并且利用BufferedImage可以加快图片的压缩速度。
package com.ctbri.test; import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*; public class PictureChange { static String suffix = ""; public static void main(String[] args) {
String newfilebase = "E:/A_xia_program/image/";
File file = new File(newfilebase + 1);
File[] oldfiles = file.listFiles();
for (File file2 : oldfiles) {
if (isImageFile(file2)) {
if (!scaleImage(newfilebase + 1 + "/" + file2.getName(), newfilebase + 11 + "/" + file2.getName())) {
System.out.println(file2.getName()+"转化成功!");
}
}
}
} public static boolean isImageFile(File file) { String fileName = file.getName(); //获取文件名的后缀,可以将后缀定义为类变量,共后面的函数使用
suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); // 声明图片后缀名数组
if (!suffix.matches("^[(jpg)|(png)|(gif)]+$")) {
System.out.println("请输入png,jpg,gif格式的图片");
return false;
}
return true;
} public static boolean scaleImage(String imgSrc, String imgDist) {
try {
File file = new File(imgSrc);
if (!file.exists()) {
return false;
} InputStream is = new FileInputStream(file);
Image src = ImageIO.read(is);
if (src.getWidth(null) <= 600) {
File tofile = new File(imgDist);
copyFile(file, tofile);
is.close();
return true;
} //获取源文件的宽高
int imageWidth = ((BufferedImage) src).getWidth();
int imageHeight = ((BufferedImage) src).getHeight(); double scale = (double) 600 / imageWidth; //计算等比例压缩之后的狂傲
int newWidth = (int) (imageWidth * scale);
int newHeight = (int) (imageHeight * scale); BufferedImage newImage = scaleImage((BufferedImage) src, scale, newWidth, newHeight); File file_out = new File(imgDist);
ImageIO.write(newImage, suffix, file_out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
} //用于具体的转化
public static BufferedImage scaleImage(BufferedImage bufferedImage, double scale, int width, int height) {
int imageWidth = bufferedImage.getWidth();
int imageHeight = bufferedImage.getHeight();
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale, scale);
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR); return bilinearScaleOp.filter(bufferedImage, new BufferedImage(width, height, bufferedImage.getType()));
} //复制文件
public static void copyFile(File fromFile, File toFile) throws IOException {
FileInputStream ins = new FileInputStream(fromFile);
FileOutputStream out = new FileOutputStream(toFile);
byte[] b = new byte[1024];
int n = 0;
while ((n = ins.read(b)) != -1) {
out.write(b, 0, n);
}
ins.close();
out.close();
}
}
java中判断图片格式并且等比例压缩图片的更多相关文章
- JAVA中判断年月日格式是否正确(支持判断闰年的2月份)
一.先说一下年月日(yyyy-MM-dd)正则表达式: 1.年月日正则表达式:^((19|20)[0-9]{2})-((0?2-((0?[1-9])|([1-2][0-9])))|(0?(1|3|5| ...
- java中判断一个字符串是否“都为数字”和“是否包含数字”和“截取数字”
在javascript中有一个方法isDigit()使用来判断一个字符串是否都是数字,在java的字符串处理方法中没有这样的方法,觉得常常需要用到,于是上网搜了一下,整理出了两个用正则表达式匹配的判断 ...
- JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson
java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...
- java中各种时间格式的转化
http://www.chinaitpower.com/A/2005-01-14/104881.html 使用java.util.Calendar返回间隔天数 static int g ...
- (转载)java中判断字符串是否为数字的方法的几种方法
java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...
- 处理页面载入图片js(等比例压缩图片)
第一页面html <div class="admin">${answer.content}</div> <div class="admin ...
- 等比例压缩图片到指定的KB大小
基本原理: 取原来的图片,长宽乘以比例,重新生成一张图片,获取这张图片的大小,如果还是超过预期大小,继续在此基础上乘以压缩比例,生成图片,直到达到预期 /** * @获取远程图片的体积大小 单位byt ...
- 最新javascript自动按比例显示图片,按比例压缩图片显示
最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- java中判断两个字符串是否相等的问题
我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...
随机推荐
- 使用JSON实现分页
使用JSON实现分页可直接用 Fenye.html <!DOCTYPE html> <html> <head> <title>JSON分页</ti ...
- 数组原型方法调用及函数apply调用时 类数组参数在IE8下的问题
当函数以 apply 方式调用时, 传参方式是一个由各个参数组成的数组或类数组(一个有length属性的对象),传入参数个数取决于 length 的值,例如,某个对象 args.length=3; a ...
- EOS下控制台以及图形界面打印sql语句
EOS下控制台以及图形界面打印sql语句 场景需求:在eos中打印sql语句,包括数据实体,查询实体和命名sql的sql语句. 所需资源: P6spy:负责拦截sql,并打印. Sqlprofiler ...
- 【Android】Retrofit网络请求Service,@Path、@Query、@QueryMap...
对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了. 下面分为GET.POST.DELETE还有PUT的请求,说明@Path.@Query.@QueryMap.@Bo ...
- Android批量打包提速 - 1分钟900个市场不是梦
版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4152323.html 黎明前的黑暗 使用Ant或者Gradl ...
- 树莓派raspberry Pi2 介绍
Compared to the Raspberry Pi 1 it has: A 900MHz quad-core ARM Cortex-A7 CPU 1GB RAM Like the (Pi 1) ...
- 线性代数的视角理解LSR(least square regression)的参数评估算法本质
https://medium.com/@andrew.chamberlain/the-linear-algebra-view-of-least-squares-regression-f67044b7f ...
- apache2.2+php5.3+mysql5.5+Zend Guard Loader集成包
由前一篇文章 http://www.cnblogs.com/darktime/p/3407980.html 我就配置了一个环境包,免安装的,只需要运行一个.bat的文件文件就算安装成功了 如果你需要用 ...
- .net下log4net的使用
这里以控制台应用程序为例 首先是要添加引用: 安装后可以看到项目中多了log4net的引用: 添加应用程序配置文件app.config,配置log4net <?xml version=" ...
- 浅析NSTextContainer
浅析NSTextContainer TextKit中的NSTextContainer有点晦涩难懂,如果想用TextKit实现文本分页的效果,你是必须要使用NSTextContainer的...... ...