简体   繁体   English

从文件夹中检索图像,缩小它们,将它们保存到另一个文件夹中。 怎么样? (JAVA)

[英]Retrieve images from a folder, Scale down them, save them into another folder. How? (Java)

I have a small problem... There are hundreds of images in a folder. 我有一个小问题......文件夹中有数百个图像。 they should scale down to specific height and specific width. 它们应缩小到特定高度和特定宽度。 so, the problem is... Retrieving images from a folder, and scale down them, and save them into another folder. 所以,问题是......从文件夹中检索图像,缩小它们,然后将它们保存到另一个文件夹中。 Is it possible with Java? 是否可以使用Java? if yes, please help me, how? 如果有,请帮帮我,怎么样? This is only for reducing memory size in my application. 这仅用于减少应用程序中的内存大小。 ( all files should be in JPEG format). (所有文件都应为JPEG格式)。

You can use ImageIO to read and Image, then use the Image.getScaledInstance() method to scale the image. 您可以使用ImageIO读取和Image,然后使用Image.getScaledInstance()方法缩放图像。 Finally you can use ImageIO to write out the new image. 最后,您可以使用ImageIO写出新图像。

Oops, I'm not sure what type of Image gets returned when you use the getScaledInstance() method. 糟糕,我不确定在使用getScaledInstance()方法时返回什么类型的Image。 The ImageIO.write() method needs a RenderedImage, so you may need to create a BufferedImage first and paint the scaled image onto the buffer and then write out the BufferedImage. ImageIO.write()方法需要一个RenderedImage,因此您可能需要先创建一个BufferedImage并将缩放后的图像绘制到缓冲区上,然后写出BufferedImage。

My solution gave images but all images has fixed size, but i need their size in specific ratio which fits my height and width. 我的解决方案提供了图像,但所有图像都有固定的尺寸,但我需要它们的尺寸以适合我的高度和宽度的特定比例。 If any modification, help me.. 如有任何修改,请帮帮我..

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

add comment 添加评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从文件夹加载所有图像并从中命名图像 - Loading all images from folder & make named Images from them 我试图重命名照片以将其移动到另一个文件夹。 使File.renameTo(File)返回false的代码有什么问题? - I tried to rename photos to move them to another folder. What is wrong with my code that make File.renameTo(File) return false? 如何从资源文件夹中获取文件。 Spring框架 - How to get files from resources folder. Spring Framework 图片存储在HTTPS服务器上,如何检索它们? - Images stored on a HTTPS server, how to retrieve them? 如何从Java中分配给它们的值中检索枚举常量? - How to retrieve Enum Constants from values assigned to them in Java? 如何将现有文件夹添加到android并使用它们? - How to add existing folder to android and use them? 如何将文件保存到文件夹并在应用程序内查询,如果重命名,则不会丢失文件? - How to save files to a folder and query it within app, without the risk of losing them if they're renamed? 如何从项目资源文件夹获取图像路径并发送到Java Eclipse中的另一个类 - How to get images path from project resource folder and send to another class in java eclipse 如何将文件保存在 java 的文件夹中? - How to save file in folder on java? Java如何为按钮分配ID并检索它们? - Java how to assign id to button and retrieve them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM