900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 单片机C语言任意角度旋转图片 图片任意角度旋转(转)

单片机C语言任意角度旋转图片 图片任意角度旋转(转)

时间:2023-12-16 13:13:24

相关推荐

单片机C语言任意角度旋转图片 图片任意角度旋转(转)

/**

*@param imgSource 源图像

*@param cx 旋转点相对于源图像坐上角横坐标

*@param cy 旋转点相对于源图像坐上角纵坐标

*@param theta 图像逆时针旋转的角度

*@return 旋转后的图像

*/

public Image rotate(Image imgSource, int cx, int cy, double theta)

{

if (Math.abs(theta % 360) < 1)

return imgSource; //角度很小时直接返回

int w1 = imgSource.getWidth(); //原始图像的高度和宽度

int h1 = imgSource.getHeight();

int[] srcMap = new int[w1 * h1];

imgSource.getRGB(srcMap, 0, w1, 0, 0, w1, h1); //获取原始图像的像素信息

int dx = cx > w1 / 2 ? cx : w1 - cx; //计算旋转半径

int dy = cy > h1 / 2 ? cy : h1 - cy;

double dr = Math.sqrt(dx * dx + dy * dy);

int wh2 = (int) (2 * dr + 1); //旋转后新图像为正方形,其边长+1是为了防止数组越界

int[] destMap = new int[wh2 * wh2]; //存放新图像象素的数组

double destX, destY;

double radian = theta * Math.PI / 180; //计算角度计算对应的弧度值

for (int i = 0; i < w1; i++) {

for (int j = 0; j < h1; j++) {

if (srcMap[j * w1 + i] >> 24 != 0) { //对非透明点才进行处理

// 得到当前点经旋转后相对于新图像左上角的坐标

destX = dr + (i - cx) * Math.cos(radian) + (j - cy) // 这个地方不懂

* Math.sin(radian);

destY = dr + (j - cy) * Math.cos(radian) - (i - cx) //这个地方不懂

* Math.sin(radian);

//从源图像中往新图像中填充像素

destMap[(int) destY * wh2 + (int) destX] = srcMap[j * w1

+ i];// 不懂

}

}

}

return Image.createRGBImage(destMap, wh2, wh2, true); //返回旋转后的图像

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。