Table2Png||二維數組2Png||java2彩信圖片||java2image



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;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章