import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
/**
* 数据生成图片工具类
*
* @author 甘立冬 2017-3-8
*/
public class PictureUtil extends Canvas {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 创建图片
*
* @param fileLocation
* @param image
*/
private static void createImage(String fileLocation, BufferedImage image) {
try {
String formatName = fileLocation.substring(fileLocation.lastIndexOf(".")+1);
ImageIO.write(image,formatName, new File(fileLocation));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<List<String>> excelDatalist = new ArrayList<List<String>>();
List<String> list1 = new ArrayList<String>();
list1.add("x姓名1");
list1.add("x姓名2");
list1.add("x姓名3");
list1.add("x姓名4");
list1.add("x姓名5");
list1.add("x姓名6");
list1.add("x姓名7");
list1.add("x姓名8");
list1.add("x姓名9");
list1.add("x姓名10");
list1.add("x姓名11");
list1.add("x姓名12");
excelDatalist.add(list1);
for (int i = 0; i <110; i++) {
List<String> dataList = new ArrayList<String>();
dataList.add("呜呜"+i);
dataList.add("发生"+i);
dataList.add("第三方"+i);
dataList.add("呜呜"+i);
dataList.add("发生"+i);
dataList.add("第三方"+i);dataList.add("呜呜"+i);
dataList.add("发生"+i);
dataList.add("第三方"+i);dataList.add("呜呜"+i);
dataList.add("发生"+i);
dataList.add("第三方"+i);
if(excelDatalist.get(0).size()>dataList.size()){
for (int j = 0; j < excelDatalist.get(0).size()-dataList.size(); j++) {
dataList.add(" ");
}
}
excelDatalist.add(dataList);
}
String path ="D://test"+"打开";
int num = 20;
getPage(excelDatalist,num, path);
}
/**
* 获取分页信息
*
* @param list
* @param num
*/
public static List<String> getPage(List<List<String>> list, int num,String path) {
List<String> picUrlList = new ArrayList<String>();
List<List<String>> excelDatalist = new ArrayList<List<String>>();
excelDatalist.add(list.get(0));
int pageNum = list.size() / num;//按照num获取页数
for (int i = 0; i <= pageNum; i++) {
List<List<String>> tempList = new ArrayList<List<String>>();
if (list.size() > num) {
if(i == 0){
for (int j = 0; j < num; j++) {
tempList.add(list.get(j));
}
}else{
tempList.addAll(excelDatalist);
for (int j = 0; j < num-1; j++) {
tempList.add(list.get(j));
}
}
String picUrl = readInData(tempList, path + i + ".jpg");
picUrlList.add(picUrl);//添加生成图片路劲
list.removeAll(tempList);//删除list数据
} else {
for (int k = 0; k < list.size(); k++) {
excelDatalist.add(list.get(k));
}
String picUrl = readInData(excelDatalist, path + i + ".jpg");//添加生成图片路劲
picUrlList.add(picUrl);
}
}
return picUrlList;
}
/**
* 读入数据在画布上
*
* @param num
* @param excelDatalist
* @param path
*/
private static String readInData(List<List<String>> excelDatalist, String path) {
int high = 0;//画布的高度
int wigth = 0;//画布的宽度
int textH = 0;//字符串的高度
List<Integer> imageWList = new ArrayList<Integer>();
List<Integer> strWList = new ArrayList<Integer>();
for (List<String> rowList : excelDatalist) {//遍历excelDatalist数据
wigth = 20;//从画布的宽20开始写入
for (String str : rowList) {
String itemName = str;
JLabel label = new JLabel();
FontMetrics metrics;
label.setText(itemName);//把字符串set到label里面的Text里,为了获取字符串的宽和高。
int textW = 0;//字符串的宽度
metrics = label.getFontMetrics(label.getFont());
textH = metrics.getHeight();//获取字符串的高度
textW = metrics.stringWidth(label.getText());//获取字符串的宽度
strWList.add(textW);
wigth += textW;//字符串的长度
wigth += textW + 20;//画布的长度
}
imageWList.add(wigth);//为了获取最长的字符串的长度
}
//获取画布的高度
for (int i = 0; i < strWList.size() - 1; i++) {
for (int j = 1; j < strWList.size() - i; j++) {
Integer a;
if ((strWList.get(j - 1)).compareTo(strWList.get(j)) > 0) {
a = (Integer) strWList.get(j - 1);
strWList.set((j - 1), strWList.get(j));
strWList.set(j, a);
}
}
}
//获取单个字符串在画布的最长宽度
for (int i = 0; i < imageWList.size() - 1; i++) {
for (int j = 1; j < imageWList.size() - i; j++) {
Integer a;
if ((imageWList.get(j - 1)).compareTo(imageWList.get(j)) > 0) {
a = (Integer) imageWList.get(j - 1);
imageWList.set((j - 1), imageWList.get(j));
imageWList.set(j, a);
}
}
}
int imageHeight = 0;//总高度
if (excelDatalist.size() < 5) {
imageHeight = (textH + 24) * (excelDatalist.size());
} else {
imageHeight = (textH + 20) * (excelDatalist.size()) ;
}
//总宽度
int imageWidth = strWList.get(strWList.size() - 1) * 3 * (excelDatalist.get(0).size() - 1)
+ strWList.get(strWList.size() - 1) * 2;
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.white);//画布颜色
graphics.fillRect(0, 0, imageWidth - 1, imageHeight);
graphics.setColor(Color.black);//行线颜色
graphics.drawLine(2, high, imageWidth - 1, high);//行线
graphics.drawLine(imageWidth - 1, imageHeight, imageWidth - 1, 0);//竖线
graphics.drawLine(2, imageHeight, 2, 0);
((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//循环读入
for (int i = 0; i < excelDatalist.size(); i++) {
graphics.setFont(new Font("宋体", Font.BOLD, 22));
graphics.setColor(Color.black);
if (i == 0) {//每页第一行的目录
graphics.setFont(new Font("宋体", Font.BOLD, 30));
for (int j = 0; j < excelDatalist.get(i + 1).size(); j++) {
String itemName = excelDatalist.get(i).get(j);
graphics.setColor(Color.BLUE);//第一行的背景颜色
graphics.fillRect(1, 1, imageWidth, textH + 28);
graphics.setColor(Color.white);//字体的颜色第一行的
graphics.drawString(itemName, wigth, high);
wigth += strWList.get(strWList.size() - 1) * 3;
}
}
if ((i + 1) % 2 == 0) {//隔行换色的
graphics.setColor(Color.LIGHT_GRAY);
graphics.fillRect(1, high + 12, imageWidth, 35);
graphics.setColor(Color.black);
}
high += 35;
wigth = 2;
graphics.drawLine(2, high + 10, imageWidth - 1, high + 10);
for (int j = 0; j < excelDatalist.get(i).size(); j++) {
System.out.println(excelDatalist.get(i).get(j));
String itemName = excelDatalist.get(i).get(j);
graphics.drawString(itemName, wigth, high);
wigth += strWList.get(strWList.size() - 1) * 3;
}
createImage(path, image);
}
return path;
}
}