当你要做一个图库的项目时,对图片大小、像素的控制是首先需要解决的难题。
一、单图生成略缩图
单图经过重新绘制,生成新的图片。新图可以按一定比例由旧图缩小,也可以规定其固定尺寸。
详细代码如下:
<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 {
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;
}
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);
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;
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 {
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;
}
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 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;
}
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);
newimage.close();
return true;
} catch (Exception e) {
return false;
}
}
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;
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>