这是我第一篇文章,本人也是菜鸟,如果有什么不对,也请大神多多指点
话不多说,进入正题。
首先录制视频的前提是推流和拉流同时是连接上才能进行录制工作。否则腾讯云不会给你返回录制的视频地址。
如果你不知道拉流和推流是什么,可以参考腾讯云的api
https://cloud.tencent.com/document/product/267/13551
说到水印就需要有一个生成水印的方法,我的水印是根据字符串动态生成的
package cn.meefo.common.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
/**
* 创建文字图片
* @author yuki_ho
*
*/
public class FontImage {
// 默认格式
private static final String FORMAT_NAME = "JPG";
// 默认 宽度
private static final int WIDTH = 100;
// 默认 高度
private static final int HEIGHT =100;
/**
* 创建图片
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @return
*/
private static BufferedImage createImage(String content,Font font,Integer width,Integer height) throws IOException {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
bi =g2.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2.dispose();
g2=bi.createGraphics();
// BufferedImage image2 = ImageIO.read(new FileInputStream(new File("C:\\apache-tomcat-9.0.13_t\\webapps\\nb_jcy_t\\static\\images\\background\\background.png")));
g2.setBackground(Color.WHITE);
// g2.drawImage(image2, 0, 0, 80, 80,null);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.BLACK);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(content, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(content, (int)x, (int)baseY);
return bi;
}
/**
* 获取 图片
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @return
*/
public static BufferedImage getImage(String content,Font font,Integer width,Integer height) throws IOException {
width=width==null?WIDTH:width;
height=height==null?HEIGHT:height;
if(null==font)
font = new Font("宋体", Font.BOLD, 11);
return createImage(content, font, width, height);
}
/**
* 获取 图片
* @param content 内容
* @param width 宽
* @param height 高
* @return
*/
public static BufferedImage getImage(String content,Integer width,Integer height) throws IOException {
return getImage(content, null,width, height);
}
/**
* 获取图片
* @param content 内容
* @return
*/
public static BufferedImage getImage(String content) throws IOException {
return getImage(content, null, null);
}
/**
* 获取图片
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @param destPath 输出路径
* @throws IOException
*/
public static String getImage(String content,Font font ,Integer width,Integer height,String destPath) throws IOException{
mkdirs(destPath);
String file = UUID.randomUUID().toString()+".jpg";
ImageIO.write(getImage(content,font,width,height),FORMAT_NAME, new File(destPath+"/"+file));
return "https://**********/static/images/watermark/"+file; //返回的是线上图片的地址
}
/**
* 获取图片
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @param output 输出流
* @throws IOException
*/
public static void getImage(String content,Font font,Integer width,Integer height, OutputStream output) throws IOException{
ImageIO.write(getImage(content,font,width,height), FORMAT_NAME, output);
}
/**
* 获取图片
* @param content 内容
* @param width 宽
* @param height 高
* @param des