展会信息港展会大全

java 自动生成略缩图 单图生成略缩图和批量生成略缩图
来源:互联网   发布日期:2016-01-26 10:11:09   浏览:1834次  

导读:本篇文章,在前辈的经验基础上,分别对单图生成略缩图和批量生成略缩图做个小结当你要做一个图库的项目时,对图片大� ⑾袼氐目刂剖鞘紫刃枰?饩龅哪烟狻R弧⒌ネ忌?陕运跬嫉ネ季??匦禄嬷疲??尚碌耐计?P� ...

本篇文章,在前辈的经验基础上,分别对单图生成略缩图和批量生成略缩图做个小结

当你要做一个图库的项目时,对图片大孝像素的控制是首先需要解决的难题。

一、单图生成略缩图

单图经过重新绘制,生成新的图片。新图可以按一定比例由旧图缩小,也可以规定其固定尺寸。

详细代码如下:

复制代码 代码如下:

<SPAN style="FONT-SIZE: 14px">import com.sun.image.codec.jpeg.JPEGImageEncoder;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGEncodeParam;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.util.HashMap;

import java.util.List;

import java.util.ArrayList;

import java.io.File;

import java.io.IOException;

import java.io.FileOutputStream;

import java.util.Map;

public class PicChange {

/**

* @param im原始图像

* @param resizeTimes需要缩小的倍数,缩小2倍为原来的1/2 ,这个数值越大,返回的图片越小

* @return返回处理后的图像

*/

public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {

/*原始图像的宽度和高度*/

int width = im.getWidth();

int height = im.getHeight();

/*调整后的图片的宽度和高度*/

int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);

int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);

/*新生成结果图片*/

BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);

result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);

return result;

}

/**

* @param im原始图像

* @param resizeTimes倍数,比如0.5就是缩小一半,0.98等等double类型

* @return返回处理后的图像

*/

public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {

/*原始图像的宽度和高度*/

int width = im.getWidth();

int height = im.getHeight();

/*调整后的图片的宽度和高度*/

int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);

int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);

/*新生成结果图片*/

BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);

result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);

return result;

}

public boolean writeHighQuality(BufferedImage im, String fileFullPath) {

try {

/*输出到文件流*/

FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);

JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);

/* 压缩质量 */

jep.setQuality(1f, true);

encoder.encode(im, jep);

/*近JPEG编码*/

newimage.close();

return true;

} catch (Exception e) {

return false;

}

}

public static void main(String[] args) throws Exception{

String inputFoler = "F:pic" ;

/*这儿填写你存放要缩小图片的文件夹全地址*/

String outputFolder = "F:picNew";

/*这儿填写你转化后的图片存放的文件夹*/

float times = 0.25f;

/*这个参数是要转化成的倍数,如果是1就是转化成1倍*/

PicChange r = new PicChange();

File ff = new File("F:picChrysanthemum1.jpg");

BufferedImage f = javax.imageio.ImageIO.read(ff);

r.writeHighQuality(r.zoomImage(f,times), outputFolder);

}

}</SPAN>

当你把上面的代码移至myEclipse时,可能会在引入一下工具包时出错。

复制代码 代码如下:

<SPAN style="FONT-SIZE: 14px">import com.sun.image.codec.</SPAN>

解 决方法:只要把Windows - Preferences - Java - Compiler - Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

二、批量生成略缩图

批量生成略缩图,即将已知文件夹中后缀为.jpg 或其他图片后缀名的文件统一转化后 放到 已定的另外文件夹中

复制代码 代码如下:

<SPAN style="FONT-SIZE: 14px">import com.sun.image.codec.jpeg.JPEGImageEncoder;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGEncodeParam;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.util.HashMap;

import java.util.List;

import java.util.ArrayList;

import java.io.File;

import java.io.IOException;

import java.io.FileOutputStream;

import java.util.Map;

public class ResizeImage {

/**

* @param im原始图像

* @param resizeTimes需要缩小的倍数,缩小2倍为原来的1/2 ,这个数值越大,返回的图片越小

* @return返回处理后的图像

*/

public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {

/*原始图像的宽度和高度*/

int width = im.getWidth();

int height = im.getHeight();

/*调整后的图片的宽度和高度*/

int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);

int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);

/*新生成结果图片*/

BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);

result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);

return result;

}

/**

* @param im原始图像

* @param resizeTimes倍数,比如0.5就是缩小一半,0.98等等double类型

* @return返回处理后的图像

*/

public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {

/*原始图像的宽度和高度*/

int width = im.getWidth();

int height = im.getHeight();

/*调整后的图片的宽度和高度*/

int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);

int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);

/*新生成结果图片*/

BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);

result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);

return result;

}

/**

* @param path要转化的图像的文件夹,就是存放图像的文件夹路径

* @param type图片的后缀名组成的数组

* @return

*/

public List<BufferedImage> getImageList(String path, String[] type) throws IOException{

Map<String,Boolean> map = new HashMap<String, Boolean>();

for(String s : type) {

map.put(s,true);

}

List<BufferedImage> result = new ArrayList<BufferedImage>();

File[] fileList = new File(path).listFiles();

for (File f : fileList) {

if(f.length() == 0)

continue;

if(map.get(getExtension(f.getName())) == null)

continue;

result.add(javax.imageio.ImageIO.read(f));

}

return result;

}

/**

* 把图片写到磁盘上

* @param im

* @param patheg: C://home// 图片写入的文件夹地址

* @param fileName DCM1987.jpg写入图片的名字

* @return

*/

public boolean writeToDisk(BufferedImage im, String path, String fileName) {

File f = new File(path + fileName);

String fileType = getExtension(fileName);

if (fileType == null)

return false;

try {

ImageIO.write(im, fileType, f);

im.flush();

return true;

} catch (IOException e) {

return false;

}

}

public boolean writeHighQuality(BufferedImage im, String fileFullPath) {

try {

/*输出到文件流*/

FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);

JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);

/* 压缩质量 */

jep.setQuality(1f, true);

encoder.encode(im, jep);

/*近JPEG编码*/

newimage.close();

return true;

} catch (Exception e) {

return false;

}

}

/**

* 返回文件的文件后缀名

* @param fileName

* @return

*/

public String getExtension(String fileName) {

try {

return fileName.split(".")[fileName.split(".").length - 1];

} catch (Exception e) {

return null;

}

}

public static void main(String[] args) throws Exception{

String inputFoler = "F:pic" ;

/*这儿填写你存放要缩小图片的文件夹全地址*/

String outputFolder = "F:picNew";

/*这儿填写你转化后的图片存放的文件夹*/

float times = 0.25f;

/*这个参数是要转化成的倍数,如果是1就是转化成1倍*/

ResizeImage r = new ResizeImage();

List<BufferedImage> imageList = r.getImageList(inputFoler,new String[] {"jpg"});

for(BufferedImage i : imageList) {

r.writeHighQuality(r.zoomImage(i,times),outputFolder);

}

}

}</SPAN>

赞助本站

人工智能实验室

相关热词: 生成略缩图 java

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港