随笔-0  评论-0  文章-2  trackbacks-0
Image img1;//原始的不翻转的图片
age img2;//要翻转成的图片
g1 = createImage("/img.png");
g2 = createTransferImage(img1, TRANS_NONE);
drawImage(img2,0,0,20);
 
///////////////////////////////////////////////////////////////
 
public final static int TRANS_NONE = 0;//不翻
 
public final static int TRANS_MIRROR = 1;//左右镜像
 
public final static int TRANS_MIRROR_ROT180 = 2;//上下镜像变换
 
public final static int TRANS_ROT90 = 3;//顺时针旋转90度
 
public final static int TRANS_ROT180 = 4;//(暂时有问题)
 
public final static int TRANS_ROT270 = 5;//顺时针旋转270度
 
public final static int TRANS_MIRROR_ROT90 = 6;//先左右镜像变换,再顺时针旋转90度
 
public final static int TRANS_MIRROR_ROT270 = 7;//先左右镜像变换,再顺时针旋转270度
 
 
 
private   Image createTransferImage(Image image, int trans) {
 
          int w = image.getWidth();
        int h = image.getHeight();
        int ARGBData[] = new int[w * h];//原始图片
        int tranARGBData[] = new int[w * h];//翻转后的图片存放的图片
        image.getRGB(ARGBData, 0, w, 0, 0, w, h);
        Image retImage=null;
 
          switch(trans)
        {
          case TRANS_NONE://无翻转
            retImage=image;
            break;
 
            case TRANS_MIRROR://左右水平镜像
            retImage=Image.createRGBImage(getMirror(ARGBData, w, h), w, h, true);
            break;
 
            case TRANS_MIRROR_ROT180://上下垂直镜像
          for (int y = 0, y1 = h; y < h; y++, y1--) {
            for (int x = 0, x1 = w; x < w; x++, x1--) {
                tranARGBData[y * w + x] = ARGBData[y1 * w - (w - x1 + 1)];
            }
          }
          retImage=Image.createRGBImage(getMirror(tranARGBData, w, h), w, h, true);
          break;
 
            case TRANS_ROT90: //顺时针90度/逆时针270度
            w = image.getHeight();
            h = image.getWidth();
            for (int y = 0; y < h; y++) {
              for (int x = 0; x < w; x++) {
                tranARGBData[y * w + x] = ARGBData[x * h + y];
              }
            }
            retImage=Image.createRGBImage(getMirror(tranARGBData, w, h), w, h, true);
            break;
 
            case TRANS_ROT180://顺/逆时针180度
          w = image.getHeight();
          h = image.getWidth();
          for (int y = 0, y1 = h; y < h; y++, y1--) {
            for (int x = 0, x1 = w; x < w; x++, x1--) {
                tranARGBData[y * w + x] = ARGBData[y1 * w - (w - x1 + 1)];
            }
          }
          retImage= Image.createRGBImage(tranARGBData, w, h, true);
          break;
 
            case TRANS_MIRROR_ROT270://顺时针270度/逆时针90度
          w = image.getHeight();
          h = image.getWidth();
          for (int y = 0, y1 = h; y < h; y++, y1--) {
            for (int x = 0, x1 = w; x < w; x++, x1--) {
                tranARGBData[y * w + x] = ARGBData[x1 * h - (h - y1 + 1)];
            }
          }
          retImage= Image.createRGBImage(getMirror(tranARGBData, w, h), w, h, true);
          break;
 
            case TRANS_MIRROR_ROT90://先镜像再顺时针翻转90度
            w = image.getHeight();
            h = image.getWidth();
            for (int y = 0, y1 = h; y < h; y++, y1--) {
              for (int x = 0, x1 = w; x < w; x++, x1--) {
                tranARGBData[y * w + x] = ARGBData[x1 * h - (h - y1 + 1)];
              }
            }
            retImage=Image.createRGBImage(tranARGBData, w, h, true);
            break;
 
 
 
            case -270://先镜像再顺时针翻转270度
            w = image.getHeight();
            h = image.getWidth();
            for (int y = 0; y < h; y++) {
              for (int x = 0; x < w; x++) {
                tranARGBData[y * w + x] = ARGBData[x * h + y];
              }
            }
            retImage=Image.createRGBImage(tranARGBData, w, h, true);
            break;
 
              default:
              break;
        }
        return retImage;
      }
      int[] getMirror(int[] ARGBData, int w, int h) {//左右水平镜像
        int tranARGBData[] = new int[w * h];
        for (int y = 0; y < h; y++) {
          for (int x = 0; x < w; x++) {
              tranARGBData[y * w + x] = ARGBData[y * w + (w - x - 1)];
          }
        }
        return tranARGBData;
}
posted on 2006-11-29 17:47 土豆娃娃 阅读(229) 评论(0)  编辑  收藏 所属分类: J2ME

只有注册用户登录后才能发表评论。


网站导航: