java - 无缝视频共享
问题描述
我正在尝试在 LAN 上共享屏幕,但它显示出非常明显的延迟。我尝试了几种技术,但仍然很明显滞后。我尝试了以下技术:
- 从 OutputStream 在套接字上发送帧
- 在使用aspose库发送之前压缩帧(jpeg)
- 将帧添加到队列,然后轮询队列以发送它们
但是尽管尝试了所有方法,但仍然可以清楚地看到滞后。任何人都可以分享他们对如何进一步改进这一点的意见,以便在局域网上共享屏幕时没有/很少滞后?
发件人.java
class SendScreen extends Thread{
Socket socket=null;
Robot robot=null;
Rectangle rectangle=null;
boolean continueLoop=true;
Queue<BufferedImage> q;
OutputStream oos=null;
public SendScreen(Socket socket,Robot robot,Rectangle rect) {
this.socket=socket;
this.robot=robot;
rectangle=rect;
q= new LinkedList<>();
start();
}
public void run(){
try{
oos=socket.getOutputStream();
}catch(IOException ex){
ex.printStackTrace();
}
while(continueLoop){
BufferedImage image=robot.createScreenCapture(rectangle);
try{
/*
ByteArrayOutputStream ous = new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", ous);
InputStream inStream = new ByteArrayInputStream( ous.toByteArray() );
Image original=Image.load(inStream);
JpegOptions jpegOptions = new JpegOptions()
{{
// Apply compression
setCompressionType(JpegCompressionMode.Progressive);
}};
// Save compressed image
ous.flush();*/
q.add(image);
//q.poll();
ImageIO.write( q.poll(),"jpeg",oos);
// original.save(oos);
//ImageIO.write(image,"jpeg",oos);
}catch(IOException ex){
ex.printStackTrace();
}
}
}
}
接收者.java
class ReceiveScreen extends Thread{
private ObjectInputStream cObjectInputStream = null;
private JPanel cPanel = null;
private boolean continueLoop = true;
InputStream oin = null;
Image image1 = null;
public ReceiveScreen(InputStream in,JPanel p){
oin = in;
cPanel = p;
start();
}
public void run(){
try{
//Read screenshots of the client and then draw them
while(continueLoop){
byte[] bytes = new byte[1024*1024];
int count = 0;
do{
count+=oin.read(bytes,count,bytes.length-count);
}while(!(count>4 && bytes[count-2]==(byte)-1 && bytes[count-1]==(byte)-39));
image1 = ImageIO.read(new ByteArrayInputStream(bytes));
image1 = image1.getScaledInstance(cPanel.getWidth(),cPanel.getHeight(),Image.SCALE_FAST);
//Draw the received screenshots
Graphics graphics = cPanel.getGraphics();
graphics.drawImage(image1, 0, 0, cPanel.getWidth(), cPanel.getHeight(), cPanel);
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
解决方案
推荐阅读
- ios - 按钮需要点击两次才能在 iOS 上触发 click 事件
- r - 一个集群的 NbClust
- sql - 在 SQL 中合并两列
- javascript - React Native 中的值属性用于 onClick 条件?
- c++ - 在 QT QNetworkAccessManager 中获取 HTTP 请求中的空响应
- python - Python继承问题
- python - 将 Discord 用户 ID 文件转换为 Name#Discriminator
- apache-spark - spark中将数组列转换为json时如何设置属性名?(无 udf)
- oracle - 从游标值执行 SQL
- html - 如何在路线角度 5 中转义 %2F