package com.xing;
import org.junit.Test;
import sun.font.FontDesignMetrics;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class CreatePhoto {
public void createGraphicsGeneration(String[][] cellsValue, String path) throws IOException {
//字体大小
int fontSize = 15;
int totalrow = cellsValue.length + 1;
//写入图片的宽度
int photoWidth = 32;
int imageWidth = 0;
//行高
int rowheight = 40;
//图片高度
int imageHeight = totalrow * rowheight + 50;
int startHeight = 10;
//使用字体
Font font = new Font("微软雅黑", Font.PLAIN, fontSize);
//保存的列宽
ArrayList<Integer> lengList = new ArrayList<>(cellsValue[0].length);
//列宽初始化
for (int i = 0; i < cellsValue[0].length; i++) {
lengList.add(0);
}
//获取列宽
String cellLeng = "";
for (int j = 0; j < cellsValue[0].length; j++) {
cellLeng = "";
for (int i = 0; i < cellsValue.length; i++) {
if (cellsValue[i][j].endsWith("JPG") || cellsValue[i][j].endsWith("jpg") || cellsValue[i][j].endsWith("PNG") || cellsValue[i][j].endsWith("png")) {
lengList.set(j, photoWidth);
} else {
if (cellsValue[i][j].length() > cellLeng.length()) {
cellLeng = cellsValue[i][j];
lengList.set(j, this.getWordWidth(font, cellLeng));
}
}
}
}
//计算图片宽度
for (Integer integer : lengList) {
imageWidth += integer + 20;
}
//创建图片
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setFont(font);
graphics.setColor(Color.BLACK);
//write content
for (int n = 0; n < cellsValue.length; n++) {
int totalWidth = 0;
for (int l = 0; l < cellsValue[n].length; l++) {
if (cellsValue[n][l].endsWith("JPG") || cellsValue[n][l].endsWith("jpg") || cellsValue[n][l].endsWith("PNG") || cellsValue[n][l].endsWith("png")) {
//Reset picture size
BufferedImage resize = this.resize(cellsValue[n][l], photoWidth, 25);
//write Image
graphics.drawImage(resize, totalWidth, startHeight + rowheight * (n + 2) - 25, null);
} else {
graphics.drawString(cellsValue[n][l], totalWidth, startHeight + rowheight * (n + 2) - 10);
}
//每一次添加的间隔,列宽
totalWidth += lengList.get(l) + 20;
}
}
graphics.dispose();
this.createImage(image, path);
}
//获取字符串的像素宽
private int getWordWidth(Font font, String content) {
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
return metrics.stringWidth(content);
}
//将图片重设大小
private BufferedImage resize(String imgPath, int newW, int newH) throws IOException {
FileInputStream readImage = new FileInputStream(imgPath);
BufferedImage read = ImageIO.read(readImage);
BufferedImage tag = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR);
Graphics g = tag.getGraphics();
g.drawImage(read.getScaledInstance(newW, newH, Image.SCALE_SMOOTH), 0, 0, null);
g.dispose();
readImage.close();
return tag;
}
//写出图片
private void createImage(BufferedImage image, String path) throws IOException {
FileOutputStream fos = new FileOutputStream(path);
//使用PNG提高图片质量
ImageIO.write(image, "PNG", fos);
fos.close();
}
@Test
public void test777() {
try {
String[][] tableData1 =
{{"合肥和巢湖", "469281", "1500000", "D:/1.jpg"},
{"芜湖", "469281", "1500000", "D:/1.jpg"},
{"蚌埠", "469281", "1500000", "D:/1.jpg"},
{"淮南", "469281", "1500000", "D:/1.jpg"},
{"马鞍山", "469281", "1500000", "D:/1.jpg"},
{"淮北", "469281", "1500000", "D:/1.jpg"}};
String[][] tableData2 =
{{"D:/1.jpg", "11111111", "1", "D:/1.jpg"},
{"D:/1.jpg", "22", "1", "D:/1.jpg"},
{"D:/1.jpg", "1", "333", "D:/1.jpg"},
{"D:/1.jpg", "1", "1", "D:/1.jpg"},
{"D:/1.jpg", "1", "1", "D:/1.jpg"},
{"D:/1.jpg", "1", "1", "D:/1.jpg"}};
this.createGraphicsGeneration(tableData2, "D:\\a.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void shape() throws IOException {
// BufferedImage resize = this.resize("D:\\1.JPG", 100, 100);
//
// this.createImage(resize, "D:\\2.JPG");
// ArrayList<Integer> objects = new ArrayList<>(Collections.singleton());
// objects.add(11);
// Integer integer = objects.get(5);
//
// System.out.println("integer = " + integer);
}
}