Java implementation of image content lossless arbitrary angle rotation

Source: Internet
Author: User
Tags cos sin

Transferred from: http://blog.csdn.net/heliang7/article/details/7309394

The main question is how to calculate the length and width of the new image after the image has been rotated.

With the help of Java 2d and the basic math library, you can actually use simple calculations to know.


The following algorithm only calculates the formula when the rotation is less than 90 degrees. When the rotation is greater than 90 o'clock, the problem domain can be converted to a sharp angle, and then calculated.

As shown, it is necessary to calculate the length of the Len_delta, that is, the position of the double vertical bar, it is the new picture to increase the width. (To increase the height of the same is available.) )

In fact, as long as the length of the Len, as well as Len and len_delta angle, you can calculate the length of Len_delta.

1. Length of Len. Notice that it is the bottom edge of the isosceles triangle, the apex is angel, easy to get Len=2*r*sin (ANGEL/2)

2. The angle between Len and Len_delta. The first can calculate the Angel_alpha, that is, the bottom corner of isosceles triangle Angel_alpha = (Pi-angel)/2

Then the angle between the R and the bottom edge of the original image is Angel_delta, obviously its tan value is the aspect ratio of the original picture (note the aspect ratio when calculating the increased height). Use Arctan to find out its angle.

The angle between len and Len_delta = Pi-angel_alpha-angel_delta

3. Len_delta = len * cos (angle of Len and Len_delta)



Java code Examples such as the following

[Java]View Plaincopy
    1. <pre name="code" class="java" >import java.awt.Dimension;
    2. Import Java.awt.Graphics2D;
    3. Import Java.awt.Image;
    4. Import Java.awt.Rectangle;
    5. Import Java.awt.image.BufferedImage;
    6. Public class Rotateimage {
    7. public static bufferedimage Rotate (Image src, int angel) {
    8. int src_width = Src.getwidth (null);
    9. int src_height = Src.getheight (null);
    10. //Calculate the new image size
    11. Rectangle rect_des = calcrotatedsize (newRectangle (new Dimension (
    12. Src_width, Src_height)), Angel);
    13. BufferedImage res = null;
    14. res = New BufferedImage (Rect_des.width, Rect_des.height,
    15. BUFFEREDIMAGE.TYPE_INT_RGB);
    16. graphics2d g2 = Res.creategraphics ();
    17. //Transform
    18. G2.translate ((rect_des.width-src_width)/ 2,
    19. (rect_des.height-src_height)/ 2);
    20. G2.rotate (Math.toradians (Angel), Src_width/ 2, Src_height/ 2);
    21. G2.drawimage (SRC, null, null);
    22. return res;
    23. }
    24. public static Rectangle calcrotatedsize (Rectangle src, int angel) {
    25. //If Angel is greater than-degree, we need to do some conversion
    26. if (Angel >= ) {
    27. if (Angel/ 2 = = = 1) {
    28. int temp = src.height;
    29. Src.height = Src.width;
    30. Src.width = temp;
    31. }
    32. Angel = Angel% 90;
    33. }
    34. Double r = math.sqrt (src.height * src.height + src.width * src.width)/ 2;
    35. Double len = 2 * Math.sin (Math.toradians (Angel)/ 2) * r;
    36. Double Angel_alpha = (Math.pi-math.toradians (angel))/ 2;
    37. Double angel_dalta_width = Math.atan ((double) src.height/src.width);
    38. Double angel_dalta_height = Math.atan ((double) src.width/src.height);
    39. int len_dalta_width = (int) (LEN * Math.Cos (Math.pi-angel_alpha
    40. -Angel_dalta_width));
    41. int len_dalta_height = (int) (LEN * Math.Cos (Math.pi-angel_alpha
    42. -Angel_dalta_height));
    43. int des_width = src.width + len_dalta_width * 2;
    44. int des_height = src.height + len_dalta_height * 2;
    45. return New Java.awt.Rectangle (new Dimension (Des_width, des_height));
    46. }
    47. }</pre><br>
    48. <pre></pre>
    49. <p>junit the test code below, change the picture path to the picture path you want to use for your test. </p>
    50. <p></p>
    51. <pre name="code" class="java" ><pre name="code" class="java" >import  Java.awt.image.BufferedImage;
    52. Import Java.io.File;
    53. Import java.io.IOException;
    54. Import Javax.imageio.ImageIO;
    55. Import Junit.framework.Assert;
    56. Import Org.junit.Test;
    57. Import Jugnoo.rotateimage;
    58. Public class Rotateimagetest {
    59. @Test
    60. public void Testrotate () throws IOException {
    61. BufferedImage src = imageio.read (new File ("d:/dog.jpg"));
    62. BufferedImage des = rotateimage.rotate (src, 30);
    63. Assert.assertnotnull (DES);
    64. Assert.asserttrue (Imageio.write (DES, "jpg", new File ("d:/dog2.jpg"));
    65. //Bigger Angel
    66. des = rotateimage.rotate (src, 150);
    67. Assert.assertnotnull (DES);
    68. Assert.asserttrue (Imageio.write (DES, "jpg", new File ("d:/dog3.jpg"));
    69. //Bigger Angel
    70. des = rotateimage.rotate (src, 270);
    71. Assert.assertnotnull (DES);
    72. Assert.asserttrue (Imageio.write (DES, "jpg", new File ("d:/dog4.jpg"));
    73. }
    74. }</pre><br>
    75. <br>
    76. <pre></pre>
    77. <p></p>
    78. <pre></pre>
    79. <pre></pre>

Java implementation of image content lossless arbitrary angle rotation

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.