在我的项目中我自己写了个函数,以便于我的游戏引擎调度:
/**
* @todo 灰度处理
*/
public
static
int
[
]
Turngrey(
Image
image)
{
int
rgx[
]
;
rgx = new
int
[
image.getWidth
(
)
* image.getHeight
(
)
]
;
image.getRGB
(
rgx, 0
, image.getWidth
(
)
, 0
, 0
, image.getWidth
(
)
,
image.getHeight
(
)
)
; //获得图片的ARGB值
int
r, g, b;
for
(
int
j = 0
; j < rgx.length
; j++)
{
r = (
rgx[
j]
& 0x00ff0000)
>> 16
;
g = (
rgx[
j]
& 0x0000ff00)
>> 8
;
b = rgx[
j]
& 0x000000ff;
if
(
(
rgx[
j]
== 0x00FFFFFF)
)
{
continue
;
}
r = g;
b = g;
//r = g*3/10;
//r = (g << 1 + r) >> 3 + (g << 1 + r);
//b = g * 6 / 10;
//b = ((g << 1) << 1 + (g << 1)) >> 3 + ((g << 1) << 1 + (g << 1));
//g = g >> 3 + g;
rgx[
j]
= (
(
r << 16
)
| (
g << 8
)
| b)
| 0xff000000;
}
return
rgx;
}
* @todo 灰度处理
*/
public
static
int
[
]
Turngrey(
Image
image)
{
int
rgx[
]
;
rgx = new
int
[
image.getWidth
(
)
* image.getHeight
(
)
]
;
image.getRGB
(
rgx, 0
, image.getWidth
(
)
, 0
, 0
, image.getWidth
(
)
,
image.getHeight
(
)
)
; //获得图片的ARGB值
int
r, g, b;
for
(
int
j = 0
; j < rgx.length
; j++)
{
r = (
rgx[
j]
& 0x00ff0000)
>> 16
;
g = (
rgx[
j]
& 0x0000ff00)
>> 8
;
b = rgx[
j]
& 0x000000ff;
if
(
(
rgx[
j]
== 0x00FFFFFF)
)
{
continue
;
}
r = g;
b = g;
//r = g*3/10;
//r = (g << 1 + r) >> 3 + (g << 1 + r);
//b = g * 6 / 10;
//b = ((g << 1) << 1 + (g << 1)) >> 3 + ((g << 1) << 1 + (g << 1));
//g = g >> 3 + g;
rgx[
j]
= (
(
r << 16
)
| (
g << 8
)
| b)
| 0xff000000;
}
return
rgx;
}
游戏中的效果在下面(当然引擎可以随时控制颜色恢复彩色):