-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathEx2MyFirstGUIAppJava.java
179 lines (146 loc) · 5.88 KB
/
Ex2MyFirstGUIAppJava.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
/*
* Copyright (c) 2011-2019 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/
package opencv_cookbook.chapter01;
import org.bytedeco.opencv.opencv_core.Mat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog;
import static opencv_cookbook.OpenCVUtils.toBufferedImage;
import static org.bytedeco.opencv.global.opencv_core.flip;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
import static org.bytedeco.opencv.global.opencv_imgproc.COLOR_BGR2RGB;
import static org.bytedeco.opencv.global.opencv_imgproc.cvtColor;
/**
* The last section in the chapter 1 of the Cookbook demonstrates how to create a simple GUI application.
* The Cookbook is using Qt GUI Toolkit. This example is using Scala Swing to create an similar application.
* <p/>
* The application has two buttons on the left "Open Image" and "Process".
* The opened image is displayed in the middle.
* When "Process" button is pressed the image is flipped upside down and its red and blue channels are swapped.
* <p/>
* Unlike other examples this one is written in Java. It is equivalent to {@link Ex2MyFirstGUIApp} written in Scala.
* It is provided for the sake of comparison of Java to Scala.
*/
public final class Ex2MyFirstGUIAppJava extends JFrame {
private static final long serialVersionUID = 1L;
private final JFileChooser fileChooser = new JFileChooser(new File("."));
/**
* Component for displaying the image
*/
private final JLabel imageView = new JLabel();
/**
* Variable for holding loaded image
*/
private Mat image = null;
private Ex2MyFirstGUIAppJava() throws HeadlessException {
super("My First GUI Java App");
//
// Define actions
//
// Action performed when "Process" button is pressed
final Action processAction = new AbstractAction("Process") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(final ActionEvent e) {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
// Process and update image display if image is loaded
if (image != null) {
processImage(image);
imageView.setIcon(new ImageIcon(toBufferedImage(image)));
} else {
showMessageDialog(Ex2MyFirstGUIAppJava.this, "Image not opened", getTitle(), ERROR_MESSAGE);
}
} finally {
setCursor(Cursor.getDefaultCursor());
}
}
};
processAction.setEnabled(false);
// Action performed when "Open Image" button is pressed
final Action openImageAction = new AbstractAction("Open Image") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(final ActionEvent e) {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
// Load image and update display. If new image was not loaded do nothing.
final Mat img = openImage();
if (img != null) {
image = img;
imageView.setIcon(new ImageIcon(toBufferedImage(image)));
processAction.setEnabled(true);
}
} finally {
setCursor(Cursor.getDefaultCursor());
}
}
};
//
// Create UI
//
// Create button panel
final JPanel buttonsPanel = new JPanel(new GridLayout(0, 1, 0, 5));
buttonsPanel.add(new JButton(openImageAction));
buttonsPanel.add(new JButton(processAction));
// Layout frame contents
// Action buttons on the left
final JPanel leftPane = new JPanel();
leftPane.add(buttonsPanel);
add(leftPane, BorderLayout.WEST);
// Image display in the center
final JScrollPane imageScrollPane = new JScrollPane(imageView);
imageScrollPane.setPreferredSize(new Dimension(640, 480));
add(imageScrollPane, BorderLayout.CENTER);
}
/**
* Ask user for location and open new image.
*
* @return Opened image or {@code null} if image was not loaded.
*/
private Mat openImage() {
// Ask user for the location of the image file
if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
return null;
}
// Load the image
final String path = fileChooser.getSelectedFile().getAbsolutePath();
final Mat newImage = imread(path);
if (newImage != null) {
return newImage;
} else {
showMessageDialog(this, "Cannot open image file: " + path, getTitle(), ERROR_MESSAGE);
return null;
}
}
/**
* Process image in place
*
* @param src image to process.
*/
private void processImage(final Mat src) {
// Flip upside down
flip(src, src, 0);
// Swap red and blue channels
cvtColor(src, src, COLOR_BGR2RGB);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final Ex2MyFirstGUIAppJava frame = new Ex2MyFirstGUIAppJava();
frame.pack();
// Mark for display in the center of the screen
frame.setLocationRelativeTo(null);
// Exit application when frame is closed.
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}