我遇到一个小问题......文件夹中有数百张图片。它们应缩小到特定高度和特定宽度。所以,问题是......从文件夹中检索图像,缩小它们,然后将它们保存到另一个文件夹中。是否可以使用Java?如果有,请帮帮我,怎么样?这仅用于减少应用程序中的内存大小。 (所有文件都应采用JPEG格式)。
答案 0 :(得分:3)
您可以使用ImageIO读取和Image,然后使用Image.getScaledInstance()方法缩放图像。最后,您可以使用ImageIO写出新图像。
糟糕,我不确定在使用getScaledInstance()方法时返回什么类型的Image。 ImageIO.write()方法需要一个RenderedImage,因此您可能需要先创建一个BufferedImage,然后将缩放的图像绘制到缓冲区上,然后写出BufferedImage。
答案 1 :(得分:1)
我的解决方案提供了图像,但所有图像都有固定的尺寸,但我需要它们的尺寸以适合我的高度和宽度的特定比例。如果有任何修改,请帮帮我..
package vimukti.image;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImagesScaling {
File inputDir;
File outputDir;
File[] files;
public ImagesScaling(File srcFile, File destFile) throws Exception{
inputDir = srcFile;
outputDir = destFile;
class JPGFileFilter implements FileFilter {
public boolean accept(File pathname) {
if (pathname.getName().endsWith(".jpg"))
return true;
if (pathname.getName().endsWith(".jpeg"))
return true;
return false;
}
}
JPGFileFilter filter = new JPGFileFilter();
if(!outputDir.exists())
outputDir.mkdir();
if(inputDir.isDirectory()){
files = inputDir.listFiles(filter);
}
if(files != null)
scaling();
}
void scaling()throws Exception{
for(File f : files){
Image srcImage = ImageIO.read(f);
Image destImage = srcImage.getScaledInstance(150, 180, Image.SCALE_AREA_AVERAGING);
BufferedImage buffImg = toBufferedImage(destImage);
ImageIO.write(buffImg, "jpg",
new File(outputDir.getAbsolutePath() + "\\"+f.getName()));
}
}
void displayFiles(File dir){
System.out.println("dir: " + dir);
String[] files = dir.list();
System.out.println("Files....");
if(files != null)
for(String s : files)
System.out.println("\t" +dir.getAbsolutePath()+"\\"+ s);
System.out.println("end");
}
public static void main(String[] args)throws Exception {
ImagesScaling imgScale = new ImagesScaling(
new File("C:\\pictures"),
new File("C:\\ScaledePictures"));
imgScale.displayFiles(new File("C:\\ScaledPictures"));
}
////////////////////// Copied ////////////////////////////////////
// This method returns a buffered image with the contents of an image
public BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage)image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent Pixels
// Create a buffered image with a format that's compatible with the screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(
image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
}
添加评论