问题描述
- 如何把这个图片做成heat map 200C
数值越大越红,数值越小越蓝色,周围有虚化效果.
解决方案
大概的实现了下,虚化效果没做^^
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
/**
- 类DrawHeatImage.java的实现描述:TODO 类实现描述
- @author broust 2016年4月4日 下午7:27:44
*/
public class DrawHeatImage {/**
- 绘制热点图
- @param heatList 热点数量
- @param colNum 一行布局多少个热点
- @param gridSize 单元格子的大小--只能绘制正方形
- @return
*/
private static BufferedImage drawHeatImage(List heatList int colNum int gridSize) {// 计算出图像的尺寸
int width = colNum * gridSize;
int height = (int) (heatList.size() / colNum)*gridSize;if (heatList.size() % colNum != 0) {
height += gridSize;
}// 创建bufferedImage
BufferedImage image = new BufferedImage(width height BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
// 底色
g2d.setColor(Color.white);
// 整个画布为白色
g2d.fillRect(0 0 width height);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING RenderingHints.VALUE_ANTIALIAS_ON);// 开始绘制格子
for (int i = 0; i < heatList.size(); i++) {
g2d.setColor(getColor(heatList.get(i)));
int lineN = i / colNum;
int colN = i % colNum;
int x = colN * gridSize;
int y = lineN * gridSize;
g2d.fillRect(x y gridSize gridSize);
}
g2d.dispose();
return image;
}
/**
- 获取颜色
- @param rate [0-255]
- @return/private static Color getColor(int rate) {/*
- 红色:(25500) --->蓝色:(00255) [红,黄,蓝]*/rate = rate < 0 ? 0 : rate > 255 ? 255 : rate;return new Color(rate 0 255 - rate);}
/**
- 保存到文件
- @param filePath
- @param image
- @throws IOException*/private static void storeToFile(String filePath BufferedImage image) throws IOException {File file = new File(filePath);ImageIO.write(imageJPEG"" file);}
/**
- 测试数据
- @param nums
- @return*/private static List testData(int nums) {List list = new ArrayList();Random r = new Random();for (int i = 0; i < nums; i++) { list.add(r.nextInt(nums));}return list;}
public static void main(String[] args) throws IOException {
List heatDataList = testData(100);
String filePath = ""/Users/broust/tmp/xx/heatImage.jpg"";
storeToFile(filePath drawHeatImage(heatDataList 10 20));
}
}
重新写了一个,这个效果行不行?
代码在 http://blog.csdn.net/broust/article/details/51099027