forked from varunon9/Remote-Control-PC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenshot.java
More file actions
68 lines (65 loc) · 2.41 KB
/
Screenshot.java
File metadata and controls
68 lines (65 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package filesharing;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import javax.imageio.ImageIO;
/**
*
* @author varun
*/
public class Screenshot {
public void sendScreenshot(final ObjectOutputStream out) {
new Thread() {
@Override
public void run() {
InputStream is = null;
try {
BufferedImage screenshot
= new Robot().createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit()
.getScreenSize()));
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(screenshot, "png", os);
is = new ByteArrayInputStream(os.toByteArray());
int fileSize = os.size();
//sending fileSize first
out.writeObject(fileSize);
out.flush();
System.out.println("screenshot: " + fileSize);
byte[] buffer = new byte[4096];
int read = 0;
long totalRead = 0;
int remaining = (int) fileSize;
while ((read = is.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("Transfer Progress: " + ((totalRead * 100) / fileSize));
out.write(buffer, 0, read);
out.flush();
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}