浏览 4152 次
锁定老帖子 主题:J2ME 图片翻转
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-01-19
原文 http://www.3geye.net/?3/viewspace-2826
import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; //功能:实现图片的高效翻转 public class ImageUtil ...{ //无变化 public final static int TRANS_NONE = 0; // 顺时针翻转90度 public final static int TRANS_ROT90 = 5; //顺时针翻转180度 public final static int TRANS_ROT180 = 3; //顺时针翻转270度 public final static int TRANS_ROT270 = 6; // 水平镜像翻转 public final static int TRANS_MIRROR = 2; //水平镜像翻转并顺时针翻转90度 public final static int TRANS_MIRROR_ROT90 = 7; //水平镜像翻转并顺时针翻转180度 public final static int TRANS_MIRROR_ROT180 = 1; //水平镜像翻转并顺时针翻转270度 public final static int TRANS_MIRROR_ROT270 = 4; //@功能 实现图片的翻转 //@参数 Image srcImage 原图 int transform 翻转方向 //@返回值 转换后的图片 public static Image transformImage(Image srcImage, int transform) ...{ int srcW = srcImage.getWidth(); int srcH = srcImage.getHeight(); Image imgRet = null; switch (transform) ...{ case TRANS_NONE: imgRet = srcImage; break; case TRANS_ROT90: ...{ /* 原: 目标: * 1234 951 * 5678 062 * 90ab a73 * b84 * */ int[] srcRGB = new int[srcW]; imgRet = Image.createImage(srcH, srcW); Graphics g = imgRet.getGraphics(); for (int i = 0; i < srcH; i++) ...{ srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1); g.drawRGB(srcRGB, 0, 1, srcH - i - 1, 0, 1, srcW, true); } } break; case TRANS_ROT180: ...{ /* 原: 目标: * 1234 ba09 * 5678 8765 * 90ab 4321 * * */ int[] srcRGB = new int[srcW]; imgRet = Image.createImage(srcW, srcH); Graphics g = imgRet.getGraphics(); for (int i = 0; i < srcH; i++) ...{ srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1); arrayExchange(srcRGB); g.drawRGB(srcRGB, 0, srcW, 0, srcH-1-i, srcW, 1, true); } } break; case TRANS_ROT270: ...{ /* 原: 目标: * 1234 48b * 5678 37a * 90ab 260 * 159 * */ int[] srcRGB = new int[srcW]; imgRet = Image.createImage(srcH, srcW); Graphics g = imgRet.getGraphics(); for (int i = 0; i < srcH; i++) ...{ srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1); arrayExchange(srcRGB); g.drawRGB(srcRGB, 0, 1, i, 0, 1, srcW, true); } } break; case TRANS_MIRROR: ...{ /* 原: 目标: * 1234 4321 * 5678 8765 * 90ab ba09 * * */ int[] srcRGB = new int[srcW]; imgRet = Image.createImage(srcW, srcH); Graphics g = imgRet.getGraphics(); for (int i = 0; i < srcH; i++) ...{ srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1); arrayExchange(srcRGB); g.drawRGB(srcRGB, 0, srcW, 0, i, srcW, 1, true); } } break; case TRANS_MIRROR_ROT90: ...{ /* 原: 目标: * 1234 b84 * 5678 a73 * 90ab 062 * 951 * */ int[] srcRGB = new int[srcW]; imgRet = Image.createImage(srcH, srcW); Graphics g = imgRet.getGraphics(); for (int i = 0; i < srcH; i++) ...{ srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1); arrayExchange(srcRGB); g.drawRGB(srcRGB, 0, 1, srcH - i - 1, 0, 1, srcW, true); } } break; case TRANS_MIRROR_ROT180: ...{ /* 原: 目标: * 1234 90ab * 5678 5678 * 90ab 1234 * * */ int[] srcRGB = new int[srcW]; imgRet = Image.createImage(srcW, srcH); Graphics g = imgRet.getGraphics(); for (int i = 0; i < srcH; i++) ...{ srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1); g.drawRGB(srcRGB, 0, srcW, 0, srcH-1-i, srcW, 1, true); } } break; case TRANS_MIRROR_ROT270: ...{ /* 原: 目标: * 1234 159 * 5678 260 * 90ab 37a * 48b * */ int[] srcRGB = new int[srcW]; imgRet = Image.createImage(srcH, srcW); Graphics g = imgRet.getGraphics(); for (int i = 0; i < srcH; i++) ...{ srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1); g.drawRGB(srcRGB, 0, 1, i, 0, 1, srcW, true); } } break; default: imgRet = srcImage; break; } return imgRet; } //@功能 翻转一个INT数组 //@参数 要转换的INT数组 // @返回值 无 // public static final void arrayExchange(int[] b)...{ int nCount = b.length; int nLen = nCount/2; nCount--; for (int i = 0; i < nLen; i++) ...{ int nTemp = b; b = b[nCount-i]; b[nCount-i] = nTemp; } } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |