java 对比度,java批改图片亮度对比度

java修改图片亮度对比度

我参考了这个博客

http://blog.csdn.net/jia20003/article/details/7385160

以下是我根据这个编的代码

public static int clamp(int value)

{

return value > 255 ? 255 :(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

{

int ta=0,tr=0,tg=0,tb=0;

for(int col=0;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

{

int ta=0,tr=0,tg=0,tb=0;

for(int col=0;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 <

}

}

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

return dest;

}

运行结果是纯黑色,感觉失败了,求教高手

Java SE

分享到:

------解决方案--------------------

rgbmeans[0]=(int)(redSum/total);

rgbmeans[1]=(int)(greenSum/total);

rgbmeans[2]=(int)(blueSum/total);

输出平均值看看。。

另外你

for(int row=0;row> 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?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值