/**
*
*/
/**
* @author
*
*/
package com.qunar.wireless.ugc.caravan.business.las.record;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.qunar.wireless.ugc.caravan.jsonbean.las.response.record.inner.BaseRecordInfo;
import com.qunar.wireless.ugc.caravan.jsonbean.las.response.record.inner.CityInfo;
import com.qunar.wireless.ugc.caravan.jsonbean.las.response.record.inner.FeelingInfo;
import com.qunar.wireless.ugc.exception.DaoException;
import com.qunar.wireless.ugc.exception.ServiceException;
import com.qunar.wireless.ugc.jsonbean.request.upload.UploadedPic;
import com.qunar.wireless.ugc.jsonbean.response.pic.inner.AlbumInfo;
import com.qunar.wireless.ugc.jsonbean.response.pic.inner.PicInfo;
import com.qunar.wireless.ugc.module.comon.Suite;
import com.qunar.wireless.ugc.module.pic.Album;
import com.qunar.wireless.ugc.util.AlbumUtils;
import com.qunar.wireless.ugc.util.PicUploadUtil;
import com.qunar.wireless.ugc.util.PicUtil;
import com.qunar.wireless.ugc.util.StringUtils;
import com.qunar.wireless.ugc.util.WebConstants;
@Component(value="mergePic")
public class MergePic{
@Resource
private AlbumUtils albumUtils;
private int headHeight =0;//头像高度
private int headWidth = 0;//头像宽度
private int innerMargin = 7;//头像内部边距
private int leftMargin = 18;//图片距离左边画布距离
private int margin = 30;//图片之间间隔
private int titleHeight = 150;//标题高度
private int lineHeight = 14;//行高
private int mainCanvasHeight =0;//图片和描述的总高度
private int mainCanvasWidth = 450;//主画布宽度
private static final String backgroundUrlString = "http://img1.qunarzz.com/wugc/p170/201209/03/1531e6d21fa15a6a93835fbb.png";//背景图片
private static final String anchorIconString = "http://img1.qunarzz.com/wugc/p245/201209/03/0a22fedb3d530d1393835fbb.png";
private static final String timeIconString = "http://img1.qunarzz.com/wugc/p182/201209/03/24536348de2d6c1a93835fbb.png";//日期图片
private static final String userHeadUrl = "http://source.qunar.com/site/images/wap/lvtu/lvtu_userhead.gif";
private static final Logger log = Logger.getLogger(MergePic.class);
/**
* @param album
* @param visitUserId
* @param injectAllRecordList 是否注入游记中所有记录列表(会过滤删除和黄反状态)
* @param pureText 照片描述/心情描述是否转换为纯文本,true表示@标签转换为@XXX(被@的用户昵称),</br> 客户端仅显示不可点击;false表示在@标签内加入昵称,仍然是标签的形式,客户端可以点击
* @return
* @throws DaoException
* @throws ServiceException
* @return 图片服务器返回的url
*/
public String createMergedPic(Album album, Long userId, boolean injectAllRecordList,boolean pureText) throws DaoException,
ServiceException {
String result = "";
try {
AlbumInfo albumInfo = albumUtils.getAlbumDetail(album, userId, injectAllRecordList,pureText);
//开始分析头像
String headUrl=albumInfo.getUserInfo().getHeadUrl();
if(headUrl==null){
headUrl = userHeadUrl;
}
URL url = new URL(headUrl);
BufferedImage headBufferedImage = ImageIO.read(url);
headHeight = headBufferedImage.getHeight();//得到头像高度
headWidth = headBufferedImage.getWidth();//得到头像宽度
//start------开始读取游记题目-------------------------------------------------------------------------------------
OutputStream outputStream = getTitle(albumInfo);
ByteArrayInputStream byteArrayInputStream = null;
if(outputStream instanceof ByteArrayOutputStream){
byteArrayInputStream = new ByteArrayInputStream( ((ByteArrayOutputStream) outputStream).toByteArray());
}
BufferedImage titleImage = ImageIO.read(byteArrayInputStream);
//开始分析游记
List<BaseRecordInfo> recordInfoList = albumInfo.getRecordList();
log.info("共生成了"+albumInfo.getRecordTotal()+"个图片或文字!");
for(BaseRecordInfo baseRecordInfo:recordInfoList){
if(baseRecordInfo.getType() == Suite.RecordType.PIC.getValue()){//如果是图片
PicInfo pic = (PicInfo)baseRecordInfo;
String picUrl = PicUtil.getMiniUrl(pic.getUrl(), WebConstants.PIC_SIZE_450_5000);
System.out.println(picUrl);
URL pUrl = new URL(picUrl);
BufferedImage bufferedImage = ImageIO.read(pUrl);
int height= bufferedImage.getHeight();//得到图片高度
mainCanvasHeight +=height;
int textheight = StringUtils.getStringLineNum(pic.getDesc(),48);//假设每行最多显示48个字节
mainCanvasHeight +=textheight;//加上图片描述的高度
mainCanvasHeight += lineHeight;//加上最下面表示时间的一行高度
}else if(baseRecordInfo.getType() == Suite.RecordType.FEELING.getValue()){//如果是心情
FeelingInfo feelingInfo = (FeelingInfo)baseRecordInfo;
String desc = feelingInfo.getDesc();
int textheight = StringUtils.getStringLineNum(desc,48);//假设每行最多显示48个字节
mainCanvasHeight +=textheight;//加上图片描述的高度
mainCanvasHeight += lineHeight;//加上最下面表示时间的一行高度
}
}
//开始生成主画布,在上面生成拼图
BufferedImage mainBufferedImage = new BufferedImage(mainCanvasWidth, mainCanvasHeight, BufferedImage.TYPE_INT_RGB);//创建一个画布
Graphics2D graphics2d = (Graphics2D)mainBufferedImage.getGraphics(); //获取画布的画笔
//生成头像栏
graphics2d.setBackground(new Color(236,230,211));//创建背景色
graphics2d.clearRect(0, 0, mainCanvasWidth, mainCanvasHeight);
graphics2d.setPaint(new Color(129,183,83));
graphics2d.fillRect(0, 0, mainCanvasWidth, headHeight+innerMargin*2);
graphics2d.drawImage(headBufferedImage, leftMargin, innerMargin, null);
graphics2d.setPaint(new Color(246,250,242));
graphics2d.setFont(new Font("宋体",Font.BOLD,20));
graphics2d.drawString("", headWidth+innerMargin*2, headHeight/2+innerMargin);//头像右边的字
//生成标题图片
graphics2d.drawImage(titleImage, leftMargin, headHeight+innerMargin*2+margin, null);
int baseHight = headHeight+innerMargin*2+margin+titleImage.getHeight();//标题图片的高度
//开始生成游记的每个图片和心情图片
for(BaseRecordInfo baseRecordInfo:recordInfoList){
OutputStream oStream = getPicText(baseRecordInfo);
if(oStream instanceof ByteArrayOutputStream){
byteArrayInputStream = new ByteArrayInputStream( ((ByteArrayOutputStream) oStream).toByteArray());
}
BufferedImage picTextmage = ImageIO.read(byteArrayInputStream);
baseHight +=margin;
graphics2d.drawImage(picTextmage, leftMargin, baseHight, null);
baseHight +=picTextmage.getHeight();
}
//将生成后的拼图写到服务器share目录下
String random = RandomStringUtils.random(5, true, true);
String fileName = Calendar.getInstance().getTimeInMillis() + random + ".jpg";
String filePath = PicUtil.getShareFileUploadDir() + File.separator + fileName;
File file = new File(filePath);
boolean f = ImageIO.write(mainBufferedImage, "jpg", file);
if(f){
log.info("生成拼图成功!");
}else{
log.info("生成拼图出错!");
}
//将生成的拼图上传到图片服务器中,并返回图片的CDN地址
List<UploadedPic> uploadedPicList = PicUploadUtil.uploadImg(file);
if (uploadedPicList != null && uploadedPicList.size() > 0) {
UploadedPic uploadedPic = uploadedPicList.get(0);
result = uploadedPic != null && org.apache.commons.lang.StringUtils.isNotBlank(uploadedPic.getImg()) ? PicUtil
.getPicCDNUrl(uploadedPic.getImg()) : result;
}
} catch (DaoException e) {
e.printStackTrace();
throw new DaoException();
} catch (ServiceException e) {
e.printStackTrace();
throw new ServiceException();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
private OutputStream getPicText(BaseRecordInfo baseRecordInfo) {
OutputStream outputStream = new ByteArrayOutputStream();
try {
int canvasHeight =0;//主画布高度
int height = 0;
int textheight =0;//描述或心情的高度
BufferedImage bufferedImage = null;
String desc ="";
if(baseRecordInfo.getType() == Suite.RecordType.PIC.getValue()){//如果是图片
PicInfo pic = (PicInfo)baseRecordInfo;
desc = pic.getDesc();
String picUrl = PicUtil.getMiniUrl(pic.getUrl(), WebConstants.PIC_SIZE_450_5000);
URL pUrl = new URL(picUrl);
bufferedImage = ImageIO.read(pUrl);
height= bufferedImage.getHeight();//得到图片高度
canvasHeight +=height;
textheight = StringUtils.getStringLineNum(desc,48);//假设每行最多显示48个字节
canvasHeight +=textheight;//加上图片描述的高度
canvasHeight += lineHeight;//加上最下面表示时间的一行高度
}else if(baseRecordInfo.getType() == Suite.RecordType.FEELING.getValue()){//如果是心情
FeelingInfo feelingInfo = (FeelingInfo)baseRecordInfo;
desc = feelingInfo.getDesc();
textheight = StringUtils.getStringLineNum(desc,48);//假设每行最多显示48个字节
canvasHeight +=textheight;//加上图片描述的高度
canvasHeight += lineHeight;//加上最下面表示时间的一行高度
}
BufferedImage mianBufferedImage = new BufferedImage(mainCanvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);//创建一个画布
Graphics2D graphics2d = (Graphics2D)mianBufferedImage.getGraphics(); //获取画布的画笔
graphics2d.setBackground(new Color(255,255,255));//创建背景色
graphics2d.clearRect(0, 0, mainCanvasWidth, canvasHeight);
if(baseRecordInfo.getType() == Suite.RecordType.PIC.getValue()){//如果是图片
PicInfo pic = (PicInfo)baseRecordInfo;
String time = pic.getCreateTime();
graphics2d.drawImage(bufferedImage, 0, 0, null);//图片
if(desc != null && !desc.equals("")){//描述
graphics2d.setPaint(new Color(60,60,60));
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(desc, leftMargin+4, height);
}
if(pic.getAddressOpenType() ==Suite.WhetherType.YES.getValue()){//POI
URL anchorUrl = new URL(anchorIconString);
BufferedImage anchorBufferedImage = ImageIO.read(anchorUrl);
String addressNameString = pic.getAddressName() +" " +pic.getParentAddressName();
graphics2d.drawImage(anchorBufferedImage, leftMargin, canvasHeight-lineHeight, null);
graphics2d.setPaint(new Color(65,152,167));//地址颜色,TODO
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(addressNameString, leftMargin+4, canvasHeight-lineHeight);
}
if(time != null && !time.equals("")){//时间
URL timerUrl = new URL(timeIconString);
BufferedImage timeBufferedImage = ImageIO.read(timerUrl);
graphics2d.drawImage(timeBufferedImage, 250, canvasHeight-lineHeight, null);//时间位置相对固定的
graphics2d.setPaint(new Color(65,152,167));
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(time, 270, canvasHeight-lineHeight);
}
}else if(baseRecordInfo.getType() == Suite.RecordType.FEELING.getValue()){//如果是心情
FeelingInfo feelingInfo = (FeelingInfo)baseRecordInfo;
String time = feelingInfo.getCreateTime();
graphics2d.setPaint(new Color(60,60,60));
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(desc, 0, 0); //画描述
if(feelingInfo.getAddressOpenType() ==Suite.WhetherType.YES.getValue()){//POI
URL anchorUrl = new URL(anchorIconString);
BufferedImage anchorBufferedImage = ImageIO.read(anchorUrl);
String addressNameString = feelingInfo.getAddressName() +" " +feelingInfo.getParentAddressName();
graphics2d.drawImage(anchorBufferedImage, leftMargin, canvasHeight-lineHeight, null);
graphics2d.setPaint(new Color(65,152,167));//地址颜色,TODO
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(addressNameString, leftMargin+4, canvasHeight-lineHeight);
}
if(time != null && !time.equals("")){//时间
URL timerUrl = new URL(timeIconString);
BufferedImage timeBufferedImage = ImageIO.read(timerUrl);
graphics2d.drawImage(timeBufferedImage, 250, canvasHeight-lineHeight, null);//时间位置相对固定的
graphics2d.setPaint(new Color(65,152,167));
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(time, 270, canvasHeight-lineHeight);
}
}
boolean f = ImageIO.write(mianBufferedImage, "jpg", outputStream);
if(f){
log.info("生成图片成功!");
}else{
log.info("生成图片出错!");
}
} catch (IOException e) {
log.info("生成图片出错!");
e.printStackTrace();
}
return outputStream;
}
public OutputStream getTitle(AlbumInfo albumInfo){
OutputStream outputStream = new ByteArrayOutputStream();
try {
int width = 430;//标题图片的宽度
int height = 150;//标题图片的高度
String title = albumInfo.getName();
title = StringUtils.truncateStr(title,20);
String dateString = albumInfo.getStartDate();
String tripString = ""+albumInfo.getTravelDayNum()+"天旅程";
String cityString = "";
List<CityInfo> cList = albumInfo.getCityList();
for(CityInfo cityInfo:cList){
cityString +=cityInfo.getName()+" ";
}
URL url = new URL(backgroundUrlString);
BufferedImage headBufferedImage = ImageIO.read(url);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建一个画布
Graphics2D graphics2d = (Graphics2D)bufferedImage.getGraphics(); //获取画布的画笔
graphics2d.setBackground(new Color(236,230,211));//创建背景色
graphics2d.clearRect(0, 0, width, height);
graphics2d.drawImage(headBufferedImage, 0, 0, null);
graphics2d.setPaint(new Color(60,60,60));
graphics2d.setFont(new Font("宋体",Font.BOLD,20));
graphics2d.drawString(title, 20, 30);
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(dateString, 45, 70);
graphics2d.drawString(tripString, 200, 70);
graphics2d.drawString(cityString, 30, 100);
boolean f = ImageIO.write(bufferedImage, "jpg", outputStream);
if(f){
log.info("生成图片成功!");
}else{
log.info("生成图片出错!");
}
} catch (IOException e) {
log.info("生成图片出错!");
e.printStackTrace();
}
return outputStream;
}
}