日期:2014-05-20  浏览次数:20804 次

求j2me图片任意角度旋转算法,不失真,j2me 自己的api实现
给出一个失真的算法 
/** 
     *@param imgSource  源图像 
     *@param cx   旋转点相对于源图像坐上角横坐标 
     *@param cy   旋转点相对于源图像坐上角纵坐标 
     *@param theta 图像逆时针旋转的角度 
     *@return 旋转后的图像 
     */  
     public static 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); //返回旋转后的图像  
    } 
------解决方案--------------------
没办法,计算过程出现的误差值是没法消除的。