Ask Your Question

Hemant Sangle's profile - activity

2015-11-18 01:38:03 -0600 commented question Why the opencv giving this error when multiple clients are connected in java?

@berak I done as you said separated the video capture And Created new Thread as LiveThread and started it on formwindowopen event. And after that only sending the images to clients

2015-11-18 01:33:30 -0600 commented question Why the opencv giving this error when multiple clients are connected in java?

@berak Thanks man you solved my problem

2015-11-18 01:21:52 -0600 commented question Why the opencv giving this error when multiple clients are connected in java?

@berak I posted the new code

2015-11-18 01:21:06 -0600 received badge  Editor (source)
2015-11-18 01:11:36 -0600 commented question Why the opencv giving this error when multiple clients are connected in java?

@berakNow Webcam.retrive(Frame) line has this exception

Exception in thread "Thread-1" 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$LiveVideo.run(CamServer.java:141)
2015-11-18 00:52:48 -0600 commented question Why the opencv giving this error when multiple clients are connected in java?

but the capture image should be run in infinite loop and Sending of images also so how could I achieve this to create video

2015-11-18 00:49:50 -0600 commented question Why the opencv giving this error when multiple clients are connected in java?

Ok I will try that

2015-11-18 00:39:34 -0600 asked a question Why the opencv giving this error when multiple clients are connected in java?

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.

Edited Code

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  

    Server server=new ...
(more)