001package com.github.sarxos.webcam.ds.javacv;
002
003import java.awt.Dimension;
004import java.awt.image.BufferedImage;
005import java.io.File;
006
007import org.bytedeco.javacpp.videoInputLib.videoInput;
008import org.bytedeco.javacv.FrameGrabber;
009
010import com.github.sarxos.webcam.WebcamDevice;
011import com.github.sarxos.webcam.WebcamException;
012import com.github.sarxos.webcam.WebcamResolution;
013import com.github.sarxos.webcam.util.OsUtils;
014
015
016/**
017 * UNSTABLE, EXPERIMENTALL STUFF !!!
018 * 
019 * @author bfiryn
020 */
021public class JavaCvDevice implements WebcamDevice {
022
023        private int address = -1;
024        private File vfile = null;
025
026        private String name = null;
027        private FrameGrabber grabber = null;
028
029        private volatile boolean open = false;
030        private volatile boolean disposed = false;
031
032        public JavaCvDevice(int address) {
033                this.address = address;
034        }
035
036        public JavaCvDevice(File vfile) {
037                this.vfile = vfile;
038        }
039
040        @Override
041        public String getName() {
042                if (name == null) {
043                        switch (OsUtils.getOS()) {
044                                case WIN:
045                                        name = videoInput.getDeviceName(address).getString();
046                                        break;
047                                case NIX:
048                                        name = vfile.getAbsolutePath();
049                                        break;
050                                case OSX:
051                                        throw new UnsupportedOperationException("Mac OS is not supported");
052                        }
053                }
054                return name;
055        }
056
057        @Override
058        public Dimension[] getResolutions() {
059                // grabber.get
060
061                throw new WebcamException("Not implemented");
062        }
063
064        @Override
065        public Dimension getResolution() {
066                return WebcamResolution.VGA.getSize();
067        }
068
069        @Override
070        public void setResolution(Dimension size) {
071                throw new WebcamException("Not implemented");
072        }
073
074        @Override
075        public BufferedImage getImage() {
076
077                if (!open) {
078                        throw new WebcamException("Cannot grab image - webcam device is not open");
079                }
080
081                try {
082                        return grabber.grab().getBufferedImage();
083                } catch (Exception e) {
084                        throw new WebcamException("Cannot grab image...");
085                }
086        }
087
088        private FrameGrabber buildGrabber() throws FrameGrabber.Exception {
089                switch (OsUtils.getOS()) {
090                        case WIN:
091                                return FrameGrabber.createDefault(address);
092                        case NIX:
093                                return FrameGrabber.createDefault(vfile);
094                        case OSX:
095                        default:
096                                throw new UnsupportedOperationException("Current OS is not supported");
097                }
098        }
099
100        public FrameGrabber getGrabber() {
101                return grabber;
102        }
103
104        @Override
105        public void open() {
106
107                if (open || disposed) {
108                        return;
109                }
110
111                try {
112
113                        grabber = buildGrabber();
114                        grabber.start();
115
116                        open = true;
117
118                } catch (FrameGrabber.Exception e) {
119                        release();
120                        throw new WebcamException(e);
121                }
122        }
123
124        private void release() {
125                if (grabber != null) {
126                        try {
127                                grabber.release();
128                        } catch (FrameGrabber.Exception e) {
129                                throw new WebcamException(e);
130                        }
131                }
132        }
133
134        @Override
135        public void close() {
136
137                if (!open) {
138                        return;
139                }
140
141                try {
142                        grabber.stop();
143                } catch (FrameGrabber.Exception e) {
144                        throw new WebcamException(e);
145                } finally {
146                        dispose();
147                }
148        }
149
150        @Override
151        public void dispose() {
152                disposed = true;
153        }
154
155        @Override
156        public boolean isOpen() {
157                return open;
158        }
159
160        @Override
161        public String toString() {
162                return getClass().getName() + "#address=" + address + "#vfile=" + vfile;
163        }
164}