java改变图片文件尺寸
方式一:缩放图片(缺点:如果需要固定尺寸图片,那么会使得原图变形)
1.单个图片尺寸处理:
package test.common; import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.imageio.ImageIO; public class ImgTest {
private Image img;
private final static int WIDTH = 147;
private final static int HEIGHT = 136; /**
* 改变图片的大小到宽为size,然后高随着宽等比例变化
* @param is 上传的图片的输入流
* @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
* @param size 新图片的宽
* @param format 新图片的格式
* @throws IOException
*/
public static OutputStream resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
BufferedImage prevImage = ImageIO.read(is);
double width = prevImage.getWidth();
double height = prevImage.getHeight();
double percent = size/width;
int newWidth = (int)(width * percent);
int newHeight = (int)(height * percent);
BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
ImageIO.write(image, format, os);
os.flush();
is.close();
os.close();
ByteArrayOutputStream b = (ByteArrayOutputStream) os;
return os;
} public static void main(String[] args) {
try {
InputStream is = new FileInputStream(new File("download/qrcode_test.jpg"));
OutputStream os = new FileOutputStream(new File("download/qrcode_test_1.jpg"));
resizeImage(is, os, 10, "png");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
2.批量图片尺寸处理:
package com.test;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap; import javax.imageio.ImageIO;
public class ImgChange {
private Image img;
private final static int WIDTH = 147;
private final static int HEIGHT = 136; /**
* 改变图片的大小到宽为size,然后高随着宽等比例变化
* @param is 上传的图片的输入流
* @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
* @param size 新图片的宽
* @param format 新图片的格式
* @throws IOException
*/
public static OutputStream resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
BufferedImage prevImage = ImageIO.read(is);
double width = prevImage.getWidth();
double height = prevImage.getHeight();
int newWidth = 600;
int newHeight = 800;
BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
ImageIO.write(image, format, os);
os.flush();
is.close();
os.close();
//ByteArrayOutputStream b = (ByteArrayOutputStream) os;
return os;
} /**
* 读取某个目录下所有文件、文件夹
* @param path
* @return LinkedHashMap<String,String>
*/
public static LinkedHashMap<String,String> getFiles(String path) {
LinkedHashMap<String,String> files = new LinkedHashMap<String,String>();
File file = new File(path);
File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) {
if (!tempList[i].isDirectory()) {
files.put(tempList[i].getName(),tempList[i].getPath());
}
}
return files;
} public static void main(String[] args) {
try {
LinkedHashMap<String,String> files = getFiles("D:/img");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date beginDate = new Date();
System.out.println("开始:"+sdf.format(beginDate));
for (String fileName : files.keySet()) {
InputStream is = new FileInputStream(new File(files.get(fileName)));
OutputStream os = new FileOutputStream(new File("D:/imgCopy/"+fileName));
resizeImage(is, os, 10, "jpg");
}
Date endDate = new Date();
System.out.println("结束:"+sdf.format(endDate));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
方式二:裁剪图片(缺点:可能会使得图片展示不完全,只能取到图片中间部分的内容)
package com.test; import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.imageio.ImageIO; public class ImgCrop { /**
* 改变图片的大小到宽为size,然后高随着宽等比例变化
* @param is 上传的图片的输入流
* @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
* @param size 新图片的宽
* @param format 新图片的格式
* @throws IOException
*/
public static OutputStream resizeImage(BufferedImage bufferedimage,int widht,int height,OutputStream os, String format) throws IOException {
BufferedImage image = new BufferedImage(widht, height, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(bufferedimage, 0, 0, widht, height, null);
ImageIO.write(image, format, os);
os.flush();
os.close();
return os;
} /**
* * 裁剪图片方法 *
* @param bufferedImage 图像源 *
* @param startX 裁剪开始x坐标 *
* @param startY 裁剪开始y坐标 *
* @param endX 裁剪结束x坐标 *
* @param endY 裁剪结束y坐标 *
* @return
*/
public static BufferedImage cropImage(BufferedImage Image, int startX, int startY, int endX, int endY) {
int width = Image.getWidth();
int height = Image.getHeight();
if (startX == -1) {
startX = 0;
}
if (startY == -1) {
startY = 0;
}
if (endX == -1) {
endX = width - 1;
}
if (endY == -1) {
endY = height - 1;
}
BufferedImage result = new BufferedImage(endX - startX, endY - startY, 4);
for (int x = startX; x < endX; ++x) {
for (int y = startY; y < endY; ++y) {
int rgb = Image.getRGB(x, y);
result.setRGB(x - startX, y - startY, rgb);
}
}
return result;
} public static void main(String[] args) throws IOException {
//原始图片路径
String imagePath = "D:/image";
File imageFile = new File(imagePath);
File[] imageFiles = imageFile.listFiles();
for (File fimage : imageFiles) {
BufferedImage bufferedimage=ImageIO.read(fimage);
int width = bufferedimage.getWidth();
int height = bufferedimage.getHeight(); OutputStream os = new FileOutputStream(new File("D:/img/"+fimage.getName()));
resizeImage(bufferedimage,width,height,os,"jpg");
os.flush();
os.close();
} //过程图片路径
String path = "D:/img";
dealImg(path); String cachePath = "D:/imgCache";
File file = new File(cachePath);
File[] files = file.listFiles();
while(files.length>0) {
dealImg(cachePath);
file = new File(path);
files = file.listFiles();
}
} /**
* 处理图片
* @param path 文件夹路径
*/
private static void dealImg(String path) {
try {
File file = new File(path);
File[] files = file.listFiles();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date beginDate = new Date();
System.out.println(path+"begin:"+sdf.format(beginDate));
for (File f : files) {
BufferedImage bufferedimage=ImageIO.read(f);
Double width = (double) bufferedimage.getWidth();
Double height = (double) bufferedimage.getHeight();
if (width<900||height<1200) {
while (width<900||height<1200) {
width=width*1.1;
height=height*1.1;
}
OutputStream os = new FileOutputStream(new File("D:/imgCache/"+f.getName()));
resizeImage(bufferedimage,width.intValue(),height.intValue(),os,"jpg");
f.delete();
os.flush();
os.close();
continue;
}
//目标将图片裁剪成 宽900,高1200
if (width > 900) {
/*开始x坐标 开始y坐标 结束x坐标 结束y坐标*/
bufferedimage=cropImage(bufferedimage,(int) ((width - 900) / 2),0,(int) (width - (width-900) / 2),height.intValue());
if (height > 1200) {
bufferedimage=cropImage(bufferedimage,0,(int) ((height - 1200) / 2),900,(int) (height - (height - 1200) / 2) );
}
}else{
if (height > 1200) {
bufferedimage=cropImage(bufferedimage,0,(int) ((height - 1200) / 2),width.intValue(),(int) (height - (height - 1200) / 2) );
}
}
OutputStream os = new FileOutputStream(new File("D:/imgCopy/"+f.getName()));
ImageIO.write(bufferedimage, "jpg", os); //输出裁剪图片
f.delete();
os.flush();
os.close();
}
Date endDate = new Date();
System.out.println(path+"end:"+sdf.format(endDate));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
裁剪图片优化版:
package com.jeecg.util; import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.imageio.ImageIO; public class ImgCrop { /**
* 改变图片的大小到宽为size,然后高随着宽等比例变化
* @param is 上传的图片的输入流
* @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
* @param size 新图片的宽
* @param format 新图片的格式
* @throws IOException
*/
public static OutputStream resizeImage(BufferedImage bufferedimage,int widht,int height,OutputStream os, String format) throws IOException {
BufferedImage image = new BufferedImage(widht, height, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(bufferedimage, 0, 0, widht, height, null);
ImageIO.write(image, format, os);
os.flush();
os.close();
return os;
} /**
* * 裁剪图片方法 *
* @param bufferedImage 图像源 *
* @param startX 裁剪开始x坐标 *
* @param startY 裁剪开始y坐标 *
* @param endX 裁剪结束x坐标 *
* @param endY 裁剪结束y坐标 *
* @return
*/
public static BufferedImage cropImage(BufferedImage Image, int startX, int startY, int endX, int endY) {
int width = Image.getWidth();
int height = Image.getHeight();
if (startX == -1) {
startX = 0;
}
if (startY == -1) {
startY = 0;
}
if (endX == -1) {
endX = width - 1;
}
if (endY == -1) {
endY = height - 1;
}
BufferedImage result = new BufferedImage(endX - startX, endY - startY, 4);
for (int x = startX; x < endX; ++x) {
for (int y = startY; y < endY; ++y) {
int rgb = Image.getRGB(x, y);
result.setRGB(x - startX, y - startY, rgb);
}
}
return result;
} public static void main(String[] args) throws IOException {
totalImg(300,400);
} /**
* 所有图片调用总方法
*/
public static void totalImg(Integer wid,Integer hei) throws IOException, FileNotFoundException {
File file1 = new File("D:/image/");
if (!file1.exists()) {
file1.mkdirs();
}
File file2 = new File("D:/img/");
if (!file2.exists()) {
file2.mkdirs();
}
File file3 = new File("D:/imgCache/");
if (!file3.exists()) {
file3.mkdirs();
}
File file4 = new File("D:/imgCopy/");
if (!file4.exists()) {
file4.mkdirs();
}
//原始图片路径
String imagePath = "D:/image";
File imageFile = new File(imagePath);
File[] imageFiles = imageFile.listFiles();
for (File fimage : imageFiles) {
BufferedImage bufferedimage=ImageIO.read(fimage);
int width = bufferedimage.getWidth();
int height = bufferedimage.getHeight(); OutputStream os = new FileOutputStream(new File("D:/img/"+fimage.getName()));
resizeImage(bufferedimage,width,height,os,"jpg");
os.flush();
os.close();
} //过程图片路径
String path = "D:/img";
dealImg(path,wid,hei,1); String cachePath = "D:/imgCache";
File file = new File(cachePath);
File[] files = file.listFiles();
while(files.length>0) {
dealImg(cachePath,wid,hei,2);
file = new File(path);
files = file.listFiles();
}
} /**
* 处理图片
* @param path 文件夹路径
*/
private static void dealImg(String path,Integer wid,Integer hei,Integer type) {
try {
File file = new File(path);
File[] files = file.listFiles();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date beginDate = new Date();
System.out.println(path+"begin:"+sdf.format(beginDate));
for (File f : files) {
BufferedImage bufferedimage=ImageIO.read(f);
Double width = (double) bufferedimage.getWidth();
Double height = (double) bufferedimage.getHeight(); Double w = 0.00;
Double h = 0.00;
if (type==1) {
if (width<wid||height<hei) {
while (width<wid||height<hei) {
width=width*1.1;
height=height*1.1;
}
OutputStream os = new FileOutputStream(new File("D:/imgCache/"+f.getName()));
resizeImage(bufferedimage,width.intValue(),height.intValue(),os,"jpg");
f.delete();
os.flush();
os.close();
continue;
}else if (width>wid&&height>hei) {
while (width>wid&&height>hei) {
width=width*0.9;
height=height*0.9; if (width<wid||height<hei) {
if (width<=height) {
w = wid.doubleValue();
h = ((wid-width)/wid)*height+height;
}else{
h = hei.doubleValue();
w = ((hei-height)/hei)*width+width;
}
}else{
w = width;
h = height;
}
}
width = w;
height = h;
OutputStream os = new FileOutputStream(new File("D:/imgCache/"+f.getName()));
resizeImage(bufferedimage,width.intValue(),height.intValue(),os,"jpg");
f.delete();
os.flush();
os.close();
continue;
}
} //目标将图片裁剪成 宽900,高1200
if (width > wid) {
/*开始x坐标 开始y坐标 结束x坐标 结束y坐标*/
bufferedimage=cropImage(bufferedimage,(int) ((width - wid) / 2),0,(int) (width - (width-wid) / 2),height.intValue());
if (height > hei) {
bufferedimage=cropImage(bufferedimage,0,(int) ((height - hei) / 2),wid,(int) (height - (height - hei) / 2) );
}
}else{
if (height > hei) {
bufferedimage=cropImage(bufferedimage,0,(int) ((height - hei) / 2),width.intValue(),(int) (height - (height - hei) / 2) );
}
}
OutputStream os = new FileOutputStream(new File("D:/imgCopy/"+f.getName()));
ImageIO.write(bufferedimage, "jpg", os); //输出裁剪图片
f.delete();
os.flush();
os.close();
}
Date endDate = new Date();
System.out.println(path+"end:"+sdf.format(endDate));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java改变图片文件尺寸的更多相关文章
- java 改变图片的DPI
代码如下: public class test01 { private static int DPI = 300; public static void main(String[] args) { S ...
- java Swing 图片缓冲机制
java Swing 图片缓冲机制: 参考:http://jorneyr.iteye.com/blog/868858#comments package util; import java.awt.ge ...
- 使用Java合并图片、修改DPI
项目中有时候需要对图片进行DPI.合并.拼接等的处理: package com.snow.web.a_test; import java.awt.Graphics; import java.awt.i ...
- Android 使用ColorMatrix改变图片颜色
原文链接:http://blog.csdn.net/janice0529/article/details/49207939 ColorMatrix的颜色矩阵介绍 颜色矩阵M是一个5*4的矩阵,在And ...
- 使用thumbnailator不按照比例,改变图片的大小
我们在平时的开发中,偶尔也会遇到图片处理的问题,比如图片的压缩,按比例改变图片的大小,不按比例改变图片的大小等等. 如果要自己去开发这样一套工具,我觉得大多数人都是做不到的,所以还是学会站在巨人的肩膀 ...
- 使用java修改图片DPI
修改以后可以直接用PS打开看效果 全部使用rt下的类,无需下载其他jar包 import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.imag ...
- java服务器图片压缩的几种方式及效率比较
以下是测试了三种图片压缩方式,通过测试发现使用jdk的ImageIO压缩时间更短,使用Google的thumbnailator更简单,但是thumbnailator在GitHub上的源码已经停止维护了 ...
- 使用javascript改变图片路径
效果预览:http://keleyi.com/keleyi/phtml/jstexiao/16.htm 代码如下: <!DOCTYPE html> <html> <hea ...
- Java中图片压缩处理
原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
随机推荐
- spring注解注入:<context:component-scan>使用说明
spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 在XML中配置了 ...
- kettle新建资源库出错
之前在本地测试新建kettle资源库很顺利,但是在把本地数据迁移到服务器的过程中出现了问题,多次新建资源库失败,提示插入数据错误. 解决办法: 将要执行的sql语句复制出来,单独在Navicat下执行 ...
- Ajax 用法, 实现方法,JS原生与JQ实现
AJAX 详解 ajax是实现页面异步加载. 常用于, 前后端数据交互, 实现前端页面无刷新更改操作. 是web前端和后端使用者开发的必备使用技能~~ Ajax操作~ : 俗话原理 : 用俗话来 ...
- C语言基础05
二维数组的定义: 数据类型 数组名称 [ 常量表达式1 ] [ 常量表达式2 ] = {.....} int a[ 2 ][ 3 ] ={ {4,5,6}, {7,8,0}, //或者{7} 后面 ...
- 类加载器子系统——JVM之四
一.类加载器基本概念 顾名思义,类加载器(class loader)用来加载 Java 类到 Java 虚拟机中.一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源程序(.java ...
- ui原则
http://www.niushe.com/news/show-3683.html 设计师Joshua Porter发表了一篇文章——<Principles of User Interface ...
- js的体会
关于观察者模式的核心是: 回调函数, 传递函数名作为参数,或者是传递变量,然后调用其函数名. 关于闭包的核心是 闭包的函数是全局变量之下的函数, 而非闭包的函数是局部变量. <script> ...
- Linux进程间通信——使用命名管道
在前一篇文章——Linux进程间通信——使用匿名管道中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关的的进程 ...
- 利用COM组件IPicture读取jpg、gif、bmp图片文件数据和显示图片
1.读取图片数据 函数原型:bool LoadImage(const char *pName, unsigned char *pBitData); 函数功能,读取pName指向的图片文件的位图数据 b ...
- 三校联考 Day3
三校联考 Day3 大水题 题目描述:给出一个圆及圆上的若干个点,问两个点间的最远距离. solution 按极角排序,按顺序枚举,显然距离最远的点是单调的,线性时间可解出答案. 大包子的束缚 题目描 ...