Table2Png||二维数组2Png||java2彩信图片||java2image

来源:互联网 发布:淘宝怎么进我的店铺 编辑:程序博客网 时间:2024/06/05 11:52



package com.htdz.caixin.action;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.imageio.ImageIO;

public class Table2Png {
public static void main(String[] args) {
Abc cg = new Abc();
try {
Color hcolor = new Color(61, 189, 249);
Color scolor = new Color(255, 170, 0);
cg.createImage(new Grid[][]{
{new Grid("时间",hcolor,2,1),new Grid("渠道",hcolor,2,1),new Grid("销售子类型",hcolor,2,1),new Grid("销量",hcolor,2,1),new Grid("**公司",hcolor),new Grid("自有渠道",hcolor),new Grid("社会渠道",hcolor)},
{null,null,null,null,new Grid("平台份额",hcolor),new Grid("份额",hcolor),new Grid("份额",hcolor)},

{new Grid("当日",7,1),new Grid("零售SO",2,1),new Grid("合约机"),new Grid("100"),new Grid("100%",7,1),new Grid("100%",7,1),new Grid("100%",7,1)},
{null,null,new Grid("祼机"),new Grid("200"),null,null,null},
{null,new Grid("铺货SO",3,1),new Grid("自有+省电商铺货"),new Grid("300"),null,null,null},
{null,null,new Grid("直供供售铺货"),new Grid("400"),null,null,null},
{null,null,new Grid("其他"),new Grid("500"),null,null,null},
{null,new Grid("**公司SO(含社会渠道)",scolor,1,2),null,new Grid("600",scolor),null,null,null},
{null,new Grid("**平台SO",scolor,1,2),null,new Grid("700",scolor),null,null,null},

{new Grid("当月",7,1),new Grid("零售SO",2,1),new Grid("合约机"),new Grid("100"),new Grid("100%",7,1),new Grid("100%",7,1),new Grid("100%",7,1)},
{null,null,new Grid("祼机"),new Grid("200"),null,null,null},
{null,new Grid("铺货SO",3,1),new Grid("自有+省电商铺货"),new Grid("300"),null,null,null},
{null,null,new Grid("直供供售铺货"),new Grid("400"),null,null,null},
{null,null,new Grid("其他"),new Grid("500"),null,null,null},
{null,new Grid("**公司SO(含社会渠道)",scolor,1,2),null,new Grid("600",scolor),null,null,null},
{null,new Grid("**平台SO",scolor,1,2),null,new Grid("700",scolor),null,null,null},

{new Grid("全年",7,1),new Grid("零售SO",2,1),new Grid("合约机"),new Grid("100"),new Grid("100%",7,1),new Grid("100%",7,1),new Grid("100%",7,1)},
{null,null,new Grid("祼机"),new Grid("200"),null,null,null},
{null,new Grid("铺货SO",3,1),new Grid("自有+省电商铺货"),new Grid("300"),null,null,null},
{null,null,new Grid("直供供售铺货"),new Grid("400"),null,null,null},
{null,null,new Grid("其他"),new Grid("500"),null,null,null},
{null,new Grid("**公司SO(含社会渠道)",scolor,1,2),null,new Grid("600",scolor),null,null,null},
{null,new Grid("**平台SO",scolor,1,2),null,new Grid("700",scolor),null,null,null},
},"X月X日**公司平台份额");
} catch (Exception e) {
e.printStackTrace();
}
}
}

class Abc{

public void createImage(Grid[][] tdDataArray,String title ) {
// 实际数据行数+标题+备注
int totalrow = 0;
int totalcol = 0;
int imageHeight = 50;//至少50像素高
int imageWidth = 20;//两边各留出10个像素
int rowheight = 30;
int startHeight = 40;
int startWidth = 10;
int wordwidth = 8;//字体宽度


//计算宽度
if(tdDataArray==null||tdDataArray.length<=0||title==null){
return;
}

totalrow = tdDataArray.length;
Grid[] tdDatas = null;
Grid tdData = null;
int currColWith = 0;
Map<Integer,Integer> maxColWidthMap = new HashMap<Integer, Integer>();//记录最大宽度的列

//预处理,算出每一列最大宽度
for (int i=0;i<totalrow;i++) {
tdDatas = tdDataArray[i];
if(tdDatas==null||tdDatas.length==0) continue;
totalcol = tdDatas.length;//设置列数
for(int j=0;j<totalcol;j++){
tdData = tdDatas[j];
if(tdData==null) continue;
currColWith = tdData.getSize()/tdData.getColspan()*wordwidth;
if(maxColWidthMap.get(j)==null){
maxColWidthMap.put(j, currColWith);
}else{
if(maxColWidthMap.get(j)<currColWith){
maxColWidthMap.put(j, currColWith);
}
}
}
}

//计算所有列的宽度(以每一列中最宽的那一列为准)
for (Entry<Integer,Integer> en : maxColWidthMap.entrySet()) {
imageWidth += en.getValue();//图片宽度
}
imageHeight += totalrow * rowheight ;//图片高度

//生成图片
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();

g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, imageWidth, imageHeight);
g2d.setColor(new Color(220, 240, 240));

// 设置字体
Font font = new Font("华文楷体", Font.BOLD, 24);
g2d.setColor(Color.black);
g2d.setFont(font);

// 写标题
FontMetrics fm = null;
int strWidth=0 ;
fm = g2d.getFontMetrics(font);
strWidth = fm.stringWidth(title);// 获取将要绘制的文字宽度
g2d.drawString(title, imageWidth / 2 - strWidth/2, startHeight - 10);

int colspan=0,rowspan = 0,temp_width = 0;
int x1=0,y1=0,x2=startWidth,y2=startHeight;
//计算没个单元格的高度和宽度
for (int i=0;i<totalrow;i++) {
tdDatas = tdDataArray[i];
if(tdDatas==null||tdDatas.length==0) continue;
x2=startWidth;//每次都要重置,因为新的一行,仍然是从最左面开始
for(int j=0;j<totalcol;j++){
tdData = tdDatas[j];
temp_width =0;
if(tdData==null) {
for(int k=0;k<j+1;k++){
temp_width+=maxColWidthMap.get(k);
}
x2 = startWidth+temp_width;//计算该单元格的结束坐标,也就是下个单元格的开始坐标
continue;
}
colspan = tdData.getColspan();
rowspan = tdData.getRowspan();
tdData.setHeight(rowspan*rowheight);
for(int k=0;k<colspan;k++){
temp_width+=maxColWidthMap.get(j+k);
}
tdData.setWidth(temp_width);
x1 = x2;//左边 x坐标
x2 = x1+tdData.getWidth();//右边 x坐标
y1 = startHeight+i*rowheight;//上边 y坐标
y2 = y1+tdData.getHeight();//下边y坐标

// g2d.drawLine(x1, y1, x2, y1);

// 绘制背景色
g2d.setColor(tdData.getBgcolor() == null ? Color.white : tdData.getBgcolor());
g2d.fillRect(x1, y1, x2-x1, y2-y1);

// 绘制边框
g2d.setColor(Color.black);
g2d.setStroke(new BasicStroke(1));
g2d.drawRect(x1, y1, x2-x1, y2-y1);

//写内容
font = tdData.getFont()==null?new Font("华文楷体", Font.BOLD, 18):tdData.getFont();
g2d.setFont(font);
g2d.setColor(tdData.getColor()==null?Color.black:tdData.getColor());
//居中显示
fm = g2d.getFontMetrics(font);
strWidth = fm.stringWidth(tdData.getValue());// 获取将要绘制的文字宽度
g2d.drawString(tdData.getValue(), x1+((x2-x1)/2-strWidth/2),
y1+ (y2-y1-font.getSize()) / 2 + font.getSize());

}
}

try {
ImageIO.write(image, "png", new File("D:/test.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}

class Grid{
private String value ;
private Color color=Color.black;
private Color bgcolor=Color.white;
private Font font=new Font("华文楷体", Font.PLAIN, 16);
private int rowspan=1 ;
private int colspan=1 ;
private int width;
private int height;

public Grid(String value) {
super();
this.value = value;
}

public Grid(String value, Color bgcolor) {
super();
this.value = value;
this.bgcolor = bgcolor;
}

public Grid(String value, int rowspan, int colspan) {
super();
this.value = value;
this.rowspan = rowspan;
this.colspan = colspan;
}

public Grid(String value, Color bgcolor, int rowspan, int colspan) {
super();
this.value = value;
this.bgcolor = bgcolor;
this.rowspan = rowspan;
this.colspan = colspan;
}

public Grid(String value, Color color, Color bgcolor, Font font, int rowspan, int colspan) {
super();
this.value = value;
this.color = color;
this.bgcolor = bgcolor;
this.font = font;
this.rowspan = rowspan;
this.colspan = colspan;
}

public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public int getRowspan() {
return rowspan;
}
public void setRowspan(int rowspan) {
this.rowspan = rowspan;
}
public int getColspan() {
return colspan;
}
public void setColspan(int colspan) {
this.colspan = colspan;
}
public int getWidth() {
return width;
}
public int getSize() {//字节长度
return value==null?0:value.getBytes().length;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
public Color getBgcolor() {
return bgcolor;
}
public void setBgcolor(Color bgcolor) {
this.bgcolor = bgcolor;
}
}
原创粉丝点击