Rotate images from any angle

Source: Internet
Author: User

the function of rotating an image from any angle is concise.

/** <Br/> * @ Param imgsource source image <br/> * @ Param CX: the rotation point is relative to the x-axis of the source image. <br/> * @ Param Cy: the rotation point is relative to the source image is taken as the vertical coordinate <br/> * @ Param Theta the angle from which the image rotates counterclockwise <br/> * @ return the rotated image <br/> */<br/> Public Image rotate (image imgsource, int CX, int cy, double theta) <br/>{< br/> If (math. ABS (theta % 360) <1) <br/> return imgsource; // return directly if the angle is very small </P> <p> int W1 = imgsource. getwidth (); // the height and width of the original image </P> <p> int H1 = imgsource. getheight (); </P> <p> int [] Srcmap = new int [W1 * H1]; </P> <p> imgsource. getrgb (srcmap, 0, W1, 0, 0, W1, H1 ); // obtain the pixel information of the original image </P> <p> int dx = Cx> W1/2? CX: W1-cx; // calculate the rotation radius </P> <p> int DY = Cy> H1/2? Cy: H1-cy; </P> <p> double DR = math. SQRT (dx * dx + dy * Dy); </P> <p> int wh2 = (INT) (2 * Dr + 1 ); // After rotation, the new image is a square with the side length + 1 to prevent array out of bounds </P> <p> int [] destmap = new int [wh2 * wh2]; // array for storing new image pixels </P> <p> double destx and desty; </P> <p> double radian = Theta * Math. PI/180; // calculate the radian value from the angle </P> <p> for (INT I = 0; I <W1; I ++) {<br/> for (Int J = 0; j <H1; j ++) {<br/> If (srcmap [J * W1 + I]> 24! = 0) {// process non-transparent Points <br/> // obtain the coordinates of the current point after rotation relative to the upper left corner of the new image <br/> destx = DR + (I-cx) * Math. cos (radian) + (J-cy) // This location does not understand <br/> * Math. sin (radian); <br/> desty = DR + (J-cy) * Math. cos (radian)-(I-cx) // This location does not understand <br/> * Math. sin (radian); <br/> // fill in pixels from the source image to the new image <br/> destmap [(INT) desty * wh2 + (INT) destx] = srcmap [J * W1 <br/> + I]; // do not understand <br/>}< br/> return image. creatergbimage (destmap, wh2, wh2, true); // returns the rotated image <br/>}

 

Another drawback is that the rotated image is a little distorted. The function marks a point that you don't understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.