java修改图片亮度对比度

CCWinner 2013-10-01 03:41:01
我参考了这个博客
http://blog.csdn.net/jia20003/article/details/7385160
以下是我根据这个编的代码

public static int clamp(int value)
{
return value > 255 ? 255 :(value < 0 ? 0 : value);
}
public static BufferedImage greyFilter(BufferedImage src)
{
//获得源图片长度和宽度
int width=src.getWidth();
int height=src.getHeight();
BufferedImage dest=new BufferedImage(width,height,src.getType());
int[] inPixels=new int[width*height];
int[] outPixels=new int[width*height];
src.getRGB(0,0,width,height,inPixels,0,width);

//计算一个像素的红,绿,蓝方法
int index=0;
int[] rgbmeans=new int[3];
double redSum=0,greenSum=0,blueSum=0;
double total=height*width;
for(int row=0;row<height;row++)
{
int ta=0,tr=0,tg=0,tb=0;
for(int col=0;col<width;col++)
{
index=row*width+col;
ta=(inPixels[index] >> 24) & 0xff;
tr=(inPixels[index] >> 16) & 0xff;
tg=(inPixels[index] >> 8) & 0xff;
tb=inPixels[index] & 0xff;
redSum+=tr;
greenSum+=tg;
blueSum+=tb;
}
}
//求出图像像素平均值
rgbmeans[0]=(int)(redSum/total);
rgbmeans[1]=(int)(greenSum/total);
rgbmeans[2]=(int)(blueSum/total);

//调整对比度,亮度
for(int row=0;row<height;row++)
{
int ta=0,tr=0,tg=0,tb=0;
for(int col=0;col<width;col++)
{
ta=(inPixels[index] >> 24) & 0xff;
tr=(inPixels[index] >> 16) & 0xff;
tg=(inPixels[index] >> 8) & 0xff;
tb=inPixels[index] & 0xff;

//移去平均值
tr -=rgbmeans[0];
tg -=rgbmeans[1];
tb -=rgbmeans[2];

//调整对比度
tr=(int)(tr * contrast);
tg=(int)(tg * contrast);
tb=(int)(tb * contrast);

//调整亮度
tr=(int)((tr+rgbmeans[0])*brightness);
tg=(int)((tg+rgbmeans[1])*brightness);
tb=(int)((tb+rgbmeans[2])*brightness); //end;
/*tr +=(int)(rgbmeans[0] * brightness);
tg +=(int)(rgbmeans[1] * brightness);
tb +=(int)(rgbmeans[2] * brightness); //end;*/

outPixels[index] = (ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);

}
}
dest.setRGB(0, 0, width, height, outPixels, 0, width);
return dest;
}

运行结果是纯黑色,感觉失败了,求教高手
...全文
499 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CCWinner 2013-10-01
  • 打赏
  • 举报
回复
引用 1 楼 AA5279AA 的回复:
rgbmeans[0]=(int)(redSum/total); rgbmeans[1]=(int)(greenSum/total); rgbmeans[2]=(int)(blueSum/total); 输出平均值看看。。 另外你 for(int row=0;row<height;row++) { int ta=0,tr=0,tg=0,tb=0; for(int col=0;col<width;col++) { index=row*width+col; ta=(inPixels[index] >> 24) & 0xff; tr=(inPixels[index] >> 16) & 0xff; tg=(inPixels[index] >> 8) & 0xff; tb=inPixels[index] & 0xff; redSum+=tr; greenSum+=tg; blueSum+=tb; } } 这里没有对index进行操作,那么index一直为0?
刚才在纠错的时候发现了这一点,之前也在质疑index的问题,查了好一会,才发现跟原文确实出了点问题,谢谢回复!
失落夏天 2013-10-01
  • 打赏
  • 举报
回复
rgbmeans[0]=(int)(redSum/total); rgbmeans[1]=(int)(greenSum/total); rgbmeans[2]=(int)(blueSum/total); 输出平均值看看。。 另外你 for(int row=0;row<height;row++) { int ta=0,tr=0,tg=0,tb=0; for(int col=0;col<width;col++) { index=row*width+col; ta=(inPixels[index] >> 24) & 0xff; tr=(inPixels[index] >> 16) & 0xff; tg=(inPixels[index] >> 8) & 0xff; tb=inPixels[index] & 0xff; redSum+=tr; greenSum+=tg; blueSum+=tb; } } 这里没有对index进行操作,那么index一直为0?

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧