图片合成
合成两张大小一样的图片
//读取第一张图片
File fileOne = new File(startPath);
BufferedImage ImageOne = ImageIO.read(fileOne);
int width = ImageOne.getWidth();//图片宽度
int height = ImageOne.getHeight();//图片高度
//从图片中读取RGB
int[] ImageArrayOne = new int[width*height];
ImageArrayOne = ImageOne.getRGB(0,0,width,height,ImageArrayOne,0,width);
//对第二张图片做相同的处理
File fileTwo = new File(endPath);
BufferedImage ImageTwo = ImageIO.read(fileTwo);
int widthTwo = ImageTwo.getWidth();//图片宽度
int heightTwo = ImageTwo.getHeight();//图片高度
int[] ImageArrayTwo = new int[widthTwo*heightTwo];
ImageArrayTwo = ImageTwo.getRGB(0,0,widthTwo,heightTwo,ImageArrayTwo,0,widthTwo);
//生成新图片
BufferedImage imageNew = new BufferedImage(width+widthTwo,height,BufferedImage.TYPE_INT_RGB);
imageNew.setRGB(0,0,width,height,ImageArrayOne,0,width);//设置左半部分的RGB
imageNew.setRGB(width,0,widthTwo,heightTwo,ImageArrayTwo,0,widthTwo);//设置右半部分的RGB
//在图片上写字
Graphics g = imageNew.getGraphics();
Font font = new Font("微软黑体",Font.BOLD,50);
g.setFont(font);
g.setColor(Color.RED);
g.drawString("区间名称:"+caplog.getQjmc()+" 距离:"+caplog.getQjjl()+"m 区间平均速度:"+caplog.getJgcs()+"km/h 限速值:"+caplog.getZdsd()+"km/h 区间超速百分比:"+csbfb, 100, height-100);
g.drawString("区间入口:"+caplog.getQdlkmc()+" 时间:"+caplog.getQdsj()+" 区间出口:"+caplog.getZdlkmc()+" 时间:"+caplog.getZdsj(), 100, height-50);
String newpicName = (sdf.format(new Date())) + ".jpg";
//合成图片的存储位置
String newPath = savePicPath + nowyear + "/" + nowmonth + "/"+newpicName;
File outFile = new File(newPath);
ImageIO.write(imageNew, "jpg", outFile);//写图片
合成两张大小不一样的图片
public static String createNewPic(String qjmc, String jgcs, String csbfb,CLS_VO_Caplog caplog) {
Calendar ca=Calendar.getInstance();
String nowyear = Integer.valueOf(ca.get(Calendar.YEAR)).toString();
String nowmonth = Integer.valueOf((ca.get(Calendar.MONTH)+1)).toString();
String path1=savePicPath+nowyear+"/"+nowmonth+"/start.jpg";
String path2=savePicPath+nowyear+"/"+nowmonth+"/end.jpg";
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
//读取第一张图片
File fileOne = new File(path1);
InputStream in0 = new FileInputStream(fileOne);
BufferedImage ImageOne = ImageIO.read(in0);
//读取第二张图片
File fileTwo = new File(path2);
InputStream in1 = new FileInputStream(fileTwo);
BufferedImage ImageTwo = ImageIO.read(in1);
//左右叠加合成
int width = 0, height = 0;
width = ImageOne.getWidth() + ImageTwo.getWidth();
height = ImageOne.getHeight() >= ImageTwo.getHeight() ? ImageOne.getHeight() : ImageTwo.getHeight();
BufferedImage imageNew = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
int x0 = 0, y0 = 0, widthX0 = ImageOne.getWidth(), heightY0 = ImageOne.getHeight();
int x1 = ImageOne.getWidth(), y1 = 0, widthX1 = ImageTwo.getWidth(), heightY1 = ImageTwo.getHeight();
Graphics g = imageNew.createGraphics();
if(heightY0>heightY1){
g.drawImage(ImageOne, x0, y0, widthX0, heightY0, null);
g.drawImage(ImageTwo, x1, y1, widthX1, heightY0, null);
}else{
g.drawImage(ImageOne, x0, y0, widthX0, heightY1, null);
g.drawImage(ImageTwo, x1, y1, widthX1, heightY1, null);
}
Font font = new Font("微软黑体",Font.BOLD,50);
g.setFont(font);
g.setColor(Color.RED);
g.drawString("区间名称:"+qjmc+" 距离:"+caplog.getQjjl()+"m 区间平均速度:"+jgcs+"km/h 限速值:"+caplog.getXsz()+"km/h 区间超速百分比:"+csbfb, 100, height-100);
g.drawString("区间入口:"+caplog.getQdlkmc()+" 时间:"+caplog.getQdtxsj1()+" 区间出口:"+caplog.getZdlkmc()+" 时间:"+caplog.getZdtxsj1(), 100, height-50);
String newpicName = (sdf.format(new Date()))+".jpg";
File outFile = new File(savePicPath+nowyear+"/"+nowmonth+"/"+newpicName);
ImageIO.write(imageNew, "jpg", outFile);//写图片
String ip = getLinuxLocalIp();
return "http://"+ip+":8000/image/"+httpPicPath+nowyear+"/"+nowmonth+"/"+newpicName;
}catch(Exception e){
e.printStackTrace();
}
return null;
}