j2me之图片翻转和透明等的处理代码

代码

/**
* 获得img的半透明图像
*
@param alpha :alpha通道值, 必须在0到255之间
*
* 注:在修改alpha值时,先用或运算 | 0xff000000 使其alpha为0xff。
* 再执行且运算,使其alpha值变为所传参数alpha。
*/
private Image createAlphaImage(Image img, int alpha)
{
int width = img.getWidth();
int height = img.getHeight();
int [] argb = new int [width * height];
img.getRGB(argb,
0 , width, 0 , 0 , width, height);
int temp = (alpha << 24 ) | 0x00ffffff ;
for ( int i = 0 ; i < argb.length; i ++ ) {
argb[i]
= (argb[i] | 0xff000000 ) & temp;
}
return Image.createRGBImage(argb, width, height, true );
}



/**
* 获得img的变色图像 -- 只用某些位的颜色
* 比如:color = 0x00ff00. 则返回图像的色值为原来的green位的值。
* 放开两条打印语句,可以清晰的看到色值变化情况。
*/
private Image createColorImage(Image img, int color)
{
int width = img.getWidth();
int height = img.getHeight();
int [] argb = new int [width * height];
img.getRGB(argb,
0 , width, 0 , 0 , width, height);

for ( int i = 0 ; i < argb.length; i ++ ) {
// System.out.print(Integer.toHexString(argb[i]) + " , ");
argb[i] = argb[i] & color;
// System.out.println(Integer.toHexString(argb[i]));
}
return Image.createRGBImage(argb, width, height, false );
}




/**
* 获得img的倒立图像(竖直翻转)
*/
private Image createReversalImage(Image img)
{
int width = img.getWidth();
int height = img.getHeight();
int [] argb = new int [width * height];
img.getRGB(argb,
0 , width, 0 , 0 , width, height);

int [] argb2 = new int [width * height];
int row = 0 ;
int column = 0 ;
int index = 0 ;
for ( int i = 0 ; i < argb.length; i ++ ) {
row
= i / width;
column
= i % width;
index
= (height - row - 1 ) * width + column;
argb2[i]
= argb[index] ;
}
return Image.createRGBImage(argb2, width, height, false );
}

/**
* 获得img的水平翻转图像
*/
private Image createMirrorImage(Image img)
{
int width = img.getWidth();
int height = img.getHeight();
int [] argb = new int [width * height];
img.getRGB(argb,
0 , width, 0 , 0 , width, height);

int [] argb2 = new int [width * height];
int row = 0 ;
int column = 0 ;
int index = 0 ;
for ( int i = 0 ; i < argb.length; i ++ ) {
row
= i / width;
column
= i % width;
index
= row * width + (width - 1 - column);
argb2[i]
= argb[index] ;
}
return Image.createRGBImage(argb2, width, height, false );
}


/**
* 获得img逆时针转90度后的图像
*/
private Image createImage_TRANS_ROT90(Image img)
{
int width = img.getWidth();
int height = img.getHeight();
int [] argb = new int [width * height];
img.getRGB(argb,
0 , width, 0 , 0 , width, height);

int [] argb2 = new int [width * height];
int row = 0 ;
int column = 0 ;
int index = 0 ;
for ( int i = 0 ; i < argb.length; i ++ ) {
row
= i / width;
column
= i % width;
index
= (width - column - 1 ) * height + row;
argb2[index]
= argb[i] ;
}
return Image.createRGBImage(argb2, height, width, false );
}


/**
* 获得img顺时针转90度后的图像(相当于逆时针转270度)
*/
private Image createImage_TRANS_ROT270(Image img)
{
int width = img.getWidth();
int height = img.getHeight();
int [] argb = new int [width * height];
img.getRGB(argb,
0 , width, 0 , 0 , width, height);

int [] argb2 = new int [width * height];
int row = 0 ;
int column = 0 ;
int index = 0 ;
for ( int i = 0 ; i < argb.length; i ++ ) {
row
= i / width;
column
= i % width;
index
= column * height + height - 1 - row;
argb2[index]
= argb[i] ;
}
return Image.createRGBImage(argb2, height, width, false );
}




 

转载于:https://www.cnblogs.com/chaohi/archive/2010/12/14/1905931.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值