【问题标题】:Retrieve images from a folder, Scale down them, save them into another folder. How? (Java)从文件夹中检索图像,缩小它们,将它们保存到另一个文件夹中。如何? (爪哇)
【发布时间】:2010-11-26 09:36:29
【问题描述】:

我有一个小问题...一个文件夹中有数百张图像。他们应该缩小到特定的高度和特定的宽度。所以,问题是......从文件夹中检索图像,缩小它们,然后将它们保存到另一个文件夹中。 Java可以吗?如果是,请帮助我,如何?这仅用于减少我的应用程序中的内存大小。 (所有文件应为 JPEG 格式)。

【问题讨论】:

  • 请注意,通过缩放 JPEG 的像素大小,您在“字节减少”方面的收获非常小。通常保持大小不变​​并增加 JPEG 压缩会更有效。

标签: java image


【解决方案1】:

可以使用 ImageIO 读取和 Image,然后使用 Image.getScaledInstance() 方法对图像进行缩放。最后你可以使用 ImageIO 写出新的图像。

糟糕,我不确定当您使用 getScaledInstance() 方法时会返回什么类型的 Image。 ImageIO.write() 方法需要一个 RenderedImage,因此您可能需要先创建一个 BufferedImage 并将缩放后的图像绘制到缓冲区上,然后再写出 BufferedImage。

【讨论】:

    【解决方案2】:

    我的解决方案提供了图像,但所有图像都有固定大小,但我需要它们的大小以适合我的高度和宽度的特定比例。如果有任何修改,请帮助我..

    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;
        }
    }
    

    添加评论

    【讨论】:

    • 您明确地将图像缩放到 150x180 - 这与您的问题一致,但在这个答案中,您似乎是在说您希望它们按特定比例缩小。如果这就是您的意思,那么您可以使用一个因子来计算它,而不是大小的内联常量 - 例如 originalWidth/20。顺便说一句,请注意扩展名可以是任何情况,因此您可能应该使用“jpg”.equalsIgnoreCase(这当然意味着您必须使用 lastIndexOf 找到扩展名)。祝你好运。
    • 就像另一个 FYI 一样,您可能希望在代码中使用“\\”的地方使用 File.separator。使用它可以让您不必知道斜杠在您的操作系统中的使用方式。
    猜你喜欢
    • 1970-01-01
    • 2020-09-06
    • 2020-07-01
    • 2016-05-08
    • 2020-04-26
    • 2015-03-04
    • 1970-01-01
    • 2019-07-24
    • 2020-04-15
    相关资源
    最近更新 更多