Home > Java > javaTutorial > java image rotation

java image rotation

黄舟
Release: 2016-12-30 11:46:29
Original
1760 people have browsed it

The rotation of the image requires the use of a rotation matrix.

The clockwise rotation matrix is:

java image rotation

The counterclockwise rotation matrix is:

java image rotation

We use The center of the image is the rotation point, and the code for counterclockwise rotation of alpha degrees is as follows:

public void Rotation(double degree){
		
	degree = Math.toRadians(degree);//化为弧度
        int sw = (int) Math.sqrt(w*w +h*h);//旋转后图像的w
        int sh = sw;//旋转后图像的h
        
        int ox = w/2;
        int oy = h/2;
        
		int[] d = new int[sw*sh];
		for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
            	     int x1 = (int)(Math.cos(degree)*(x-ox) + Math.sin(degree)*(y-oy));//原图像上点旋转后的点的x坐标
            	     int y1 = (int)(Math.cos(degree)*(y-oy) - Math.sin(degree)*(x-ox));
            	     d[x1-sw/2+ (y1+sh/2)* sw] = data[x + y * w];
                   }
		}

		this.data = d;
		this.w = sw;
		this.h = sh;
	}
Copy after login

The result of the rotation:

java image rotation

java image rotation

As you can see from the image, some points are abandoned, so after rotation, we still need to perform interpolation. Bilinear interpolation is recommended.

But for specific angles, we can still achieve lossless.



Rotate 90 degrees clockwise, rotate 90 degrees counterclockwise, and flip the key code:

for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {           	
            	d[y+ (w-x-1)* h] = data[x + y * w];//逆时针
                d[h-1-y+ x* h] = data[x + y * w];//顺时针
                d[w-x-1+ y* w] = data[x + y * w];//翻转
     }
}
Copy after login

The results of the operation are as follows:

java image rotation

java image rotation

The above is the content of java image rotation. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template