About Image Resize

Asked 2 years ago, Updated 2 years ago, 33 views

I am a beginner in java.Currently, I would like to write a program that captures images, resizes them to the specified size, and returns them in BUfferedImage, but I don't know what to do.I don't want to reduce the image quality as much as possible, but I'm having a hard time because I'm not familiar with the image and drawing system.I would appreciate it if you could let me know if there are any websites that give advice or explain how samples and images work in an easy-to-understand way.Thank you for your cooperation.

java

2022-09-30 16:24

3 Answers

I don't know about the image quality, but I think it's easy to do it with a standard library.

https://qiita.com/tool-taro/items/1f414424b31a86e97446
http://dotnsf.blog.jp/archives/1062006362.html

I took a look around .

What about programs like the following?Replace the file name and size accordingly.

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;

public class test {
    public static void main(String[]args) rows IOException {
        BufferedImage bi=scaleImage(new File("src.jpg"), 100, 150;
        // The second argument is the format of the image, and the third argument is the filename.
        ImageIO.write(bi, "jpeg", new File("dest.jpg"));
    }

    /**
     * 
     * @paramin Files to Load
     * @param destWidth Maximum size next to output image
     * @param destHeight Maximum vertical size of the image to be output
     * @return BufferedImage
     * @throws IOException
     */
    public static bufferedImage scaleImage(File in, int destWidth, int destHeight) rows IOException {
        BufferedImage src=ImageIO.read(in);

        int width = src.getWidth(); // original image width
        int height = src.getHeight(); // Height of the original image

        // Decide the scale based on the aspect ratio.
        double widthScale=(double)destWidth/(double)width;
        double heightScale=(double)destHeight/(double)height;
        doublescale=widthScale<heightScale?widthScale:heightScale;

        ImageFilter filter = new AreaAveragingScaleFilter(
            (int)(src.getWidth()*scale), (int)(src.getHeight()*scale);
        ImageProducer p = new FilteredImageSource(src.getSource(), filter);
        Image dstImage=Toolkit.getDefaultToolkit().createImage(p);
        BufferedImage dst = new BufferedImage(
            dstImage.getWidth(null), dstImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics 2Dg = dst.createGraphics();
        g.drawImage(dstImage,0,0,null);
        g.dispose();
        return dst;
    }
}


2022-09-30 16:24

You wanted to know the calculation method and process, so I'm not specializing in image processing, so I'll just explain how it works.

Considering how the images are drawn in the first place, I think resizing is relatively simple.
To put it bluntly, an image of a canvas with square squares like a square paper, painted one square at a time and viewed from a distance.(It's easy to understand when you think of something like a dot picture)

When resizing this, if you change the size of the mass applied to all squares evenly, the image size will change.
How do you change the size of the trout? You just have to increase the number of squares you put on.(And I will prepare a canvas for that size separately.)
You want to maintain the aspect ratio, so when you change the size of the square, you should be able to handle it by changing the aspect ratio while maintaining the aspect ratio.For example, if you want to resize an image with a length of 10 cm and a width of 30 cm, you can change the size of the mass by a ratio of vertical:horizontal=1:3 to each mass.

However, it is easy to imagine that the larger the image, the rougher the image.
So, for example, you need to blur the boundaries (for adjacent squares, fill the squares with their respective intermediate colors) and round the boundaries.
Also, when making the image smaller, on the other hand, it is necessary to remove unnecessary colors (what is it?) because it will not be possible to apply the range that I have been applying until now.If you do that, the image will appear to be crushed, so you may need to do something about it.

So far, I explained the simple logic in writing, but when I looked it up briefly, I found a very good slide, so I'll share it with you.
https://www.slideshare.net/ginrou799/ss-12685973

The world of image processing is quite profound, and unless you are interested in research in that field or for learning purposes, you can't really recommend implementing it on your own.
I think it's better to keep a simple logic in mind and leave it to an external library.


2022-09-30 16:24

(Answer as a generalization of image processing)

Capture the image and resize it to the specified size while maintaining the aspect ratio
I don't want to reduce the image quality as much as possible...

The following algorithms are frequently used as a classic digital image magnification/reduction algorithm.Both algorithms calculate (add and multiply) the pixel value of the destination image from multiple reference source image pixel values.

  • Bilinear method
  • Bicubic method
  • Lanczos method (Lancozs3 filter)
  • Mean pixel method [reduction only]

All of the above are classic algorithms at the level of textbook content, so technical details are passed on to reliable sources.New scaling algorithms using machine learning technology (so-called "super-resolution" technology) include waifu2x.


2022-09-30 16:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.