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
- <pre name="code" class="java" >import java.awt.Dimension;
- Import Java.awt.Graphics2D;
- Import Java.awt.Image;
- Import Java.awt.Rectangle;
- Import Java.awt.image.BufferedImage;
- Public class Rotateimage {
- public static bufferedimage Rotate (Image src, int angel) {
- int src_width = Src.getwidth (null);
- int src_height = Src.getheight (null);
- //Calculate the new image size
- Rectangle rect_des = calcrotatedsize (newRectangle (new Dimension (
- Src_width, Src_height)), Angel);
- BufferedImage res = null;
- res = New BufferedImage (Rect_des.width, Rect_des.height,
- BUFFEREDIMAGE.TYPE_INT_RGB);
- graphics2d g2 = Res.creategraphics ();
- //Transform
- G2.translate ((rect_des.width-src_width)/ 2,
- (rect_des.height-src_height)/ 2);
- G2.rotate (Math.toradians (Angel), Src_width/ 2, Src_height/ 2);
- G2.drawimage (SRC, null, null);
- return res;
- }
- public static Rectangle calcrotatedsize (Rectangle src, int angel) {
- //If Angel is greater than-degree, we need to do some conversion
- if (Angel >= ) {
- if (Angel/ 2 = = = 1) {
- int temp = src.height;
- Src.height = Src.width;
- Src.width = temp;
- }
- Angel = Angel% 90;
- }
- Double r = math.sqrt (src.height * src.height + src.width * src.width)/ 2;
- Double len = 2 * Math.sin (Math.toradians (Angel)/ 2) * r;
- Double Angel_alpha = (Math.pi-math.toradians (angel))/ 2;
- Double angel_dalta_width = Math.atan ((double) src.height/src.width);
- Double angel_dalta_height = Math.atan ((double) src.width/src.height);
- int len_dalta_width = (int) (LEN * Math.Cos (Math.pi-angel_alpha
- -Angel_dalta_width));
- int len_dalta_height = (int) (LEN * Math.Cos (Math.pi-angel_alpha
- -Angel_dalta_height));
- int des_width = src.width + len_dalta_width * 2;
- int des_height = src.height + len_dalta_height * 2;
- return New Java.awt.Rectangle (new Dimension (Des_width, des_height));
- }
- }</pre><br>
- <pre></pre>
- <p>junit the test code below, change the picture path to the picture path you want to use for your test. </p>
- <p></p>
- <pre name="code" class="java" ><pre name="code" class="java" >import Java.awt.image.BufferedImage;
- Import Java.io.File;
- Import java.io.IOException;
- Import Javax.imageio.ImageIO;
- Import Junit.framework.Assert;
- Import Org.junit.Test;
- Import Jugnoo.rotateimage;
- Public class Rotateimagetest {
- @Test
- public void Testrotate () throws IOException {
- BufferedImage src = imageio.read (new File ("d:/dog.jpg"));
- BufferedImage des = rotateimage.rotate (src, 30);
- Assert.assertnotnull (DES);
- Assert.asserttrue (Imageio.write (DES, "jpg", new File ("d:/dog2.jpg"));
- //Bigger Angel
- des = rotateimage.rotate (src, 150);
- Assert.assertnotnull (DES);
- Assert.asserttrue (Imageio.write (DES, "jpg", new File ("d:/dog3.jpg"));
- //Bigger Angel
- des = rotateimage.rotate (src, 270);
- Assert.assertnotnull (DES);
- Assert.asserttrue (Imageio.write (DES, "jpg", new File ("d:/dog4.jpg"));
- }
- }</pre><br>
- <br>
- <pre></pre>
- <p></p>
- <pre></pre>
- <pre></pre>
Java implementation of image content lossless arbitrary angle rotation