Jump to content

  • Log In with Google      Sign In   
  • Create Account

[java] how to rotate an image?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 thuned   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 February 2001 - 03:26 PM

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

Sponsor:

#2 thuned   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 February 2001 - 06:04 AM

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

#3 JasonB   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 February 2001 - 09:18 AM

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


#4 thuned   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 February 2001 - 06:59 PM

thanks, i''ll try it out

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS