-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraFeed.java
75 lines (66 loc) · 2.2 KB
/
CameraFeed.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2014vision;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_highgui;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameGrabber;
/**
*
* @author Jared "Jär Bär" Gentner
*/
public class CameraFeed {
private static volatile CameraFeed instance;
private FrameGrabber grabber;
private static String feed = "";
private CameraFeed(String feed){
this.feed = feed;
if(feed.matches("^[0-9]+$"))
grabber = new OpenCVFrameGrabber(Integer.parseInt(feed));
else
grabber = new OpenCVFrameGrabber(feed);
try {
grabber.start();
} catch (FrameGrabber.Exception ex) {
Logger.getLogger(CameraFeed.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void Finalize(){
try {
grabber.release();
} catch (FrameGrabber.Exception ex) {
Logger.getLogger(CameraFeed.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static synchronized CameraFeed getFeed(String feed){
if(instance == null)
instance = new CameraFeed(feed);
else
if(feed != CameraFeed.feed)
throw new IllegalArgumentException("CameraFeed can only be instanciated once.");
return instance;
}
public static synchronized CameraFeed getFeed(){
return getFeed(feed);
}
public opencv_core.IplImage getImage(){
try {
return grabber.grab();
} catch (FrameGrabber.Exception ex) {
Logger.getLogger(CameraFeed.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public void writeImage(String loc){
try {
opencv_highgui.cvSaveImage(loc, grabber.grab());
} catch (FrameGrabber.Exception ex) {
Logger.getLogger(CameraFeed.class.getName()).log(Level.SEVERE, null, ex);
}
}
}