tags:

views:

2783

answers:

3

hello all im trying to resize bufferdImage in memory in java but to keep the aspect ratio of the image im have something like this but this is not good

              int w = picture.getWidth();
              int h = picture.getWidth();
              int neww=w;
              int newh=h;
              int wfactor = w;
              int hfactor = h;
              if(w > DEFULT_PICTURE_WIDTH || h > DEFULT_PICTURE_HIGHT)
              {
               while(neww > DEFULT_PICTURE_WIDTH)
               {
                neww = wfactor /2;
                newh = hfactor /2;
                wfactor = neww;
                hfactor = newh;
               }
              }
              picture = Utils.resizePicture(picture,neww,newh);
+2  A: 

For starters - take a look at line 2. Shouldnt that be getHeight()?

You dont want a while loop for the resizing, you want to find out the resizing ratio, which is a simple bit of math.

(width / height) = (new_width / new_height)

If you know one of the 'new' sizes, the other can be found via multiplication

new_height * (width / height) = new_width

You can also use the lazy method provided by BufferedImage's superclass Image, getScaledInstance() - using -1 for either width or height will maintain aspect ratio

ex:
scaledPic = picture.getScaledInstance(new_width, -1, Image.SCALE_FAST);

Max
yeah its getHeight wrong typois there any Reason i cant use getScaledInstance with BufferdImage?
That should be fine because BufferedImage extends Image
Max
A: 

If you want to resize a picture of w0 x h0 to w1 x h1 by keeping the aspect ratio, then calculate the vertical and horizontal scale and select the smaller one.

double scalex = 1;
double scaley = 1;
if (scalingMode == ScalingMode.WINDOW_SIZE) {
  scalex = (double)getWidth() / frontbuffer.getWidth();
  scaley = (double)getHeight() / frontbuffer.getHeight();
} else
if (scalingMode == ScalingMode.KEEP_ASPECT) {
  double sx = (double)getWidth() / frontbuffer.getWidth();
  double sy = (double)getHeight() / frontbuffer.getHeight();
  scalex = Math.min(sx, sy);
  scaley = scalex;
  // center the image
  g2.translate((getWidth() - (frontbuffer.getWidth() * scalex)) / 2,
    (getHeight() - (frontbuffer.getHeight() * scaley)) / 2);
}
g2.scale(scalex, scaley);
if (interpolation != ImageInterpolation.NONE) {
  g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolation.hint);
}
g2.drawImage(frontbuffer, 0, 0, null);
kd304
what represent the frontbuffer?
This is an excerpt from one of my codes which renders an animation using swap buffering. frontbuffer is just the BufferedImage which needs to be shown at the moment.
kd304
A: 

If width, height of source and target are known, use following function to determine scale of the image.

private double determineImageScale(int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {

double scalex = (double) targetWidth / sourceWidth;
double scaley = (double) targetHeight / sourceHeight;
return Math.min(scalex, scaley);

}

Then use this scale to scale up/down the image using following code

Image scaledImage = sourceBufferedImage.getScaledInstance((int) (width * scale), (int) (height * scale), Image.SCALE_SMOOTH);
ronpaul fan