转自: imuse的专栏
在MIDP1.0中开发者自己实现翻转,在MIDP2.0中系统已经提供了翻转方法,但有些机型翻转效果不正确(厂商实现问题),所以自己实现翻转 方法。本想一次将图读到一个int数数组,然后进行变换,但如果图片过大,会内存溢出,所以通过对图片的每行进行变换来达到高效翻转。如果你有更高效的方 法欢迎讨论。
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* @功能:实现图片的高效翻转
* @作者: imuse
* @MAIL: 该邮件地址已受到反垃圾邮件插件保护。要显示它需要在浏览器中启用 JavaScript。
*
*/
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 原图<br>int transform 翻转方向<br>
* @返回值 转换后的图片
* */
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[i];
b[i] = b[nCount-i];
b[nCount-i] = nTemp;
}
}
}