Advertisement

[java] how to rotate an image?

Started by February 17, 2001 09:26 PM
2 comments, last by thuned 23 years, 11 months ago
how can you rotate an image? for example, i load a gif image and display it. what do i need to import and what functions can i use to rotate the image. thuned life is unfair, take advantage of it. UNMB2 - if the link doesn't work, try clicking it Edited by - thuned on February 18, 2001 2:14:32 AM
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
anyone? too be more specific, i want it rotated around the zaxis
so if i rotate it 180 degrees it''s upsidedown instead of a "line"

life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
Advertisement
There''s probably some Java2D functions you can use, but if you''re wanting to do it the old fashioned way, here''s some really old code I''ve dug out - can''t remember where I originally appropriated it from though. Dunno if it will do what you want but it might be helpful...?

Basically convert your image to pixels (use MemoryImageSource/PixelGrabber/whatever), then call this function:

  public final byte[] rotate ( byte iPixels[], int degree, int w, int h, boolean imageClear ) {     if (iPixels.length < 1)       return null;    byte p[] = new byte[iPixels.length];    //    //the following is to avoid repeated calculations    //    double theta = degree * RADIANS_CALC;  //rotation    double tx = w / 2.0;  //translation (to rotate about center)    double ty = h / 2.0;    double cos_theta = Math.cos(theta);    double sin_theta = Math.sin(theta);    double dx = tx * (1.0 - cos_theta) + ty * sin_theta;    double dy = ty * (1.0 - cos_theta) - tx * sin_theta;    int i = 0;    for (int y = 0; y < h; y++) {      for (int x = 0; x < w; x++) {        int xp = (int)((cos_theta * x - sin_theta * y + dx) + 0.5);        int yp = (int)((sin_theta * x + cos_theta * y + dy) + 0.5);        if (imageClear) {          if (yp < 0)            yp = 0;          else if (yp >= h)            yp = h-1;          if (xp < 0)            xp = 0;          else if (xp >= w)            xp = w-1;        }        if (yp >= 0 && yp < h && xp >= 0 && xp < w) {          int dst = yp * w + xp;          p = iPixels[dst];        }        i++;      }    }    return p;  } 


Convert the returned array back into an image.

Jason
thanks, i''ll try it out

life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)

This topic is closed to new replies.

Advertisement