📄 imageutil.java
字号:
* 图片特效黑白
*/
public Image effect_black_white(Image src)
{
int srcW = src.getWidth();
int srcH = src.getHeight();
int[] srcPixels = getPixels(src);
int r = 0;
int g = 0;
int b = 0;
int a = 0;
int argb;
int temp;
for (int i = 0; i < srcH; i++)
{
for(int ii=0;ii<srcW;ii++)
{
argb = srcPixels[i*srcW+ii];
a = ((argb & 0xff000000) >> 24); // alpha channel
r = ((argb & 0x00ff0000) >> 16); // red channel
g = ((argb & 0x0000ff00) >> 8); // green channel
b = (argb & 0x000000ff); // blue channel
temp = (int)(.299*(double)r+.587*(double)g+.114*(double)b);
r = temp;
g = temp;
b = temp;
srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
}
}
return drawPixels(srcPixels, srcW, srcH);
}
/*
* 图片特效粉笔画
*/
public Image effect_crayon(Image src)
{
int srcW = src.getWidth();
int srcH = src.getHeight();
int[] srcPixels = getPixels(src);
int r = 0;
int g = 0;
int b = 0;
int a = 0;
int argb;
int r1 = 0;
int g1 = 0;
int b1 = 0;
int a1 = 0;
int r2 = 0;
int g2 = 0;
int b2 = 0;
int a2 = 0;
for (int i = 0; i < srcH; i++)
{
for(int ii=0;ii<srcW;ii++)
{
argb = srcPixels[i*srcW+ii];
a = ((argb & 0xff000000) >> 24); // alpha channel
r = ((argb & 0x00ff0000) >> 16); // red channel
g = ((argb & 0x0000ff00) >> 8); // green channel
b = (argb & 0x000000ff); // blue channel
if(i+1 == srcH)
{
r1= 0;
g1= 0;
b1=0;
}
else
{
argb = srcPixels[(i+1)*srcW+ii];
//a1 = ((argb & 0xff000000) >> 24); // alpha channel
r1 = ((argb & 0x00ff0000) >> 16); // red channel
g1 = ((argb & 0x0000ff00) >> 8); // green channel
b1 = (argb & 0x000000ff); // blue channel
}
if(ii+1 == srcW){
r2= 0;
g2= 0;
b2=0;
}
else
{
argb = srcPixels[i*srcW+ii+1];
r2 = ((argb & 0x00ff0000) >> 16); // red channel
g2 = ((argb & 0x0000ff00) >> 8); // green channel
b2 = (argb & 0x000000ff); // blue channel
}
// rr1=(r1-r2)^2 rr2=(r1-r3)^2
r = (int)Math.sqrt((double)(2*(r-r1)*(r-r1)+(r-r2)*(r-r2)));
g = (int)Math.sqrt((double)(2*(g-g1)*(g-g1)+(g-g2)*(g-g2)));
b = (int)Math.sqrt((double)(2*(b-b1)*(b-b1)+(b-b2)*(b-b2)));
r =255-r; // red channel
g =255-g; // green channel
b =255-b; // blue channel
srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
}
}
return drawPixels(srcPixels, srcW, srcH);
}
/*
* 图片特效蒙版
*/
public Image effect_hoodwink(Image src)
{
int srcW = src.getWidth();
int srcH = src.getHeight();
int[] srcPixels = getPixels(src);
int r = 0;
int g = 0;
int b = 0;
int a = 0;
int argb;
for (int i = 0; i < srcH; i++)
{
for(int ii=0;ii<srcW;ii++)
{
argb = srcPixels[i*srcW+ii];
a = ((argb & 0xff000000) >> 24); // alpha channel
r = ((argb & 0x00ff0000) >> 16); // red channel
g = ((argb & 0x0000ff00) >> 8); // green channel
b = (argb & 0x000000ff); // blue channel
r = (int)(.299*(double)r);
g = (int)(.587*(double)g);
b = (int)(.114*(double)b);
srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
}
}
return drawPixels(srcPixels, srcW, srcH);
}
private int[] getTransImg(int alpha,int[] srcRgbdata,int[] desRgbdata)
{
int [] tempRgbData = new int[desRgbdata.length];
int sr ;
int sg ;
int sb ;
int dr ;
int dg ;
int db ;
int tr ;
int tg ;
int tb ;
for(int i=0;i<desRgbdata.length;i++)
{
sr = (srcRgbdata[i]&0xff0000)>>16;
sg = (srcRgbdata[i]&0xff00)>>8;
sb = srcRgbdata[i]&0xff;
dr = (desRgbdata[i]&0xff0000)>>16;
dg = (desRgbdata[i]&0xff00)>>8;
db = desRgbdata[i]&0xff;
tr = (sr*alpha + dr*(255-alpha))/255;
tg = (sg*alpha + dg*(255-alpha))/255;
tb = (sb*alpha + db*(255-alpha))/255;
tempRgbData[i] = (tr<<16)|(tg<<8)|tb;
}
return tempRgbData;
}
/*
* 图片特旋转
*/
public Image effect_rotate(Image src,int direction)
{
Sprite sprite = new Sprite(src);
switch(direction)
{
case 1:
sprite.setTransform(sprite.TRANS_ROT270);
break;
case 2:
sprite.setTransform(sprite.TRANS_ROT90);
break;
}
Image temp = Image.createImage(src.getHeight(),src.getWidth());
Graphics g = temp.getGraphics();
sprite.setPosition(0,0);
sprite.paint(g);
return temp;
}
/*
* 图片特霓虹灯
*/
public Image effect_neonLight(Image src)
{
int srcW = src.getWidth();
int srcH = src.getHeight();
int[] srcPixels = getPixels(src);
int r = 0;
int g = 0;
int b = 0;
int a = 0;
int argb;
int r1 = 0;
int g1 = 0;
int b1 = 0;
int a1 = 0;
int r2 = 0;
int g2 = 0;
int b2 = 0;
int a2 = 0;
for (int i = 0; i < srcH; i++)
{
for(int ii=0;ii<srcW;ii++)
{
argb = srcPixels[i*srcW+ii];
a = ((argb & 0xff000000) >> 24); // alpha channel
r = ((argb & 0x00ff0000) >> 16); // red channel
g = ((argb & 0x0000ff00) >> 8); // green channel
b = (argb & 0x000000ff); // blue channel
if(i+1 == srcH)
{
r1= 0;
g1= 0;
b1=0;
}
else
{
argb = srcPixels[(i+1)*srcW+ii];
//a1 = ((argb & 0xff000000) >> 24); // alpha channel
r1 = ((argb & 0x00ff0000) >> 16); // red channel
g1 = ((argb & 0x0000ff00) >> 8); // green channel
b1 = (argb & 0x000000ff); // blue channel
}
if(ii+1 == srcW){
r2= 0;
g2= 0;
b2=0;
}
else
{
argb = srcPixels[i*srcW+ii+1];
r2 = ((argb & 0x00ff0000) >> 16); // red channel
g2 = ((argb & 0x0000ff00) >> 8); // green channel
b2 = (argb & 0x000000ff); // blue channel
}
// rr1=(r1-r2)^2 rr2=(r1-r3)^2
r = (int)Math.sqrt((double)(2*(r-r1)*(r-r1)+(r-r2)*(r-r2)));
g = (int)Math.sqrt((double)(2*(g-g1)*(g-g1)+(g-g2)*(g-g2)));
b = (int)Math.sqrt((double)(2*(b-b1)*(b-b1)+(b-b2)*(b-b2)));
srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
}
}
return drawPixels(srcPixels, srcW, srcH);
}
/*********************************
* @todo 图片放大缩小
* @param srcImg 原始图片
* @param desW 变化后图片的宽
* @param desH 变化后图片的高
* @return 处理后的图片
*********************************/
private Image ZoomImageIns(Image srcImg, int desW, int desH) {
int srcW = srcImg.getWidth(); //原始图像宽
int srcH = srcImg.getHeight(); //原始图像高
int[] srcBuf = new int[srcW * srcH]; //原始图片像素信息缓存
//srcBuf获取图片像素信息
Image desImg = Image.createImage(srcW, srcH);
if (srcImg.isMutable()) { /*如果是可变图像*/
srcImg.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
//DirectUtils.getDirectGraphics(srcImg.getGraphics()).
// getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH, 444);
} else { /*如果是非可变图像*/
srcImg.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
//desImg.getGraphics().drawImage(srcImg, 0, 0, 0);
//DirectUtils.getDirectGraphics(desImg.getGraphics()).
// getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH, 444);
}
//计算插值表
int[] tabY = new int[desH];
int[] tabX = new int[desW];
int sb = 0;
int db = 0;
int tems = 0;
int temd = 0;
int distance = srcH > desH ? srcH : desH;
for (int i = 0; i <= distance; i++) { /*垂直方向*/
tabY[db] = (short) sb;
tems += srcH;
temd += desH;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
sb = 0;
db = 0;
tems = 0;
temd = 0;
distance = srcW > desW ? srcW : desW;
for (int i = 0; i <= distance; i++) { /*水平方向*/
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
//生成放大缩小后图形像素buf
int[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 0;
int sx = 0;
int sy = 0;
int oldy = -1;
for (int i = 0; i < desH; i++) {
if (oldy == tabY[i]) {
System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
} else {
dx = 0;
for (int j = 0; j < desW; j++) {
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i] - oldy) * srcW;
}
oldy = tabY[i];
dy += desW;
}
//生成图片
//desImg = Image.createImage(desW, desH);
desImg = Image.createRGBImage(desBuf, desW, desH, true);
desBuf = null;
//DirectUtils.getDirectGraphics(desImg.getGraphics()).
//drawPixels(desBuf, true, 0, desW, 0, 0, desW, desH, 0, 444);
return desImg;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -