001package com.github.sarxos.webcam.ds.javacv;
002
003import java.io.File;
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.List;
007
008import org.bytedeco.javacpp.videoInputLib.videoInput;
009
010import com.github.sarxos.webcam.WebcamDevice;
011import com.github.sarxos.webcam.WebcamDriver;
012import com.github.sarxos.webcam.util.NixVideoDevUtils;
013
014
015/**
016 * Webcam driver using JavaCV interface to OpenCV. OpenCV (Open Source Computer
017 * Vision Library) is library of programming functions for real time computer
018 * vision. JavaCV provides wrappers to commonly used libraries for OpenCV and
019 * few others.
020 * 
021 * UNSTABLE, EXPERIMENTALL STUFF !!!
022 * 
023 * @author Bartosz Firyn (SarXos)
024 */
025public class JavaCvDriver implements WebcamDriver {
026
027        private List<WebcamDevice> getDevicesWindows() {
028                List<WebcamDevice> devices = new ArrayList<WebcamDevice>();
029                int n = videoInput.listDevices();
030                if (n > 0) {
031                        for (int i = 0; i < n; i++) {
032                                devices.add(new JavaCvDevice(i));
033                        }
034                } else {
035                        devices = Collections.emptyList();
036                }
037                return devices;
038        }
039
040        private List<WebcamDevice> getDevicesLinux() {
041                List<WebcamDevice> devices = new ArrayList<WebcamDevice>();
042                for (File vfile : NixVideoDevUtils.getVideoFiles()) {
043                        devices.add(new JavaCvDevice(vfile));
044                }
045                return devices;
046        }
047
048        @Override
049        public List<WebcamDevice> getDevices() {
050                boolean linux = System.getProperty("os.name").toLowerCase().indexOf("linux") != -1;
051                if (linux) {
052                        return getDevicesLinux();
053                } else {
054                        return getDevicesWindows();
055                }
056        }
057
058        public static void main(String[] args) {
059                for (WebcamDevice d : new JavaCvDriver().getDevices()) {
060                        System.out.println(d);
061                }
062        }
063
064        @Override
065        public boolean isThreadSafe() {
066                return false;
067        }
068
069        @Override
070        public String toString() {
071                return getClass().getSimpleName();
072        }
073}