Java WebCam Streaming over TCP Socket,将图像序列重定向到客户端收件人

时间:2018-05-06 19:09:30

标签: java sockets tcp webcam

提前感谢您的时间和精力! 目前,我正在构建即时消息应用程序,并通过Github的Sarxos Webcam API包含Webcam流功能。 我的应用程序的实现是,只要接受客户端,就会打开运行服务器应用程序的机器操作系统上的另一个线程,并将客户端存储在MySql数据库中。我还没有学习IO多路复用,因此暂时坚持这种实现方式。

关于WebCam流媒体,我能够将图像从客户端应用程序发送到服务器应用程序,服务器应用程序正在接收图像序列并呈现视频,并且没有滞后。

但是,我的麻烦是将这些序列的图像(视频)从服务器重定向到指定的客户端,启动流的客户端希望与之进行视频聊天。

我尝试了太多的实现列表,但这是我当前的代码:

客户申请:

    Thread t1 = new Thread() {
        @Override
        public void run() {
            while (true) {
                BufferedImage frame = webCam.getImage();
                int frameWidth = frame.getWidth();
                int frameHeight = frame.getHeight();

                try {
                    videoOutputStream.writeInt(frameWidth);
                    videoOutputStream.writeInt(frameHeight);

                    int[] pixelData = new int[frameWidth * frameHeight];
                    frame.getRGB(0, 0, frameWidth, frameHeight, pixelData, 0, frameWidth);
                    for (int i = 0; i < pixelData.length; i++) {
                        videoOutputStream.writeInt(pixelData[i]);
                    }
                     Thread.sleep(20);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t1.start();

服务器应用程序:

      BufferedImage frame; 

        while (true) {

    //Client who is SENDING images via webcam is using the "videoInputStream" object 
    //Client who is RECEIVING these images is using the "recipientVideoOutputStream" object 

DataOutputStream recipientVideoOutputStream = c.getVideoOutputStream(); 
                int frameWidth = videoInputStream.readInt(); 
                int frameHeight = videoInputStream.readInt();

                frame = new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_RGB);

                recipientVideoOutputStream.writeInt(frameWidth);
                recipientVideoOutputStream.writeInt(frameHeight);

                int[] pixelData = new int[frameWidth * frameHeight];
                for (int i = 0; i < pixelData.length; i++) {
                    pixelData[i] = videoInputStream.readInt();
                }

                for (int i = 0; i < pixelData.length; i++) {
                    recipientVideoOutputStream.writeInt(pixelData[i]); //This is where we write/re-direct the images as bytes to the receiving client
                }
            }

而且,现在,客户端应用程序的代码是RECEIVING一个视频源,它是发送给客户端服务器的,服务器现在将图像重定向到指定的客户端收件人:

            Thread t1 = new Thread() {
                @Override
                public void run() {
                    int frameWidth;
                    int frameHeight;
                    int[] pixelData;
                    BufferedImage frame;
                    Image imagefx;

                    while (true) {

                        try {
                            frameWidth = videoInputStream.readInt();
                            frameHeight = videoInputStream.readInt();
                            pixelData = new int[frameWidth * frameHeight];

                            for (int i = 0; i < pixelData.length; i++) {
                                pixelData[i] = videoInputStream.readInt();
                                System.out.println("CLIENT READING PIXEL DATA " + pixelData[i]);
                            }

                            frame = new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_RGB);
                            frame.setRGB(0, 0, frameWidth, frameHeight, pixelData, 0, frameWidth);

                            imagefx = SwingFXUtils.toFXImage(frame, null);
                            System.out.println("CLIENT GOT IMAGE " + imagefx);
                            videoStreamStage.updateImage(imagefx); //Sets the image for the client's stage showing the streamed video, this is within a Platform.runLater()
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };

目前,接收客户端在使用NegativeArraySizeException崩溃之前会看到大约1帧,有时还会出现内存堆异常。 我可以毫无问题地传递从客户端 - 服务器渲染视频的图像,但是将视频源重定向到另一个客户端已经吃掉了整个周末,可悲的是没有成功。

(编辑):在客户端应用程序(接收流式图像的客户端)中执行以下行时抛出NegativeArraySizeException:

pixelData = new int[frameWidth * frameHeight];

如何将这些图像重定向到指定的客户端收件人,是否可以实现我的实现?

再次感谢您的时间!

0 个答案:

没有答案
相关问题