001package org.opencv.highgui;
002
003import org.opencv.core.Mat;
004import org.opencv.core.Size;
005import org.opencv.imgproc.Imgproc;
006
007import javax.swing.*;
008import java.awt.*;
009
010/**
011 * This class was designed to create and manipulate
012 * the Windows to be used by the HighGui class.
013 */
014public final class ImageWindow {
015
016    public final static int WINDOW_NORMAL = 0;
017    public final static int WINDOW_AUTOSIZE = 1;
018
019    public String name;
020    public Mat img = null;
021    public Boolean alreadyUsed = false;
022    public Boolean imgToBeResized = false;
023    public Boolean windowToBeResized = false;
024    public Boolean positionToBeChanged = false;
025    public JFrame frame = null;
026    public JLabel lbl = null;
027    public int flag;
028    public int x = -1;
029    public int y = -1;
030    public int width = -1;
031    public int height = -1;
032
033    public ImageWindow(String name, Mat img) {
034        this.name = name;
035        this.img = img;
036        this.flag = WINDOW_NORMAL;
037    }
038
039    public ImageWindow(String name, int flag) {
040        this.name = name;
041        this.flag = flag;
042    }
043
044    public static Size keepAspectRatioSize(int original_width, int original_height, int bound_width, int bound_height) {
045
046        int new_width = original_width;
047        int new_height = original_height;
048
049        if (original_width > bound_width) {
050            new_width = bound_width;
051            new_height = (new_width * original_height) / original_width;
052        }
053
054        if (new_height > bound_height) {
055            new_height = bound_height;
056            new_width = (new_height * original_width) / original_height;
057        }
058
059        return new Size(new_width, new_height);
060    }
061
062    public void setMat(Mat img) {
063
064        this.img = img;
065        this.alreadyUsed = false;
066
067        if (imgToBeResized) {
068            resizeImage();
069            imgToBeResized = false;
070        }
071
072    }
073
074    public void setFrameLabelVisible(JFrame frame, JLabel lbl) {
075        this.frame = frame;
076        this.lbl = lbl;
077
078        if (windowToBeResized) {
079            lbl.setPreferredSize(new Dimension(width, height));
080            windowToBeResized = false;
081        }
082
083        if (positionToBeChanged) {
084            frame.setLocation(x, y);
085            positionToBeChanged = false;
086        }
087
088        frame.add(lbl);
089        frame.pack();
090        frame.setVisible(true);
091    }
092
093    public void setNewDimension(int width, int height) {
094
095        if (this.width != width || this.height != height) {
096            this.width = width;
097            this.height = height;
098
099            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
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
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}