0

I'm trying to scale an image (my last goal is to perform anti-aliasing on an image) but I see a white screen after scaling. how am I supposed to use the getScaledInstance() method to scale an image? I'm learning java and I never used this before. I checked out two similar questions in here(this and this one and also this one) but I didn't understand. I think I need a completer explanation.

this is how I'm doing this:

private void imageScanner(String C_imagePath, String N_imagePath, int N_WIDTH, int N_HEIGHT) throws IOException {
        System.out.println("imageScanner started.");
        Image image = new ImageIcon(C_imagePath).getImage();
        image = image.getScaledInstance(N_WIDTH * 20, N_HEIGHT * 20, Image.SCALE_SMOOTH);
        BufferedImage bi = new BufferedImage(N_WIDTH * 20, N_HEIGHT * 20, BufferedImage.TYPE_INT_ARGB);
        bi.getGraphics().drawImage(image, 0, 0, N_WIDTH * 20, N_HEIGHT * 20, null);
        try {
            ImageIO.write(bi, "png", new File(N_imagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("imageScanner is done.");
    }

I also tried to wrap it with imageIcon (like this: image = new ImageIcon(image.getScaledInstance(N_WIDTH * 20, N_HEIGHT * 20, Image.SCALE_SMOOTH)).getImage();) to sovle this problem and acually the white screen problem solved but I don't see any differece. I'm trying not to do anti-aliasing using Graphics2D but if it is not the only way of performing anti-aliasing.

EDIT: actually, the white screen problem solved and now the main problem is I don't see any difference.

without using image = new ImageIcon(image.getScaledInstance(N_WIDTH * 20, N_HEIGHT * 20, Image.SCALE_SMOOTH)).getImage();

without image = new ImageIcon(image.getScaledInstance(N_WIDTH * 20, N_HEIGHT * 20, Image.SCALE_SMOOTH)).getImage();

with using image = new ImageIcon(image.getScaledInstance(N_WIDTH * 20, N_HEIGHT * 20, Image.SCALE_SMOOTH)).getImage();

with image = new ImageIcon(image.getScaledInstance(N_WIDTH * 20, N_HEIGHT * 20, Image.SCALE_SMOOTH)).getImage();

hamid
  • 9
  • 7
  • 1
    What difference is it that you expect to see? – Harald K Jun 14 '21 at 10:57
  • @HaraldK doesn't that supposed to go to do some anti-aliasing on it and give me an image with better quality? – hamid Jun 14 '21 at 12:24
  • 1
    Internally `Image.getScaledInstance` w/`SCALE_SMOOTH` parameter uses `AreaAveragingScaleFilter`. From its API doc: *"The blending process is analogous to scaling up the source image to a multiple of the destination size using pixel replication and then scaling it back down to the destination size by simply averaging all the pixels in the supersized image that fall within a given pixel of the destination image. "* So, for scaling up, no. – Harald K Jun 14 '21 at 16:33
  • If an image is scaled up by a factor of 20, it **will** look horrid. It is better to load an image at the size it is intended to be displayed, or at worst, scale it ***down*** from a larger image. – Andrew Thompson Jun 24 '21 at 21:16

0 Answers0