java 自动生成略缩图示例代码
程序员文章站
2024-02-12 14:55:28
当你要做一个图库的项目时,对图片大小、像素的控制是首先需要解决的难题。一、单图生成略缩图单图经过重新绘制,生成新的图片。新图可以按一定比例由旧图缩小,也可以规定其固定尺寸。...
当你要做一个图库的项目时,对图片大小、像素的控制是首先需要解决的难题。
一、单图生成略缩图
单图经过重新绘制,生成新的图片。新图可以按一定比例由旧图缩小,也可以规定其固定尺寸。
详细代码如下:
复制代码 代码如下:
<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:\\pic\\chrysanthemum1.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 path eg: 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>