最近项目中需要判断上传的图片必须是png,jpg,gif三种格式的图片,并且当图片的宽度大于600px时,压缩图片至600px,并且等比例的压缩图片的高度。

具体的实现形式:

大致的思路是:

  1. 判断根据文件名判断图片的格式是否是png,jpg,gif三种图片文件   定义了 isImageFile 方法
  2. 如果是的,则进入到scaleImage(String imgSrc, String imgDist)方法中判断图片大小,如果图片大小合适,则直接利用copyFile(File fromFile, File toFile)方法复制图片
  3. 在缩放图片中利用到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中判断图片格式并且等比例压缩图片的更多相关文章

  1. JAVA中判断年月日格式是否正确(支持判断闰年的2月份)

    一.先说一下年月日(yyyy-MM-dd)正则表达式: 1.年月日正则表达式:^((19|20)[0-9]{2})-((0?2-((0?[1-9])|([1-2][0-9])))|(0?(1|3|5| ...

  2. java中判断一个字符串是否“都为数字”和“是否包含数字”和“截取数字”

    在javascript中有一个方法isDigit()使用来判断一个字符串是否都是数字,在java的字符串处理方法中没有这样的方法,觉得常常需要用到,于是上网搜了一下,整理出了两个用正则表达式匹配的判断 ...

  3. JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson

    java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...

  4. java中各种时间格式的转化

    http://www.chinaitpower.com/A/2005-01-14/104881.html 使用java.util.Calendar返回间隔天数         static int g ...

  5. (转载)java中判断字符串是否为数字的方法的几种方法

    java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...

  6. 处理页面载入图片js(等比例压缩图片)

    第一页面html  <div class="admin">${answer.content}</div> <div class="admin ...

  7. 等比例压缩图片到指定的KB大小

    基本原理: 取原来的图片,长宽乘以比例,重新生成一张图片,获取这张图片的大小,如果还是超过预期大小,继续在此基础上乘以压缩比例,生成图片,直到达到预期 /** * @获取远程图片的体积大小 单位byt ...

  8. 最新javascript自动按比例显示图片,按比例压缩图片显示

    最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  9. java中判断两个字符串是否相等的问题

    我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...

随机推荐

  1. cf623A. Graph and String(二分图 构造)

    题意 题目链接 Sol 可以这样考虑,在原图中没有边相连的点的值肯定是a / c 那么直接二分图染色即可 #include<bits/stdc++.h> #define LL long l ...

  2. phoenix使用vue--单独js(不使用app.js)

    实际中不能都在一个js里 api.js app.js admin.js --vue 后台 记录下方法 static--admin--hello.js import "phoenix_html ...

  3. Keras & Theano 输出中间层结果

    Keras & Theano get output of an intermediate layer 1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要 ...

  4. 格式化字符串漏洞利用实战之 njctf-decoder

    前言 格式化字符串漏洞也是一种比较常见的漏洞利用技术.ctf 中也经常出现. 本文以 njctf 线下赛的一道题为例进行实战. 题目链接:https://gitee.com/hac425/blog_d ...

  5. 【iOS开发】在ARC项目中使用非ARC文件

    ARC的出现应该说是开发者的一大福利,苹果是推荐使用的,但是因为之前没有ARC机制,好多比较好的类库都是使用的非ARC,或是有些大牛还是不喜欢用ARC,封装的类也是非ARC的,想要在自己的ARC项目中 ...

  6. mybatis 在存储Integer、bigdecimal等java数据类型时,将0存成null

    我们的项目中,有关于金额的计算,所以,一般在java环境中我们使用bigdecimal来做运算和存储金额信息.数据库sqlServer2008用的float类型 问题是,当我将金额赋值成0时,很意外的 ...

  7. mysql的日期函数介绍

    仅供参考 DAYOFWEEK(date)  返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03 ...

  8. logback.xml配置示例

    需要的jar如下: <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api< ...

  9. 如何将同一 VNET 下的虚拟机从经典部署模型迁移到 Azure Resource Manager

    本文内容 适用场景 解决方案 适用场景 用户拥有多个云服务但是在同一个 VNET 下,希望将这些虚拟机从经典部署模型(以下简称:ASM)迁移到 Azure Resource Manager(以下简称: ...

  10. Apache下开启SSI配置,使html支持include包含

    有的时候,我们的页面有公共的导航栏navbar,公共的脚注footer,那么我们就想把这些公共部分独立成一个html文件,在要引用的地方像引用js,css一样,给包含进来. Apache下开启SSI配 ...