Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasEeeE authored Dec 4, 2016
1 parent 7c725cd commit 4dd4abc
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
68 changes: 68 additions & 0 deletions AppSinkListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package application;

import java.nio.ByteBuffer;

import org.freedesktop.gstreamer.Buffer;
import org.freedesktop.gstreamer.FlowReturn;
import org.freedesktop.gstreamer.Sample;
import org.freedesktop.gstreamer.Structure;
import org.freedesktop.gstreamer.elements.AppSink;


import javafx.scene.image.Image;
import javafx.scene.image.PixelFormat;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;


public class AppSinkListener implements AppSink.NEW_SAMPLE {

private Image actualFrame;
private int lastWidth = 0;
private int lastHeigth = 0;
private byte[] byteArray;
private ImageContainer imageContainer = new ImageContainer();

public ImageContainer getImageContainer(){
return imageContainer;
}

@Override
public FlowReturn newSample(AppSink appSink) {
// Try to get a sample
Sample sample = appSink.pullSample();
Buffer buffer = sample.getBuffer();
ByteBuffer byteBuffer = buffer.map(false);
if (byteBuffer != null){
Structure capsStruct = sample.getCaps().getStructure(0);
int width = capsStruct.getInteger("width");
int height = capsStruct.getInteger("height");
if (width != lastWidth || height != lastHeigth){
lastWidth = width;
lastHeigth = height;
byteArray = new byte[width * height * 4];
}
// Writes the buffer to the byteArray
byteBuffer.get(byteArray);
actualFrame = convertBytesToImage(byteArray, width, height);
// Writes the new Image to the ImageContainer. If an other part of the programm wants to do something like displaying or storing
//with the frames it can set up a changeListener to get a chance to do something with the newest frame.
imageContainer.setImage(actualFrame);
buffer.unmap();
}
sample.dispose();
return FlowReturn.OK;
}

private Image convertBytesToImage(byte[] pixels, int width, int height){
//Writes a bytearray to a WritableImage.
WritableImage img = new WritableImage(width, height);
PixelWriter pw = img.getPixelWriter();
pw.setPixels(0, 0, width, height, PixelFormat.getByteBgraInstance(), pixels, 0, width *4);
return img;
}

}
111 changes: 111 additions & 0 deletions GstRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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);
}
}


25 changes: 25 additions & 0 deletions ImageContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package application;


import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.scene.image.Image;


public class ImageContainer {
private ObjectProperty<Image> image = new SimpleObjectProperty<Image>();

public Image getImage() {
return image.get();
}

public void setImage(Image image) {
this.image.set(image);
}

public void addListener(ChangeListener<Image> listener){
image.addListener(listener);
}
}

0 comments on commit 4dd4abc

Please sign in to comment.