Why the opencv giving this error when multiple clients are connected in java?

asked 2015-11-18 00:30:57 -0600

Hemant Sangle gravatar image

updated 2015-11-18 01:21:06 -0600

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)
edit retag flag offensive close merge delete

Comments

1

you only got 1 videocamera, but you try to open a videocapture with the same id multiple times per client ? that won't work.

imho, you need to put the VideoCapture into a seperate thread, grab images there, and dispatch images to multiple clients

berak gravatar imageberak ( 2015-11-18 00:44:43 -0600 )edit

Ok I will try that

Hemant Sangle gravatar imageHemant Sangle ( 2015-11-18 00:49:50 -0600 )edit

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

Hemant Sangle gravatar imageHemant Sangle ( 2015-11-18 00:52:48 -0600 )edit

@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)
Hemant Sangle gravatar imageHemant Sangle ( 2015-11-18 01:11:36 -0600 )edit
1

and we have to magically guess your current code now ?

berak gravatar imageberak ( 2015-11-18 01:15:31 -0600 )edit

@berak I posted the new code

Hemant Sangle gravatar imageHemant Sangle ( 2015-11-18 01:21:52 -0600 )edit

@berak Thanks man you solved my problem

Hemant Sangle gravatar imageHemant Sangle ( 2015-11-18 01:33:30 -0600 )edit

i don't see any relevant change.

but let's abstract away from the code (more of it won't make it better).

do you understand the original problem ? you can't have a VideoCapture object per client.

berak gravatar imageberak ( 2015-11-18 01:33:44 -0600 )edit
2

@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

Hemant Sangle gravatar imageHemant Sangle ( 2015-11-18 01:38:03 -0600 )edit