java - 使用java进行屏幕共享
问题描述
我使用 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();
}
}
}
谁能解释一下这个while条件有什么作用?
while(!(count>4 && bytes[count-2]==(byte)-1 && bytes[count-1]==(byte)-39));
查看服务器端。
解决方案
它从套接字读取字节,直到至少有 4 个字节。
然后它检查最后两个字节的幻数,表示图像的结束。
然后它从原始字节创建图像对象。
然后它将图像对象绘制到屏幕上。
(并且它一直重复此操作,直到continuteloop
设置为 false。
你应该学习 DeMorgan 的 therom。它允许重写条件
while(!(count>4 && bytes[count-2]==(byte)-1 && bytes[count-1]==(byte)-39));
是相同的
while ( count < 4 || bytes[count-2] != (byte)-1 || bytes[count-1] != (byte)-39 );
这使条件更加清晰。
- 必须读取四个字节
- 倒数第二个字节必须是 0xFF
- 最后一个字节必须是 0xD9
如果您查看 JPEG 图像规范格式,您会看到 0xFFD9 是一个“JPEG 标记”,它表示“图像流的结束”
因此,此循环有效地从套接字读取 JPEG 图像并显示它,直到continuteloop
标志设置为 false。
推荐阅读
- python - 使用 Python 清除验证码图像的线条噪声
- html - html 对象能否将自身作为参数传递给 JS 函数
- c++ - 为什么通过 BLE 的串行通信在第一次运行代码时失败,但在连续运行时工作正常?
- javascript - 倒数计时器完成后如何自动播放音频?
- php - Symfony 3 - 如何通过 bcrypt 加密的密码对用户进行身份验证?
- c - 如何创建一个从 99% 倒计时到 0% 的计时器,每秒减少 1%
- python - 从 cassandra 获取所有数据
- bash - 用于在其中包含 .git 目录的所有子文件夹中进行 git pull 的代码的 if 语句问题
- android - 如何修复错误:找不到 recyclerview-v7.jar (com.android.support:recyclerview-v7:28.0.0)
- java - 如何从 onClickListener 之外的 onClickListener 获取值