基本信息
源码名称:对显示屏播放节目进行插播
源码大小:49.00M
文件格式:.gz
开发语言:Java
更新时间:2021-02-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

对显示屏播放节目进行插播

/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);  
        }
    }
}