File: SimpleWebcamClient.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 (95 lines) | stat: -rw-r--r-- 2,657 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
import com.robotraconteur.*;

import java.awt.image.*;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;

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

import experimental.simplewebcam3.*;

// Simple client to read images from a Webcam server
// and display the images

public class SimpleWebcamClient
{

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

        String url = "rr+tcp://localhost:22355?service=multiwebcam";
        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
        {
            RobotRaconteurNode.s().registerServiceType(new experimental__simplewebcam3Factory());

            // Connect to service
            WebcamHost c_host = (WebcamHost)RobotRaconteurNode.s().connectService(
                url, null, null, null, "experimental.simplewebcam3.WebcamHost");

            // Get the Webcam objects from the "Webcams" objref
            Webcam c1 = c_host.get_webcams(0);
            Webcam c2 = c_host.get_webcams(1);

            // Capture an image and convert to OpenCV image type
            Mat frame1 = WebcamImageToMat(c1.capture_frame());
            Mat frame2 = WebcamImageToMat(c2.capture_frame());

            // Show the images
            showImage(c1.get_name(), frame1);
            showImage(c2.get_name(), frame2);
        }
        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 void showImage(String title, Mat img)
    {
        MatOfByte matOfByte = new MatOfByte();

        Imgcodecs.imencode(".bmp", img, matOfByte);
        byte[] byteArray = matOfByte.toArray();
        BufferedImage bufImage = null;
        try
        {
            InputStream in = new ByteArrayInputStream(byteArray);
            bufImage = ImageIO.read(in);
            JFrame frame = new JFrame();
            frame.setTitle(title);
            frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}