-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighGui.java
195 lines (159 loc) · 6.15 KB
/
HighGui.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* my temporary repair to OpenCV HighGui.java until they fix it */
package app;
import org.opencv.core.Mat;
import org.opencv.highgui.ImageWindow;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This class was designed for use in Java applications
* to recreate the OpenCV HighGui functionalities.
*/
public final class HighGui {
// Constants for namedWindow
public final static int WINDOW_NORMAL = ImageWindow.WINDOW_NORMAL;
public final static int WINDOW_AUTOSIZE = ImageWindow.WINDOW_AUTOSIZE;
// Control Variables
public static int n_closed_windows = 0;
public static AtomicInteger pressedKey = new AtomicInteger(-1);
public static CountDownLatch latch = new CountDownLatch(1);
// Windows Map
public static Map<String, ImageWindow> windows = new HashMap<String, ImageWindow>();
public static void namedWindow(String winname) {
namedWindow(winname, HighGui.WINDOW_AUTOSIZE);
}
public static void namedWindow(String winname, int flag) {
ImageWindow newWin = new ImageWindow(winname, flag);
if (windows.get(winname) == null) windows.put(winname, newWin);
}
public static void imshow(String winname, Mat img) {
if (img.empty()) {
System.err.println("Error: Empty image in imshow");
System.exit(-1);
} else {
ImageWindow tmpWindow = windows.get(winname);
if (tmpWindow == null) {
ImageWindow newWin = new ImageWindow(winname, img);
windows.put(winname, newWin);
} else {
tmpWindow.setMat(img);
}
}
}
public static Image toBufferedImage(Mat m) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if (m.channels() > 1) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels() * m.cols() * m.rows();
byte[] b = new byte[bufferSize];
m.get(0, 0, b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
public static JFrame createJFrame(String title, int flag) {
JFrame frame = new JFrame(title);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
n_closed_windows++;
if (n_closed_windows == windows.size()) latch.countDown();
}
});
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
pressedKey.set(e.getKeyCode());
latch.countDown();
}
});
if (flag == WINDOW_AUTOSIZE) frame.setResizable(false);
return frame;
}
public static void waitKey(){
waitKey(0);
}
public static int waitKey(int delay) {
// Reset control values
latch = new CountDownLatch(1);
n_closed_windows = 0;
// If there are no windows to be shown return
if (windows.isEmpty()) {
System.err.println("Error: waitKey must be used after an imshow");
System.exit(-1);
}
// Remove the unused windows
Iterator<Map.Entry<String,
ImageWindow>> iter = windows.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String,
ImageWindow> entry = iter.next();
ImageWindow win = entry.getValue();
if (win.alreadyUsed) {
iter.remove();
win.frame.dispose();
}
}
// (if) Create (else) Update frame
for (ImageWindow win : windows.values()) {
if (win.img != null) {
ImageIcon icon = new ImageIcon(toBufferedImage(win.img));
if (win.lbl == null) {
JFrame frame = createJFrame(win.name, win.flag);
JLabel lbl = new JLabel(icon);
win.setFrameLabelVisible(frame, lbl);
} else {
win.lbl.setIcon(icon);
}
} else {
System.err.println("Error: no imshow associated with" + " namedWindow: \"" + win.name + "\"");
System.exit(-1);
}
}
try {
if (delay == 0) {
latch.await();
} else {
latch.await(delay, TimeUnit.MILLISECONDS);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// Set all windows as already used
for (ImageWindow win : windows.values())
win.alreadyUsed = true;
return pressedKey.getAndSet(-1);
}
public static void destroyWindow(String winname) {
ImageWindow tmpWin = windows.get(winname);
if (tmpWin != null) windows.remove(winname);
}
public static void destroyAllWindows() {
windows.clear();
}
public static void resizeWindow(String winname, int width, int height) {
ImageWindow tmpWin = windows.get(winname);
if (tmpWin != null) tmpWin.setNewDimension(width, height);
}
public static void moveWindow(String winname, int x, int y) {
ImageWindow tmpWin = windows.get(winname);
if (tmpWin != null) tmpWin.setNewPosition(x, y);
}
}