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 ...
随机推荐
- css points
<style type="text/css" rel="stylesheet">.a{ width:500px; height:400px;对放置图 ...
- ios移动端禁止双指缩放功能
在实际开发中,我们禁止缩放的实现方式: 1.meta设置: <meta name="viewport" content="width=device-width,h ...
- Dynamics 365Online 查询Web Api的请求WebUri
在on-premises版本中,获取weburi的方式是进设置-自定义项-开发人员资源中查看地址,但online版本中的地址会有些许的差异 online的开发者资源中的地址如下图,如果你在页面java ...
- CRM 安装过程 AD+SQL+CRM
AD: 通过服务器管理器添加域服务,配置域服务器域名为crm5.lab. 注意:使用高级模式安装. 说明:服务器是windows server 2003 那么就选windows server 2003 ...
- ew代理实战
前言 渗透内网代理必不可少,本文做个记录 正文 工具下载地址 http://rootkiter.com/EarthWorm/ ssocksd开启 socks5 代理 环境 代理:192.168.211 ...
- MongoDB数据库安装及配置环境(windows10系统)
windows10系统下MongoDB的安装及环境配置: MongoDB的安装 下载地址: https://www.mongodb.com/download-center (这是windows10环境 ...
- centos7.2+php7.2+nginx1.12.0+mysql5.7配置
一. 源码安装php7.2 选择需要的php版本 从 php官网: http://cn2.php.net/downloads.php 选择需要的php版本,选择.tar.gz 的下载包,点击进入,选择 ...
- springboot学习入门之一---基本了解
1springboot基本了解 1.1概述 Spring Boot不是一门新技术,本质上就是spring. 特性: 1) 零配置(或很少配置) 2) 四个核心:(ASCA) 3.1)自动配置:spri ...
- Vue2学习笔记:v-for指令
1.使用 <!DOCTYPE html> <html> <head> <title></title> <meta charset=&q ...
- JS弹出div简单样式
<div id="dialog" style="display:none;z-index:9999;position: absolute;border:1px so ...