import java.awt.Color
import java.awt.Font
import java.awt.Graphics
import java.awt.image.BufferedImage
import java.io.BufferedOutputStream
import java.io.FileOutputStream
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import java.util.Map
import com.haier.bean.PolicyBean
import com.haier.dto.PolicyPersonDto
import com.haier.dto.PolicyProductDto
import com.sun.image.codec.jpeg.JPEGCodec
import com.sun.image.codec.jpeg.JPEGImageEncoder
public class PictureUtil {
private static void createImage(String fileLocation, BufferedImage image) {
try {
FileOutputStream fos = new FileOutputStream(fileLocation)
BufferedOutputStream bos = new BufferedOutputStream(fos)
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos)
encoder.encode(image)
bos.close()
fos.close()
} catch (Exception e) {
e.printStackTrace()
}
}
public static void graphicsGeneration(String path, PolicyBean policyBean) {
int imageWidth = 1000
int imageHeight = 1000
BufferedImage image = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_RGB)
Graphics graphics = image.getGraphics()
graphics.setColor(Color.white)
graphics.fillRect(0, 0, imageWidth, imageHeight)
graphics.setColor(Color.ORANGE)
graphics.setFont(new Font("宋体", Font.BOLD, 20))
PolicyPersonDto policyPersonDto = policyBean.getPolicyPersonDto()
PolicyProductDto policyProductDto = policyBean.getPolicyProductDto()
int num = 50
graphics.drawString("手机号 : " + policyPersonDto.getMobile(), 50, num)
num += 50
graphics.drawString("登陆密码 : " + policyPersonDto.getPassword(), 50, num)
num += 50
graphics.drawString("身份证号 : " + policyPersonDto.getIdentityCard(), 50, num)
num += 50
graphics.drawString("姓名 : " + policyPersonDto.getUserName(), 50, num)
num += 50
graphics.drawString("所在地区 : " + policyPersonDto.getArea(), 50, num)
num += 50
graphics.drawString("常住地址 : " + policyPersonDto.getAddress(), 50, num)
num += 50
graphics.drawString("微信号 : " + policyPersonDto.getWechatNo(), 50, num)
num += 50
graphics.drawString("QQ号 : " + policyPersonDto.getQq(), 50, num)
num += 50
graphics.drawString("Email : " + policyPersonDto.getEmail(), 50, num)
num += 50
graphics.drawString("品牌 : " + policyProductDto.getBrand(), 50, num)
num += 50
graphics.drawString("型号 : " + policyProductDto.getModel(), 50, num)
num += 50
graphics.drawString("商品编号 : " + policyProductDto.getProductId(), 50, num)
num += 50
graphics.drawString("购买日期 : " + policyProductDto.getPurchaseDate(), 50, num)
num += 50
graphics.drawString("购买价格 : " + policyProductDto.getPurchasePrice(), 50, num)
num += 50
graphics.drawString("购买途径 : " + policyProductDto.getPurchaseWay(), 50, num)
num += 50
graphics.drawString("报修电话 : " + policyProductDto.getWarrantyPhone(), 50, num)
num += 50
graphics.drawString("延保单位 : " + policyProductDto.getExtendedWarrantyUnit(), 50, num)
num += 50
graphics.drawString("延保电话 : " + policyProductDto.getExtendedWarrantyPhone(), 50, num)
num += 50
graphics.drawString("发票编号 : " + policyProductDto.getInvoiceNo(), 50, num)
createImage(path, image)
}
public static void main(String[] args){
List<Map> list = new ArrayList<Map>()
Map<String, String> mapTitle1 = new HashMap<String, String>()
mapTitle1.put("title", "使用人信息")
list.add(mapTitle1)
Map<String, String> map1 = new HashMap<String, String>()
map1.put("客户姓名", "张三")
map1.put("手机号", "123123")
map1.put("身份证号", "230302198811241234")
list.add(map1)
Map<String, String> map2 = new HashMap<String, String>()
map2.put("送货地址", "北京市海淀区知春路113号银网中心B座1009室")
list.add(map2)
Map<String, String> map3 = new HashMap<String, String>()
map3.put("微信号码", "123123")
map3.put("qq号码", "123123")
map3.put("电子邮箱", "gaop@haierubic.com")
list.add(map3)
Map<String, String> mapTitle2 = new HashMap<String, String>()
mapTitle2.put("title", "购买人信息")
list.add(mapTitle2)
Map<String, String> map4 = new HashMap<String, String>()
map4.put("姓名", "张三朋友")
map4.put("手机号", "15612341122")
map4.put("身份证号", "230302198811241234")
list.add(map4)
Map<String, String> mapTitle3 = new HashMap<String, String>()
mapTitle3.put("title", "产品信息")
list.add(mapTitle3)
Map<String, String> map5 = new HashMap<String, String>()
map5.put("产品型号", "ALI88")
map5.put("凭证类型", "发票")
map5.put("购买日期", "2014-12-02")
list.add(map5)
Map<String, String> map6 = new HashMap<String, String>()
map6.put("购买商城", "ALI88")
map6.put("凭证编号", "发票")
list.add(map6)
int imageWidth = 1200
int imageHeight = 1000
BufferedImage image = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_RGB)
Graphics graphics = image.getGraphics()
graphics.setColor(Color.white)
graphics.fillRect(0, 0, imageWidth, imageHeight)
graphics.setColor(Color.black)
int high = 100
int wigth = 0
graphics.setFont(new Font("宋体", Font.BOLD, 50))
graphics.drawString("注册保单", 500, high)
graphics.setFont(new Font("宋体", Font.BOLD, 20))
high += 10
graphics.drawLine(50, high, 1150, high)
for(Map<String, String> rowMap : list){
high += 50
wigth = 50
for(Map.Entry<String, String> entry : rowMap.entrySet()){
String name = entry.getKey() + ":" + entry.getValue()
if("title".equals(entry.getKey())){
high += 50
graphics.setFont(new Font("黑体", Font.BOLD, 30))
graphics.drawString(entry.getValue(), wigth, high)
graphics.setFont(new Font("宋体", Font.BOLD, 20))
} else {
graphics.drawString(name, wigth, high)
wigth += 400
}
}
}
createImage("D://test1.jpg", image)
}
}
public void graphicsGeneration() throws Exception {
int totalrow = 6;
int totalcol = 5;
int imageWidth = 1024;
int imageHeight = totalrow * 40 + 20;
int rowheight = 40;
int startHeight = 10;
int startWidth = 10;
int colwidth = ((imageWidth - 20) / totalcol);
BufferedImage image = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setColor(new Color(220, 240, 240));
for (int j = 0; j < totalrow - 1; j++) {
graphics.setColor(Color.black);
graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight,
imageWidth - 5, startHeight + (j + 1) * rowheight);
}
graphics.setColor(Color.black);
graphics.drawLine(startWidth, imageHeight - 90, imageWidth - 5,
imageHeight - 90);
for (int k = 0; k < totalcol; k++) {
graphics.setColor(Color.black);
graphics.drawLine(startWidth + k * colwidth, startHeight
+ rowheight, startWidth + k * colwidth, imageHeight - 50);
}
graphics.setColor(Color.black);
graphics.drawLine(imageWidth - 5, startHeight + rowheight,
imageWidth - 5, imageHeight - 50);
Font font = new Font("华文楷体", Font.BOLD, 20);
graphics.setFont(font);
String title = "标题写在这里";
graphics.drawString(title, imageWidth / 3 + startWidth, startHeight
+ rowheight - 10);
font = new Font("华文楷体", Font.BOLD, 18);
graphics.setFont(font);
String[] headCells = { "编号", "名称", "年龄", "性别", "体重" };
for (int m = 0; m < headCells.length; m++) {
graphics.drawString(headCells[m].toString(), startWidth + colwidth
* m + 5, startHeight + rowheight * 2 - 10);
}
font = new Font("华文楷体", Font.PLAIN, 16);
graphics.setFont(font);
String[][] cellsValue = { { "101", "xiaozhang", "13", "M", "55" },
{ "102", "xiaowang", "14", "F", "53" },
{ "103", "同事接听,工作信息核实无误,收入纯打卡,无现金发薪同事接听,工作信息核实无误,收入纯打卡,无现金发薪同事接听,工作信息核实无误,收入纯打卡,无现金发薪同事接听,工作信息核实无误,收入纯打卡,无现金发薪", "15", "M", "58" } };
for (int n = 0; n < cellsValue.length; n++) {
String[] arr = cellsValue[n];
for (int l = 0; l < arr.length; l++) {
graphics.drawString(cellsValue[n][l].toString(), startWidth
+ colwidth * l + 5, startHeight + rowheight * (n + 3)
- 10);
}
}
font = new Font("华文楷体", Font.BOLD, 18);
graphics.setFont(font);
graphics.setColor(Color.RED);
String remark = "备注:备注写在这里。";
graphics.drawString(remark, startWidth, imageHeight - 30);
ImageIO.write(image, "jpg", new File("D:\\1.jpg"));
}
/**
* 打印模版
*
* @param imgWidth 图片的宽度
* @param imgHeight 图片的高度
* @param x 起始x轴
* @param y 起始y轴
* @param rowHeight 每行的高度
* @param dataStart 数据缩进
* @param firstWidth 第一列间距x坐标
* @param secondWidth 第二列间距x坐标
* @param thirdWidth 第三列间距x坐标
* @param fourWidth 第四列间距x坐标
* @param tb 实体对象
* @return BufferedImage
*(参数自己定,我的是340, 200, 0, 0, 20, 10, 60, 190, 250, 340, tb)
*/
private BufferedImage createTbGraphics(int imgWidth, int imgHeight, int x, int y, int rowHeight,
int dataStart, int firstWidth, int secondWidth, int thirdWidth, int fourWidth,
Tb tb) {
BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = null;
try {
g = image.createGraphics();
g.setBackground(Color.WHITE);
g.fillRect(x, y, imgWidth, imgHeight);
g.setColor(Color.BLACK);
g.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 12));
g.drawLine(x, y, imgWidth, y);
g.drawLine(x, y, x, rowHeight);
g.drawString("名称", dataStart, rowHeight - 5);
g.drawLine(firstWidth, y, firstWidth, rowHeight);
g.drawString(tb.getName() == null ? "" : tb.getName() , firstWidth + dataStart, rowHeight - 5);
g.drawLine(imgWidth - 1, y, imgWidth - 1, rowHeight);
g.drawLine(x, rowHeight, imgWidth, rowHeight);
g.drawLine(x, rowHeight, x, rowHeight * 2);
g.drawString("代码", dataStart, rowHeight * 2 - 5);
g.drawLine(firstWidth, rowHeight, firstWidth, rowHeight * 2);
g.drawString(tb.getCode() == null ? "" : tb.getCode() , firstWidth + dataStart, rowHeight * 2 - 5);
g.drawLine(secondWidth, rowHeight, secondWidth, rowHeight * 2);
g.drawLine(x, imgHeight - 1, imgWidth, imgHeight - 1);
g.drawImage(ImageIO.read(getServletContext().getResourceAsStream(t.getPicUrl())), null, 198, 61);
g.dispose();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}