实例介绍
【实例简介】
对显示屏播放节目进行插播
【实例截图】
/home/dhr/ScreenCutImage/JiePingChaBo.jpg
【核心代码】
import com.alibaba.fastjson.JSON;
import com.hidisp.api.cloud.models.EmergentSendMode;
import com.hidisp.api.cloud.models.ExecuteMode;
import com.hidisp.api.cloud.models.SendResult;
import com.hidisp.cutScreen.iled.ILEDCloudService;
import com.hidisp.property.PropertyUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
public class CutScreenManage extends Thread{
private static CutScreenManage cutScreenManage =null;
private static final Logger LOG = Logger.getLogger(CutScreenManage.class);
private Object waitObj = null;
private String zoneId = null;
private String savePath = null;
private int num = 1;
private long delay = 0;
private long inteval = 0;
private long chaBoPlayTime = 0;
private long normalPlayTime = 0;
private long cutAndPushTotalTime = 0;
private long cutAndPushAverageTotalTime = 0;
private int startX = 0;
private int startY = 0;
private int width = 0;
private int height = 0;
private String saveCutImagePath = null;
private EmergentSendMode emergentSendMode= null;
private Integer playMode;
private Integer playTime;
private BufferedImage image = null;
private ArrayList<Long> cutAndPushTotalTimeList = null;
private static final String IMAGE_NAME = "JiePingChaBo";
private static final String IMAGE_SUFFIX= "jpg";
public static synchronized CutScreenManage getCutScreenManage(){
if(cutScreenManage ==null){
cutScreenManage = new CutScreenManage();
}
return cutScreenManage;
}
private CutScreenManage() { cutScreenInit();
start();
}
public void cutScreenInit(){
chaBoPlayTime = Long.parseLong(PropertyUtil.getProperty("cutScreen.s.chaBoPlayTime"));
normalPlayTime = Long.parseLong(PropertyUtil.getProperty("cutScreen.s.normalPlayTime"));
startX = Integer.parseInt(PropertyUtil.getProperty("cutScreen.s.startX"));
startY = Integer.parseInt(PropertyUtil.getProperty("cutScreen.s.startY"));
width = Integer.parseInt(PropertyUtil.getProperty("cutScreen.s.width"));
height = Integer.parseInt(PropertyUtil.getProperty("cutScreen.s.hegith"));
zoneId = PropertyUtil.getProperty("cutScreen.s.zoneId");
waitObj = new Object();
savePath = PropertyUtil.getProperty("cutScreen.s.saveCutImagePath");
StringBuffer sb = new StringBuffer();
sb.append(savePath);
sb.append(File.separator);
sb.append(IMAGE_NAME);
sb.append(".");
sb.append(IMAGE_SUFFIX);
saveCutImagePath = sb.toString(); LOG.info("------------SaveCutImagePath:" saveCutImagePath);
emergentSendMode = new EmergentSendMode();
String exMode = PropertyUtil.getProperty("cutScreen.s.executeMode");
if(exMode.equals("Sync")){ emergentSendMode.setExecuteMode(ExecuteMode.Sync); } else { emergentSendMode.setExecuteMode(ExecuteMode.Async); } playMode = Integer.parseInt(PropertyUtil.getProperty("cutScreen.s.playMode")); emergentSendMode.setPlayMode(playMode); playTime = Integer.parseInt(PropertyUtil.getProperty("cutScreen.s.chaBoPlayTime")) /1000; emergentSendMode.setPlayTime(playTime); cutAndPushTotalTimeList = new ArrayList<>(); } public void setNormalPlayTime(long normalPlayTime) { this.normalPlayTime = normalPlayTime; } //多任务管理类线程关闭方法 public void close(){ try { this.interrupt(); this.join(1000); synchronized (waitObj) { waitObj.notify(); } }catch (Exception e){
LOG.error("CutScreenManage close is error.",e); } }
@Override
public void run(){
try { //先进行第一次截屏插播计算截屏插播总消耗时间,间接获得定时任务的时间间隔
if(1 == num){
setCutAndPushProgram();
num ; }
while(!this.isInterrupted()) {
try {
synchronized (waitObj) {
inteval = chaBoPlayTime normalPlayTime;
if(null != cutAndPushTotalTimeList && cutAndPushTotalTimeList.size() > 0){
long consumeTotalTime = 0;
for(int i = 0 ; i < cutAndPushTotalTimeList.size() ; i ){ consumeTotalTime = cutAndPushTotalTimeList.get(i) consumeTotalTime;
}
cutAndPushAverageTotalTime = consumeTotalTime / cutAndPushTotalTimeList.size();
LOG.info("-------------cutAndPushAverageTotalTime:[" cutAndPushAverageTotalTime "]ms");
delay = inteval - cutAndPushTotalTime;
LOG.info("-----delay-----:" delay "ms");
if(delay <= 0){
LOG.error("The time of screen capture exceeds the sum of the time of screen capture and ordinary program");
} else {
waitObj.wait(delay); //Thread.sleep(delay); }
setCutAndPushProgram();
} } }catch (Exception e){ LOG.error(e); }
if(this.isInterrupted()) break; } } catch (Exception e) {
LOG.error("CutScreenManage run error:", e);
} }
public void setCutAndPushProgram(){
try {
long start =System.currentTimeMillis();
Rectangle rect = new Rectangle(startX, startY, width, height);
Robot robot = new Robot();
image = robot.createScreenCapture(rect); //将image写到指定的文件中去
File file = new File(saveCutImagePath);
boolean flag = ImageIO.write(image, IMAGE_SUFFIX, file);
if(flag){ long end01 = System.currentTimeMillis();
long count01 = end01 -start;
LOG.info("Screen capture completed, time consuming:[" count01 "] ms");
LOG.info("Screen capture savePath:" file.getAbsolutePath());
} else { LOG.error("Screen capture failed"); } File[] files = new File[]{file};
HashMap<String, File[]> map = new HashMap<>();
map.put( zoneId,files);
SendResult sendResult = ILEDCloudService.getInstance().sendInfo("all",map, null, emergentSendMode);
if(null != sendResult){
LOG.info("sendResult:" JSON.toJSONString(sendResult));
JSONObject jsonObject = JSONObject.fromObject(sendResult);
JSONObject data = jsonObject.getJSONObject("data"); //正式提取未知的key值
Iterator<String> sIterator = data.keySet().iterator(); //循环并得到key列表
while (sIterator.hasNext()) { // 获得key
String key = sIterator.next(); //获得key值对应的value
String value = data.getString(key);
JSONObject jsonvalue = JSONObject.fromObject(value);
boolean success = jsonvalue.getBoolean("success");
if(success){
long end02 = System.currentTimeMillis();
cutAndPushTotalTime = end02 -start;
cutAndPushTotalTimeList.add(cutAndPushTotalTime);
LOG.info("Push program total time consuming:[" cutAndPushTotalTime "] ms");
} else { String message = jsonvalue.getString("message");
LOG.error("Push program failed :" message);
} } } } catch (Exception e) {
LOG.error("CutScreenManage run error:", e);
} } }
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论