96 lines
3.5 KiB
Plaintext
96 lines
3.5 KiB
Plaintext
package com.bonus.core;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Font;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
|
|
import com.sun.image.codec.jpeg.JPEGCodec;
|
|
import com.sun.image.codec.jpeg.JPEGImageEncoder;
|
|
|
|
public class PictureUtils {
|
|
|
|
private BufferedImage image;
|
|
private int imageWidth = 285; //图片的宽度
|
|
private int imageHeight =280; //图片的高度
|
|
//生成图片文件
|
|
@SuppressWarnings("restriction")
|
|
public void createImage(String fileLocation) {
|
|
BufferedOutputStream bos = null;
|
|
if(image != null){
|
|
try {
|
|
FileOutputStream fos = new FileOutputStream(fileLocation);
|
|
bos = new BufferedOutputStream(fos);
|
|
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
|
|
encoder.encode(image);
|
|
bos.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}finally{
|
|
if(bos!=null){//关闭输出流
|
|
try {
|
|
bos.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public long graphicsGeneration(String path,String fileName,String devicecode) {
|
|
String imgurl=path+fileName;
|
|
int H_title = 2; //头部高度
|
|
int H_mainPic = 250; //轮播广告高度
|
|
int H_tip = 1; //上网提示框高度
|
|
int H_btn = 1; //按钮栏的高度
|
|
int tip_2_top = (H_title+H_mainPic);
|
|
int btns_2_top = tip_2_top+H_tip+20;
|
|
int btn1_2_top = btns_2_top-5;
|
|
int btn2_2_top = btn1_2_top+H_btn;
|
|
int shops_2_top = btn2_2_top+H_btn+20;
|
|
int W_btn = 280; //按钮栏的宽度
|
|
//获得当前的时间戳
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
|
|
//设置图片的背景色
|
|
Graphics2D main = image.createGraphics();
|
|
main.setColor(Color.white);
|
|
main.fillRect(0, 0, imageWidth, imageHeight);
|
|
//***********************页面头部
|
|
Graphics title = image.createGraphics();
|
|
title.setColor(Color.white);
|
|
title.fillRect(0, 0, imageWidth, H_title);
|
|
Graphics mainPic = image.getGraphics();
|
|
BufferedImage bimg = null;
|
|
try {
|
|
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
|
|
} catch (Exception e) {}
|
|
|
|
if(bimg!=null){
|
|
mainPic.drawImage(bimg, 0, H_title, imageWidth, H_mainPic, null);
|
|
mainPic.dispose();
|
|
}
|
|
Graphics2D tip = image.createGraphics();
|
|
title.setColor(Color.white);
|
|
tip.fillRect(0, tip_2_top, imageWidth, H_tip);
|
|
Font btnFont = new Font("宋体", Font.BOLD, 30);
|
|
|
|
Graphics2D btn2 = image.createGraphics();
|
|
btn2.fillRect(2, btn2_2_top, W_btn, H_btn);
|
|
btn2.setColor(Color.black);
|
|
//btn2文本
|
|
// btn2.setColor(new Color(39,163,217));
|
|
btn2.setFont(btnFont);
|
|
btn2.drawString(devicecode, 30, btn2_2_top+(H_btn/2)+5);
|
|
createImage(path+currentTimeMillis+".jpg");
|
|
return currentTimeMillis;
|
|
}
|
|
}
|
|
|
|
|