-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
69 lines (55 loc) · 2.25 KB
/
Copy pathClient.java
File metadata and controls
69 lines (55 loc) · 2.25 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
69
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.color.*;
import javax.imageio.*;
public class Client {
/*************************************************
*This function change a byte[] to a BufferedImage*
*************************************************/
private static BufferedImage createRGBImage(byte[] bytes, int width, int height) {
DataBufferByte buffer = new DataBufferByte(bytes, bytes.length);
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, Raster.createInterleavedRaster(buffer, width, height, width * 3, 3, new int[]{0, 1, 2}, null), false, null);
}
public static void main(String[] args) throws IOException {
Socket socket = null;
String host = "127.0.0.1";
socket = new Socket(InetAddress.getLocalHost(), 22222);
DataInputStream dIn = new DataInputStream(socket.getInputStream());
while(true){
byte[] recievedMsg;
int length = dIn.readInt();
try {
if (length >0) {
recievedMsg = new byte[length];
dIn.readFully(recievedMsg,0,recievedMsg.length);
//Part wich write the byte array in a file
// FileOutputStream fos = new FileOutputStream(new File("toto.txt"));
// fos.write(recievedMsg);
// fos.close();
//Part wich convert and display the image
BufferedImage recievedImg = createRGBImage(recievedMsg,640,480);
try {
ImageIO.write(recievedImg, "BMP", new File("toto.png"));
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(recievedImg)));
frame.pack();
frame.setVisible(true);
}
finally {
//close the inputStream
dIn.close();
}
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}