/**
*
*/
/**
* @author xingwei.jiao
*
*/
package com.qunar.wireless.ugc.service.cms;
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qunar.wireless.ugc.caravan.business.las.record.MergePic;
import com.qunar.wireless.ugc.exception.DaoException;
import com.qunar.wireless.ugc.exception.ServiceException;
import com.qunar.wireless.ugc.module.pic.Album;
import com.qunar.wireless.ugc.service.pic.AlbumServiceImpl;
import com.qunar.wireless.ugc.service.pic.IAlbumService;
import com.qunar.wireless.ugc.service.user.UserServiceImpl;
public class TestPic{
private ApplicationContext ac;
private IAlbumService albumService;
private MergePic mergePic;
private static final Logger log = Logger.getLogger(TestPic.class);
@Before
public void setUp() throws Exception {
try {
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
albumService = (AlbumServiceImpl) ac.getBean("com.qunar.wireless.ugc.service.pic.AlbumServiceImpl");
mergePic =(MergePic)ac.getBean("mergePic");
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void tearDown() throws Exception {
ac = null;
}
@Test
public void testSharePic(){
Long userId = 88l;
Long albumId = 68l;
try {
Album album = albumService.getById(albumId);
long start = System.currentTimeMillis();
String result = mergePic.createMergedPic(album, userId, true, false);
long end = System.currentTimeMillis();
log.info("生成拼图共花费了"+((end-start)/1000)+"秒!");
log.info("拼图的url: "+result);
System.out.println(result);
} catch (DaoException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
@Test
public void testYSplicePic(){
try {
/* 1 读取第一张图片*/
File fileOne = new File("D:\\test\\2.JPG");
BufferedImage imageFirst = ImageIO.read(fileOne);
int width = imageFirst.getWidth();// 图片宽度
int height = imageFirst.getHeight();// 图片高度
int[] imageArrayFirst = new int[width * height];// 从图片中读取RGB
imageArrayFirst = imageFirst.getRGB(0, 0, width, height, imageArrayFirst, 0, width);
/* 1 对第二张图片做相同的处理 */
File fileTwo = new File("D:\\test\\8.JPG");
BufferedImage imageSecond = ImageIO.read(fileTwo);
height = imageSecond.getHeight();// 图片高度
int[] imageArraySecond = new int[width * height];
imageArraySecond = imageSecond.getRGB(0, 0, width, height, imageArraySecond, 0, width);
// 生成新图片
BufferedImage imageResult = new BufferedImage(width, height * 2,BufferedImage.TYPE_INT_RGB);
imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);// 设置左半部分的RGB
imageResult.setRGB(0, height, width, height, imageArraySecond, 0, width);// 设置右半部分的RGB
File outFile = new File("D:\\test\\out1.jpg");
ImageIO.write(imageResult, "jpg", outFile);// 写图片
System.out.println("合并图片成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testCreateMainPic_File(){
try {
int width = 470;
int height = 1024;
String s = "游记";
File file = new File("D:\\test\\album.JPG");
File head = new File("D:\\test\\head.JPG");
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建一个画布
BufferedImage headBufferedImage = ImageIO.read(head);
Graphics2D graphics2d = (Graphics2D)bufferedImage.getGraphics(); //获取画布的画笔
graphics2d.setBackground(new Color(236,230,211));//创建背景色
graphics2d.clearRect(0, 0, width, height);
graphics2d.setPaint(new Color(129,183,83));
graphics2d.fillRect(0, 0, 400, 100);
graphics2d.drawImage(headBufferedImage, 10, 5, null);
graphics2d.setPaint(new Color(246,250,242));
graphics2d.setFont(new Font("宋体",Font.BOLD,20));
// graphics2d.fillRect(0, 10, 400, 10);
// graphics2d.setPaint(Color.red);
// FontRenderContext context = graphics2d.getFontRenderContext();
// Font font = Font.decode("宋体");
// font = font.deriveFont(Float.parseFloat("12"));
// Rectangle2D rectangle2d = font.getStringBounds(s, context);
// double x = (width - rectangle2d.getWidth()) / 2;
// double y = (height - rectangle2d.getHeight()) / 2;
// double ascent = -rectangle2d.getY();
// double baseY = y + ascent;
graphics2d.drawString(s, 120, 60);
ImageIO.write(bufferedImage, "jpg", file);
System.out.println("生成图片成功!");
} catch (IOException e) {
System.out.println("生成图片出错........");
e.printStackTrace();
}
}
@Test
public void testCreateMainPic(){
try {
int width = 400;
int height = 1024;
String s = "游记";
File file = new File("D:\\test\\album.JPG");
File head = new File("D:\\test\\head.JPG");
File pic1 = new File("D:\\test\\pic1.JPG");
//start------开始读取标题信息,“去那网....”-------------------------------------------------------------------------------------
OutputStream outputStream = getBackgroundPicText();
ByteArrayInputStream byteArrayInputStream = null;
if(outputStream instanceof ByteArrayOutputStream){
byteArrayInputStream = new ByteArrayInputStream( ((ByteArrayOutputStream) outputStream).toByteArray());
}
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建一个画布
BufferedImage headBufferedImage = ImageIO.read(head);
BufferedImage bImage = ImageIO.read(byteArrayInputStream);
BufferedImage pic1Image = ImageIO.read(pic1);
Graphics2D graphics2d = (Graphics2D)bufferedImage.getGraphics(); //获取画布的画笔
graphics2d.setBackground(new Color(236,230,211));//创建背景色
graphics2d.clearRect(0, 0, width, height);
graphics2d.setPaint(new Color(129,183,83));
graphics2d.fillRect(0, 0, width, 100);
graphics2d.drawImage(headBufferedImage, 10, 5, null);
graphics2d.setPaint(new Color(246,250,242));
graphics2d.setFont(new Font("宋体",Font.BOLD,20));
graphics2d.drawString(s, 120, 60);
//start,开始插入标题信息图片--------------------------------------------------------------------------------------------------------
graphics2d.drawImage(bImage, 20, 130, null);
//开始插入图片和文字1
outputStream = getPicText();
if(outputStream instanceof ByteArrayOutputStream){
byteArrayInputStream = new ByteArrayInputStream( ((ByteArrayOutputStream) outputStream).toByteArray());
}
bImage = ImageIO.read(byteArrayInputStream);
graphics2d.drawImage(bImage, 20, 300, null);
//开始生成图片
ImageIO.write(bufferedImage, "jpg", file);
graphics2d.dispose();
System.out.println("生成图片成功!");
} catch (IOException e) {
System.out.println("生成图片出错........");
e.printStackTrace();
}
}
@Test
public void testCreateBackgroundPicText(){
try {
int width = 470;
int height = 125;
String s1 = "【去哪儿网旅图】每日最佳图片1111";
String dateString = "2012-08-21";
String tripString = "29天旅程";
String addressString = "北京 上海 广州 山东";
File result = new File("D:\\test\\textHead.JPG");
File background = new File("D:\\test\\backgroundTextHead.JPG");
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建一个画布
BufferedImage headBufferedImage = ImageIO.read(background);
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.fillRect(0, 10, 400, 10);
// graphics2d.setPaint(Color.red);
// FontRenderContext context = graphics2d.getFontRenderContext();
// Font font = Font.decode("宋体");
// font = font.deriveFont(Float.parseFloat("12"));
// Rectangle2D rectangle2d = font.getStringBounds(s, context);
// double x = (width - rectangle2d.getWidth()) / 2;
// double y = (height - rectangle2d.getHeight()) / 2;
// double ascent = -rectangle2d.getY();
// double baseY = y + ascent;
graphics2d.drawString(s1, 20, 30);
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(dateString, 45, 70);
graphics2d.drawString(tripString, 200, 70);
graphics2d.drawString(addressString, 30, 100);
ImageIO.write(bufferedImage, "jpg", result);
graphics2d.dispose();
System.out.println("生成图片成功!");
} catch (IOException e) {
System.out.println("生成图片出错........");
e.printStackTrace();
}
}
public OutputStream getBackgroundPicText(){
OutputStream outputStream = new ByteArrayOutputStream();
try {
int width = 470;
int height = 125;
String s1 = "【去哪儿网旅图】每日最佳图片";
String dateString = "2012-08-21";
String tripString = "29天旅程";
String addressString = "北京 上海 广州 山东";
File result = new File("D:\\test\\textHead.JPG");
File background = new File("D:\\test\\backgroundTextHead.JPG");
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建一个画布
BufferedImage headBufferedImage = ImageIO.read(background);
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.fillRect(0, 10, 400, 10);
// graphics2d.setPaint(Color.red);
// FontRenderContext context = graphics2d.getFontRenderContext();
// Font font = Font.decode("宋体");
// font = font.deriveFont(Float.parseFloat("12"));
// Rectangle2D rectangle2d = font.getStringBounds(s, context);
// double x = (width - rectangle2d.getWidth()) / 2;
// double y = (height - rectangle2d.getHeight()) / 2;
// double ascent = -rectangle2d.getY();
// double baseY = y + ascent;
graphics2d.drawString(s1, 20, 30);
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(dateString, 45, 70);
graphics2d.drawString(tripString, 200, 70);
graphics2d.drawString(addressString, 30, 100);
boolean f = ImageIO.write(bufferedImage, "jpg", outputStream);
if(f){
System.out.println("生成图片成功!");
}else{
System.out.println("生成图片出错........");
}
} catch (IOException e) {
System.out.println("生成图片出错........");
e.printStackTrace();
}
return outputStream;
}
public OutputStream getPicText(){
OutputStream outputStream = new ByteArrayOutputStream();
try {
int width = 470;
int height = 400;//传进来的
String desc = "描述信息haha的绝对狙击得紧紧的...........";
String addressString = "北京 上海 广州 山东";
String dateString = "2012-08-21";
String pic1String = "";
File anchor = new File("D:\\test\\anchor.JPG");
File time = new File("D:\\test\\time.JPG");
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建一个画布
Graphics2D graphics2d = (Graphics2D)bufferedImage.getGraphics(); //获取画布的画笔
graphics2d.setBackground(new Color(255,255,255));//创建背景色
graphics2d.clearRect(0, 0, width, height);
if(pic1String != null && !pic1String.equals("")){
File pic1 = new File(pic1String);
BufferedImage pic1BufferedImage = ImageIO.read(pic1);
graphics2d.drawImage(pic1BufferedImage, 10, 5, null);
}
if(desc != null && !desc.equals("")){
graphics2d.setPaint(new Color(60,60,60));
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(desc, 20, 30); //30,是自动算出来
}
if(addressString != null && !addressString.equals("")){//画锚点跟地址
BufferedImage anchorBufferedImage = ImageIO.read(anchor);
graphics2d.drawImage(anchorBufferedImage, 10, 310, null);
graphics2d.setPaint(new Color(65,152,167));
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(addressString, 20, 320);
}
if(dateString != null && !dateString.equals("")){//时间
BufferedImage timeBufferedImage = ImageIO.read(time);
graphics2d.drawImage(timeBufferedImage, 250, 310, null);
graphics2d.setPaint(new Color(65,152,167));
graphics2d.setFont(new Font("宋体",Font.PLAIN,14));
graphics2d.drawString(dateString, 270, 320);
}
boolean f = ImageIO.write(bufferedImage, "jpg", outputStream);
if(f){
System.out.println("生成图片成功!");
}else{
System.out.println("生成图片出错........");
}
} catch (IOException e) {
System.out.println("生成图片出错........");
e.printStackTrace();
}
return outputStream;
}
@Test
public void getHeadHeight(){
String headUrl = "http://source.qunar.com/site/images/wap/lvtu/lvtu_userhead.gif";
try {
URL url = new URL(headUrl);
BufferedImage headBufferedImage = ImageIO.read(url);
int height = headBufferedImage.getHeight();
System.out.println("height:"+height);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}