File: SimpleWebcamClient_streaming.java

package info (click to toggle)
robotraconteur 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,380 kB
  • sloc: cpp: 1,149,268; cs: 87,653; java: 58,127; python: 26,897; ansic: 356; sh: 152; makefile: 90; xml: 51
file content (139 lines) | stat: -rw-r--r-- 4,229 bytes parent folder | download
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import com.robotraconteur.*;

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

import org.opencv.core.*;
import org.opencv.imgcodecs.*;

import experimental.simplewebcam3.*;

// Simple client to read streaming images from the Webcam pipe to show
// a live view from the cameras

public class SimpleWebcamClient_streaming
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {

        String url = "rr+tcp://localhost:22355?service=webcam";
        if (args.length > 0)
        {
            url = args[0];
        }

        // Load the opencv library
        nu.pattern.OpenCV.loadShared();

        // Use ClientNodeSetup to initialize node
        ClientNodeSetup setup = new ClientNodeSetup();

        try
        {

            // Register the service type
            RobotRaconteurNode.s().registerServiceType(new experimental__simplewebcam3Factory());

            // Connect to the service
            Webcam c1 = (Webcam)RobotRaconteurNode.s().connectService(url, null, null, null,
                                                                      "experimental.simplewebcam3.Webcam");

            // Connect to the FrameStream pipe and receive a PipeEndpoint
            // PipeEndpoints a symmetric on client and service meaning that
            // you can send and receive on both ends
            Pipe<WebcamImage>.PipeEndpoint p = c1.get_frame_stream().connect(-1);
            // Add a callback for when a new pipe packet is received
            p.addPacketReceivedListener(new new_frame());

            // Start the packets streaming.  If there is an exception ignore it.
            // Exceptions are passed transparently to the client/service.
            try
            {
                c1.start_streaming();
            }
            catch (Exception e)
            {}

            JFrame frame = new JFrame();
            JLabel label = new JLabel();
            frame.setTitle("Live View");
            frame.getContentPane().add(label);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);

            while (true)
            {
                if (current_frame != null)
                {
                    try
                    {
                        MatOfByte matOfByte = new MatOfByte();

                        Imgcodecs.imencode(".bmp", current_frame, matOfByte);
                        byte[] byteArray = matOfByte.toArray();
                        BufferedImage bufImage = null;
                        InputStream in = new ByteArrayInputStream(byteArray);
                        bufImage = ImageIO.read(in);
                        label.setIcon(new ImageIcon(bufImage));
                        frame.pack();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        return;
                    }
                }
                if (!frame.isVisible())
                {
                    break;
                }
                try
                {
                    Thread.sleep(50);
                }
                catch (InterruptedException e)
                {}
            }

            // Close the PipeEndpoint
            p.close();

            // Stop streaming the frame
            c1.stop_streaming();
        }
        finally
        {
            setup.finalize();
        }
    }

    // Convert WebcamImage to OpenCV format
    public static Mat WebcamImageToMat(WebcamImage image)
    {
        Mat mat = new Mat(image.height, image.width, CvType.CV_8UC3);
        mat.put(0, 0, image.data.value);
        return mat;
    }

    public static Mat current_frame;

    // Callback for when a new frame packet is received
    public static class new_frame implements Action1<Pipe<WebcamImage>.PipeEndpoint>
    {
        public void action(Pipe<WebcamImage>.PipeEndpoint pipe_ep)
        {
            while (pipe_ep.available() > 0)
            {
                WebcamImage image = pipe_ep.receivePacket();
                current_frame = WebcamImageToMat(image);
            }
        }
    }
}