This is the Server side code which accepts multiple clients.
class Server extends Thread{
@Override
public void run(){
UserArrayList=new ArrayList();
try {
Server=new ServerSocket(1013);
while (true) {
ServerSocket=Server.accept();
StreamVideo StartStreamVideo=new StreamVideo(ServerSocket);
StartStreamVideo.start();
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class StreamVideo extends Thread{
Socket NewSocket;
public StreamVideo(Socket NewSocket) {
this.NewSocket=NewSocket;
WebCam=new VideoCapture(0);
}
@Override
public void run() {
ObjectOutputStream SendMessage = null;
try {
SendMessage = new ObjectOutputStream(NewSocket.getOutputStream());
while(true){
if(WebCam.grab()){
try {
Grabing=true;
WebCam.retrieve(Frame);
Imgcodecs.imencode(".jpg", Frame, mem); //This line gives the error when more than one client is connected
Image Video=ImageIO.read(new ByteArrayInputStream(mem.toArray()));
BufferedImage SendVideo=(BufferedImage) Video;
Graphics DrawImage=jPanel1.getGraphics();
DrawImage.drawImage(SendVideo, 0, 0, getWidth(), getHeight()-100,0,0,SendVideo.getWidth(),SendVideo.getHeight(), null);
img=new ImageIcon(SendVideo);
SendMessage.writeObject(img);
SendMessage.reset();
} catch (IOException ex) {
Logger.getLogger(CamServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}} catch (IOException ex) {
Logger.getLogger(CamServer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
SendMessage.close();
} catch (IOException ex) {
Logger.getLogger(CamServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
This is the client side code which get the Webcam stream form server.
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
String Ip=GetIp();
Socket ClientSocket=new Socket(Ip,1013);
MsgOfServer=new ObjectInputStream(ClientSocket.getInputStream());
GetStream getStream=new GetStream();
getStream.start();
}catch(IOException ex ){
}
}
private String GetIp(){
return JOptionPane.showInputDialog(this, "Server IP Address");
}
class GetStream extends Thread{
@Override
public void run() {
while (true) {
try {
int x=0,y=0;
ImageIcon imageIcon = (ImageIcon) MsgOfServer.readObject();
Image image = imageIcon.getImage();
image = image.getScaledInstance(jPanel1.getWidth(),jPanel1.getHeight(),Image.SCALE_SMOOTH);
Graphics DrawImage=jPanel1.getGraphics();
DrawImage.drawImage(image, 0, 0, jPanel1.getWidth(), jPanel1.getHeight(), null);
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
and the error is
Exception in thread "Thread-6" Exception in thread "Thread-4" java.lang.Exception: unknown exception
at org.opencv.videoio.VideoCapture.retrieve_1(Native Method)
at org.opencv.videoio.VideoCapture.retrieve(VideoCapture.java:161)
at CamServer$StreamVideo.run(CamServer.java:203)
java.lang.Exception: unknown exception
at org.opencv.videoio.VideoCapture.retrieve_1(Native Method)
at org.opencv.videoio.VideoCapture.retrieve(VideoCapture.java:161)
at CamServer$StreamVideo.run(CamServer.java:203)
OpenCV Error: Unknown error code -10 (Raw image encoder error: Empty JPEG image (DNL not supported)) in cv::BaseImageEncoder::throwOnEror, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgcodecs\src\grfmt_base.cpp, line 131
Exception in thread "Thread-7" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgcodecs\src\grfmt_base.cpp:131: error: (-10) Raw image encoder error: Empty JPEG image (DNL not supported) in function cv::BaseImageEncoder::throwOnEror
]
at org.opencv.imgcodecs.Imgcodecs.imencode_1(Native Method)
at org.opencv.imgcodecs.Imgcodecs.imencode(Imgcodecs.java:166)
at CamServer$StreamVideo.run(CamServer.java:204)
Please help me to solve this problem our give another way to do it.