【Java开源代码栏目提醒】:网学会员Java开源代码为您提供BrightnessRetriever.java参考,解决您在BrightnessRetriever.java学习中工作中的难题,参考学习。
import java.awt.image.*;
public class BrightnessRetriever extends PointProcessor
{
float scale = 1.0f;
int lower_valve = 0;
int upper_valve = 255;
public BrightnessRetriever(float scale)
{
this.type = this.PIXEL_BRIGHTNESS_SCALE;
this.scale = scale;
}
public BrightnessRetriever(float scale,int lower,int upper)
{
this.type = this.PIXEL_BRIGHTNESS_SCALE;
this.scale = scale;
if(lower > upper)
{
this.lower_valve = upper;
this.upper_valve = lower;
}
else
{
this.lower_valve = lower;
this.upper_valve = upper;
}
}
public void setRetrieverType(int type)
{
switch(type)
{
case 0: this.type = this.PIXEL_BRIGHTNESS_SCALE;break;
case 1: this.type = this.PIXEL_BRIGHTNESS_REVERSE;break;
default: this.type = this.PIXEL_BRIGHTNESS_SCALE;break;
}
}
public boolean retrieveRGB(int[] src,int scansize,int[] dst)
{
if(scansize == 0)return false;
int height = src.length / scansize;
return (this.retrieveRGB(src,0,0,scansize,height,scansize,dst));
}
public boolean retrieveRGB(int[] src,int x,int y,int w,int h,int scansize,int[] dst)
{
if(scansize <= 0) return false;
int nTotal = src.length;
//原图像的宽度
int srcWidth = scansize;
//原图像可以扫描的总行数:即原图像的高度
int srcHeight = nTotal / scansize;
//作一些合法性准备:越界则不进行任何处理
if((x > (scansize - 1)) || (y > (srcHeight - 1)))return false;
int width = w;
int height = h;
if((x + w) > scansize) width = scansize - x;
if((y + h) > srcHeight) height = srcHeight - y;
System.arraycopy(src,0,dst,0, nTotal);
ColorModel colorModel = ColorModel.getRGBdefault();
int i = 0;int j = 0;
for(i = 0; i < height;i++)
{
int indexBase = (y + i) * scansize;
for(j = 0;j < width;j++)
{
int index = indexBase + x + j;
int r,g,b;
r = colorModel.getRed(src[index]);
g = colorModel.getGreen(src[index]);
b = colorModel.getBlue(src[index]);
dst[index] = retrieve(r,g,b);
}
}
return true;
}
public int retrieve(int r,int g,int b)
{
switch(this.type)
{
//亮度比例化
case this.PIXEL_BRIGHTNESS_SCALE :
r = (int)(r * scale);
g = (int)(g * scale);
b = (int)(b * scale);
r = (r < lower_valve) ? lower_valve : ((r > upper_valve) ? upper_valve : r);
g = (g < lower_valve) ? lower_valve : ((g > upper_valve) ? upper_valve : g);
b = (b < lower_valve) ? lower_valve : ((b > upper_valve) ? upper_valve : b);
break;
//亮度反置
case this.PIXEL_BRIGHTNESS_REVERSE :
r = (255 - r);
g = (255 - g);
b = (255 - b);
break;
default:break;
}
return (0xFF000000 | (( r << 16 ) | ( g << 8 ) | b ));
}
}