1 package org.opencv.highgui;
2 
3 import org.opencv.core.Mat;
4 import org.opencv.core.Size;
5 import org.opencv.imgproc.Imgproc;
6 
7 import javax.swing.*;
8 import java.awt.*;
9 
10 /**
11  * This class was designed to create and manipulate
12  * the Windows to be used by the HighGui class.
13  */
14 public final class ImageWindow {
15 
16     public final static int WINDOW_NORMAL = 0;
17     public final static int WINDOW_AUTOSIZE = 1;
18 
19     public String name;
20     public Mat img = null;
21     public Boolean alreadyUsed = false;
22     public Boolean imgToBeResized = false;
23     public Boolean windowToBeResized = false;
24     public Boolean positionToBeChanged = false;
25     public JFrame frame = null;
26     public JLabel lbl = null;
27     public int flag;
28     public int x = -1;
29     public int y = -1;
30     public int width = -1;
31     public int height = -1;
32 
ImageWindow(String name, Mat img)33     public ImageWindow(String name, Mat img) {
34         this.name = name;
35         this.img = img;
36         this.flag = WINDOW_NORMAL;
37     }
38 
ImageWindow(String name, int flag)39     public ImageWindow(String name, int flag) {
40         this.name = name;
41         this.flag = flag;
42     }
43 
keepAspectRatioSize(int original_width, int original_height, int bound_width, int bound_height)44     public static Size keepAspectRatioSize(int original_width, int original_height, int bound_width, int bound_height) {
45 
46         int new_width = original_width;
47         int new_height = original_height;
48 
49         if (original_width > bound_width) {
50             new_width = bound_width;
51             new_height = (new_width * original_height) / original_width;
52         }
53 
54         if (new_height > bound_height) {
55             new_height = bound_height;
56             new_width = (new_height * original_width) / original_height;
57         }
58 
59         return new Size(new_width, new_height);
60     }
61 
setMat(Mat img)62     public void setMat(Mat img) {
63 
64         this.img = img;
65         this.alreadyUsed = false;
66 
67         if (imgToBeResized) {
68             resizeImage();
69             imgToBeResized = false;
70         }
71 
72     }
73 
setFrameLabelVisible(JFrame frame, JLabel lbl)74     public void setFrameLabelVisible(JFrame frame, JLabel lbl) {
75         this.frame = frame;
76         this.lbl = lbl;
77 
78         if (windowToBeResized) {
79             lbl.setPreferredSize(new Dimension(width, height));
80             windowToBeResized = false;
81         }
82 
83         if (positionToBeChanged) {
84             frame.setLocation(x, y);
85             positionToBeChanged = false;
86         }
87 
88         frame.add(lbl);
89         frame.pack();
90         frame.setVisible(true);
91     }
92 
setNewDimension(int width, int height)93     public void setNewDimension(int width, int height) {
94 
95         if (this.width != width || this.height != height) {
96             this.width = width;
97             this.height = height;
98 
99             if (img != null) {
100                 resizeImage();
101             } else {
102                 imgToBeResized = true;
103             }
104 
105             if (lbl != null) {
106                 lbl.setPreferredSize(new Dimension(width, height));
107             } else {
108                 windowToBeResized = true;
109             }
110         }
111     }
112 
setNewPosition(int x, int y)113     public void setNewPosition(int x, int y) {
114         if (this.x != x || this.y != y) {
115             this.x = x;
116             this.y = y;
117 
118             if (frame != null) {
119                 frame.setLocation(x, y);
120             } else {
121                 positionToBeChanged = true;
122             }
123         }
124     }
125 
resizeImage()126     private void resizeImage() {
127         if (flag == WINDOW_NORMAL) {
128             Size tmpSize = keepAspectRatioSize(img.width(), img.height(), width, height);
129             Imgproc.resize(img, img, tmpSize, 0, 0, Imgproc.INTER_LINEAR_EXACT);
130         }
131     }
132 }
133