Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibilty update for procamcalib. #922

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 57 additions & 12 deletions src/main/java/org/bytedeco/javacv/RealSenseFrameGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public static void tryLoad() throws FrameGrabber.Exception {
private static context context = null;
private int deviceNumber = 0;
private device device = null;
private static device globalDevice = null;
private boolean depth = false; // default to "video"
private boolean colorEnabled = false;
private boolean depthEnabled = false;
Expand Down Expand Up @@ -208,7 +209,10 @@ public double getFrameRate() { // TODO: check this.
private boolean startedOnce = false;
private boolean behaveAsColorFrameGrabber = false;

public device loadDevice() throws FrameGrabber.Exception{
public device loadDevice() throws FrameGrabber.Exception {
if (context == null) {
context = new context();
}
if (context == null || context.get_device_count() <= deviceNumber) {
throw new Exception("FATAL error: Realsense camera: " + deviceNumber + " not connected/found");
}
Expand All @@ -218,13 +222,41 @@ public device loadDevice() throws FrameGrabber.Exception{

@Override
public void start() throws FrameGrabber.Exception {

// There is already a device, we reboot everything (for Procamcalib).
if (globalDevice != null) {
globalDevice.close();
context.close();
globalDevice = null;
context = null;
}

if (context == null) {
context = new context();
}
if (context == null || context.get_device_count() <= deviceNumber) {
throw new Exception("FATAL error: Realsense camera: " + deviceNumber + " not connected/found");
}

if (device == null) {
device = context.get_device(deviceNumber);
}
globalDevice = device;

// Choose the camera to enable by format.
if (format != null) {
switch (format) {
case "rgb":
this.enableColorStream();
break;
case "ir":
this.enableIRStream();
break;
case "depth":
this.enableDepthStream();
break;
}
}

if (colorEnabled) {
device.enable_stream(RealSense.color, imageWidth, imageHeight, RealSense.rgb8, (int) frameRate);
Expand All @@ -237,14 +269,10 @@ public void start() throws FrameGrabber.Exception {
}
// if no stream is select, just get the color.
if (!colorEnabled && !IREnabled && !depthEnabled) {

if (!startedOnce) {
enableColorStream();
behaveAsColorFrameGrabber = true;
this.setImageMode(ImageMode.GRAY);
}
enableColorStream();
device.enable_stream(RealSense.color, imageWidth, imageHeight, RealSense.rgb8, (int) frameRate);
behaveAsColorFrameGrabber = true;
}
startedOnce = true;
device.start();
}

Expand All @@ -255,9 +283,9 @@ public void start() throws FrameGrabber.Exception {
@Override
public void stop() throws FrameGrabber.Exception {
device.stop();
colorEnabled = false;
IREnabled = false;
depthEnabled = false;
// colorEnabled = false;
// IREnabled = false;
// depthEnabled = false;
frameNumber = 0;
}
private Pointer rawDepthImageData = new Pointer((Pointer) null),
Expand Down Expand Up @@ -295,7 +323,6 @@ public IplImage grabDepth() {
// ShortBuffer out = bb.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
// out.put(in);
// }

return rawDepthImage;
}

Expand Down Expand Up @@ -384,7 +411,25 @@ public Frame grab() throws Exception {
}
cvCvtColor(image, returnImage, CV_BGR2GRAY);
return converter.convert(returnImage);
} else {
if (IREnabled) {
return converter.convert(grabIR());
} else {
if (depthEnabled) {

// Fake colors
IplImage image = grabDepth();
if (returnImage == null) {
int deviceWidth = device.get_stream_width(RealSense.depth);
int deviceHeight = device.get_stream_height(RealSense.depth);
// returnImage = IplImage.create(deviceWidth, deviceHeight, IPL_DEPTH_8U, 3);
returnImage = IplImage.create(deviceWidth, deviceHeight, IPL_DEPTH_8U, 1);
}
return converter.convert(returnImage);
}
}
}

return null;
}

Expand Down