This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGstRenderer.java
111 lines (92 loc) · 3.28 KB
/
GstRenderer.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
package application;
import java.nio.ByteOrder;
import org.freedesktop.gstreamer.Bin;
import org.freedesktop.gstreamer.Bus;
import org.freedesktop.gstreamer.Caps;
import org.freedesktop.gstreamer.Gst;
import org.freedesktop.gstreamer.Message;
import org.freedesktop.gstreamer.Pipeline;
import org.freedesktop.gstreamer.elements.AppSink;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/*
* The main idea is to create a pipeline that has an appsink to display the images.
* Connect the AppSink to the rest of the pipeline.
* Connect the AppSinkListener to the AppSink.
* The AppSink writes frames to the ImageContainer.
* if you want to display the Videoframes simply add a changeListener to the container who will draw the current
* Image to a Canvas or ImageView.
*/
public class GstRenderer extends Application{
private ImageView imageView;
private AppSink videosink;
private Pipeline pipe;
private Bin bin;
Bus bus;
private StringBuilder caps;
private ImageContainer imageContainer;
public GstRenderer() {
videosink = new AppSink("GstVideoComponent");
videosink.set("emit-signals", true);
AppSinkListener GstListener = new AppSinkListener();
videosink.connect(GstListener);
caps = new StringBuilder("video/x-raw, ");
// JNA creates ByteBuffer using native byte order, set masks according to that.
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
caps.append("format=BGRx");
} else {
caps.append("format=xRGB");
}
videosink.setCaps(new Caps(caps.toString()));
videosink.set("max-buffers", 5000);
videosink.set("drop", true);
bin = Bin.launch("autovideosrc ! videoconvert ", true);
pipe = new Pipeline();
pipe.addMany(bin, videosink);
Pipeline.linkMany(bin, videosink);
imageView = new ImageView();
imageContainer = GstListener.getImageContainer();
imageContainer.addListener(new ChangeListener<Image>() {
@Override
public void changed(ObservableValue<? extends Image> observable, Image oldValue, final
Image newValue) {
Platform.runLater(new Runnable() {
@Override
public void run() {
imageView.setImage(newValue);
}
});
}
});
bus = pipe.getBus();
bus.connect(new Bus.MESSAGE() {
@Override
public void busMessage(Bus arg0, Message arg1) {
System.out.println(arg1.getStructure());
}
});
pipe.play();
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Drawing Operations Test");
BorderPane grid = new BorderPane();
grid.setCenter(imageView);
imageView.fitWidthProperty().bind(grid.widthProperty());
imageView.fitHeightProperty().bind(grid.heightProperty());
imageView.setPreserveRatio(true);
primaryStage.setScene(new Scene(grid, 460, 460));
primaryStage.show();
}
public static void main(String[] args) {
Gst.init("CameraTest",args);
launch(args);
}
}