0

I can write the image but its default resolution is 600x450. I want to specify my own resolution.

public void save(String path, String name) throws IOException 
{
            int x ;
            int y;
            x=scaled.getHeight();
            y=scaled.getWidth();

            System.out.println(x);
            System.out.println(y);

            if (scaled != null) 
            {  
                name += scaled.getWidth() + "x" + scaled.getHeight();
                ImageIO.write(scaled, "png", new File(path + File.separator + name + ".png"));
            } 
            else 
            {
                throw new NullPointerException("Scaled instance is null");
            }
}

Any suggestions?

Georgian
  • 8,795
  • 8
  • 46
  • 87
Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63

2 Answers2

1

Use AffineTransformOp, shown here, or Image#getScaledInstance(), shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

I presume scaled is a field image. Also, I will assume you're not getting a NullPointerException.

You can use scaled.getImageData().getScaled(int x, int y) to return a new scaled image with your own x and y.

Like so:

ImageData newData = scaled.getImageData().scaledTo(int x, int y);
Image newScaledImage = new Image(Display.getCurrent(), newData);
ImageIO.write(newScaledImage , "png", new File(path + File.separator + name + ".png"));
newScaledImage.dispose();
Georgian
  • 8,795
  • 8
  • 46
  • 87