|
| 1 | +package it.polito.elite.teaching.cv; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.util.Timer; |
| 5 | +import java.util.TimerTask; |
| 6 | + |
| 7 | +import javafx.beans.property.ObjectProperty; |
| 8 | +import javafx.beans.property.SimpleObjectProperty; |
| 9 | +import javafx.event.ActionEvent; |
| 10 | +import javafx.fxml.FXML; |
| 11 | +import javafx.scene.control.Button; |
| 12 | +import javafx.scene.image.Image; |
| 13 | +import javafx.scene.image.ImageView; |
| 14 | + |
| 15 | +import org.opencv.core.Mat; |
| 16 | +import org.opencv.core.MatOfByte; |
| 17 | +import org.opencv.highgui.Highgui; |
| 18 | +import org.opencv.highgui.VideoCapture; |
| 19 | +import org.opencv.imgproc.Imgproc; |
| 20 | + |
| 21 | +/** |
| 22 | + * The controller for our application, where the application logic is |
| 23 | + * implemented. It handles the button for starting/stopping the camera and the |
| 24 | + * acquired video stream. |
| 25 | + * |
| 26 | + * @author <a href="mailto:[email protected]">Luigi De Russis</a> |
| 27 | + * @since 2013-10-20 |
| 28 | + * |
| 29 | + */ |
| 30 | +public class FXHelloCVController |
| 31 | +{ |
| 32 | + // the FXML button |
| 33 | + @FXML |
| 34 | + private Button button; |
| 35 | + |
| 36 | + // the main app |
| 37 | + private FXHelloCV mainApp; |
| 38 | + // a timer for acquiring the video stream |
| 39 | + private Timer timer; |
| 40 | + // the OpenCV object that realizes the video capture |
| 41 | + private VideoCapture capture = new VideoCapture(); |
| 42 | + // a flag to change the button behavior |
| 43 | + private boolean cameraActive = false; |
| 44 | + |
| 45 | + /** |
| 46 | + * The action triggered by pushing the button on the GUI |
| 47 | + * |
| 48 | + * @param event |
| 49 | + * the push button event |
| 50 | + */ |
| 51 | + @FXML |
| 52 | + protected void startCamera(ActionEvent event) |
| 53 | + { |
| 54 | + // check: the main class is accessible? |
| 55 | + if (this.mainApp != null) |
| 56 | + { |
| 57 | + // get the ImageView object for showing the video stream |
| 58 | + final ImageView frameView = (ImageView) mainApp.getRootElement().lookup("#currentFrame"); |
| 59 | + // bind an image property with the container for frames |
| 60 | + final ObjectProperty<Image> imageProp = new SimpleObjectProperty<>(); |
| 61 | + frameView.imageProperty().bind(imageProp); |
| 62 | + |
| 63 | + if (!this.cameraActive) |
| 64 | + { |
| 65 | + // start the video capture |
| 66 | + this.capture.open(0); |
| 67 | + |
| 68 | + // is the video stream available? |
| 69 | + if (this.capture.isOpened()) |
| 70 | + { |
| 71 | + this.cameraActive = true; |
| 72 | + |
| 73 | + // grab a frame every 33 ms (30 frames/sec) |
| 74 | + TimerTask frameGrabber = new TimerTask() { |
| 75 | + @Override |
| 76 | + public void run() |
| 77 | + { |
| 78 | + // update the image property => update the frame |
| 79 | + // shown in the UI |
| 80 | + imageProp.set(grabFrame()); |
| 81 | + } |
| 82 | + }; |
| 83 | + this.timer = new Timer(); |
| 84 | + this.timer.schedule(frameGrabber, 0, 33); |
| 85 | + |
| 86 | + // update the button content |
| 87 | + this.button.setText("Stop Camera"); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + // log the error |
| 92 | + System.err.println("Impossible to open the camera connection..."); |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + // the camera is not active at this point |
| 98 | + this.cameraActive = false; |
| 99 | + // update again the button content |
| 100 | + this.button.setText("Start Camera"); |
| 101 | + // stop the timer |
| 102 | + if (this.timer != null) |
| 103 | + { |
| 104 | + this.timer.cancel(); |
| 105 | + this.timer = null; |
| 106 | + } |
| 107 | + // release the camera |
| 108 | + this.capture.release(); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get a frame from the opened video stream (if any) |
| 115 | + * |
| 116 | + * @return the {@link Image} to show |
| 117 | + */ |
| 118 | + private Image grabFrame() |
| 119 | + { |
| 120 | + // init everything |
| 121 | + Image imageToShow = null; |
| 122 | + Mat frame = new Mat(); |
| 123 | + |
| 124 | + // check if the capture is open |
| 125 | + if (this.capture.isOpened()) |
| 126 | + { |
| 127 | + try |
| 128 | + { |
| 129 | + // read the current frame |
| 130 | + this.capture.read(frame); |
| 131 | + |
| 132 | + // if the frame is not empty, process it |
| 133 | + if (!frame.empty()) |
| 134 | + { |
| 135 | + // convert the image to gray scale |
| 136 | + Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY); |
| 137 | + // convert the Mat object (OpenCV) to Image (JavaFX) |
| 138 | + imageToShow = mat2Image(frame); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + catch (Exception e) |
| 143 | + { |
| 144 | + // log the error |
| 145 | + System.err.println("ERROR: " + e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return imageToShow; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Convert a Mat object (OpenCV) in the corresponding Image for JavaFX |
| 154 | + * |
| 155 | + * @param frame |
| 156 | + * the {@link Mat} representing the current frame |
| 157 | + * @return the {@link Image} to show |
| 158 | + */ |
| 159 | + private Image mat2Image(Mat frame) |
| 160 | + { |
| 161 | + // create a temporary buffer |
| 162 | + MatOfByte buffer = new MatOfByte(); |
| 163 | + // encode the frame in the buffer |
| 164 | + Highgui.imencode(".png", frame, buffer); |
| 165 | + // build and return an Image created from the image encoded in the |
| 166 | + // buffer |
| 167 | + return new Image(new ByteArrayInputStream(buffer.toArray())); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Set the reference to the main class of the application |
| 172 | + * |
| 173 | + * @param mainApp |
| 174 | + * the {@FXHelloCV} object to set |
| 175 | + */ |
| 176 | + public void setMainApp(FXHelloCV mainApp) |
| 177 | + { |
| 178 | + this.mainApp = mainApp; |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments