告警展示2
对于存在告警信息的网元渲染,今天找到一段代码,基本实现Twaver渲染效果,具体流程如下:
具体代码如下
/**
* 处理图片,覆盖颜色,保持和Twaver一致
* @param image 目标图片
* @param color 要设置的颜色
* @return
*/
public ImageIcon processImage(Image image, Color color) {
if (image != null) {
/*
* * 当我们获得了图像后,可以通过java.awt.image.PixelGrabber包
* 中的PixelGrabber方法来将图像中的像素信息完全读取出来,其用法如下: PixelGrabber(Image img, int
* x, int y, int w, int h, int[] pix, int off, int scansize)
* 其中img是要读取的图像,x/y是要读取图像中的左上角坐标,w/h分别是从x/y开始起的距离,
* 其实x,y,w,h就是一个矩形,pix是保存像素的数组,off是读取图像时开始的位置,scansize是指扫描的宽度,
*一般来说和w相等。
*/
int w = image.getWidth(null);
int h = image.getHeight(null);
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
try {
pg.grabPixels();//读取像素入数组
} catch (InterruptedException ex) {
}
BufferedImage bi = new BufferedImage(w > 1 ? w : 1, h > 1 ? h : 1,
BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i < pixels.length; i++) {
int pixel = pixels[i];
int row = i / w;
int col = i % w;
int alpha = (pixel >> 24) & 0xff;
if (alpha > 0) { // if not transparent pixel
if (color != null) { // if has transformed color
pixel = filterRGB(pixel);
bi.setRGB(col, row, pixel & color.getRGB());
} else {
bi.setRGB(col, row, pixel);
}
}
}
return new ImageIcon(bi);
}
return null;
}
/**
* 灰度值
* @param rgb
* @return
*/
private int filterRGB(int rgb) {
// 由于像素信息是由alpha,red,green,blue组成的,其格式为
// alpha red green blue
// 8bit 8bit 8bit 8bit
// 因此,我们可以将颜色分解成单独的RGB信息
int a = (rgb >>24) & 0xff;
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
rgb = (r * 28 + g * 151 + b * 77) >> 8;
return a | (rgb << 16) | (rgb << 8) | rgb;
}:
注:
什么叫灰度图
任何颜色都有红、绿、蓝三原色组成,假如原来某点的颜色为RGB(R,G,B),那么,我们可以通过下面几种方法,将其转换为灰度:
1.浮点算法:Gray=R*0.3+G*0.59+B*0.11
2.整数方法:Gray=(R*30+G*59++B*11)/100
3.移位方法:Gray =(R*28+G*151+B*77)>>8;
4.平均值法:Gray=(R+G+B)/3;
5.仅取绿色:Gray=G;
通过上述任一种方法求得Gray后,将原来的RGB(R,G,B)中的R,G,B统一用Gray替换,形成新的颜色RGB(Gray,Gray,Gray),用它替换原来的RGB(R,G,B)就是灰度图了。
具体示例效果:
对于传递告警边框渲染,可以通过BufferedImage的画笔画出相关告警颜色的边框,例如:
Graphics2D g = bi.createGraphics();
g.setColor(Color.RED);
g.drawRect(0, 0, w-1, h-1);
具体示例效果: