Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
Put OrbbecCam code in separate class, add depth frame example
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael List committed May 18, 2019
1 parent 5927f13 commit 264dc73
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 79 deletions.
112 changes: 33 additions & 79 deletions app/src/main/java/com/example/michi/myapplication/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package com.example.michi.myapplication;

import android.support.annotation.NonNull;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.orbbec.astra.Astra;
import com.orbbec.astra.PointFrame;
import com.orbbec.astra.PointStream;
import com.orbbec.astra.ReaderFrame;
import com.orbbec.astra.StreamReader;
import com.orbbec.astra.StreamSet;
import com.orbbec.astra.Vector3D;
import com.orbbec.astra.android.AstraAndroidContext;

import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
private Executor ex;
private OrbbecCamAndroid orbbecCamAndroid;
private Button getDataButton;

// Used to load the 'native-lib' library on application startup.
static {
Expand All @@ -37,15 +30,17 @@ protected void onCreate(Bundle savedInstanceState) {
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());

// Executor class
ex = new Executor(){
// Initialize OrbbecCam only if the app is not running in an emulator
if (!isEmulator()) {
orbbecCamAndroid = new OrbbecCamAndroid(getApplicationContext(), 640, 480);
}

getDataButton.setOnClickListener(new View.OnClickListener(){
@Override
public void execute(@NonNull Runnable r) {
new Thread (r).start();
public void onClick(View v){
getData();
}
};
// Execute the Runnable object
ex.execute(new UpdateRunnable());
});
}

/**
Expand All @@ -54,68 +49,27 @@ public void execute(@NonNull Runnable r) {
*/
public native String stringFromJNI();

/**
* "Main" Runnable code (to be run inside of Astra initialized Thread)
*/
private class UpdateRunnable implements Runnable {
boolean frameFinished = false;
ArrayList<Vector3D> vector3DList = new ArrayList<>();

@Override
public void run() {
// Astra.initialize
final AstraAndroidContext aac = new AstraAndroidContext(getApplicationContext());


try {
aac.initialize();
aac.openAllDevices();

Log.e("STREAM", "open stream");
StreamSet streamSet = StreamSet.open();
StreamReader reader = streamSet.createReader();
private void getData() {
new Thread(new Runnable() {
public void run() {
ArrayList<Vector3D> vector3DList = orbbecCamAndroid.get3DVectors();

reader.addFrameListener(new StreamReader.FrameListener() {

public void onFrameReady(StreamReader reader, ReaderFrame frame) {
PointFrame df = PointFrame.get(frame);
FloatBuffer buffer = df.getPointBuffer();
Log.e("FRAME", "height: " + df.getHeight());
Log.e("FRAME", "width: " + df.getWidth());

if (df.isValid()) {
Log.e("FRAME", "frame is valid");
while (buffer.hasRemaining()) {
vector3DList.add(new Vector3D(
buffer.get(),
buffer.get(),
buffer.get() * -1));
}
frameFinished = true;
}
}
});

PointStream pointStream = PointStream.get(reader);
pointStream.start();

while (!frameFinished) {
Astra.update();
TimeUnit.MILLISECONDS.sleep(100);
}

pointStream.stop();
streamSet.close();
} catch (Throwable e) {
Log.e("ASTRA", e.toString());
} finally {
aac.terminate();
Log.e("DATA", "x of 200th vector: " + vector3DList.get(200).getX());
Log.e("DATA", "y of 200th vector: " + vector3DList.get(200).getY());
Log.e("DATA", "z of 200th vector: " + vector3DList.get(200).getZ());
Log.e("DATA", "size of list: " + vector3DList.size());
}
}).start();
}

Log.e("DATA", "x: " + this.vector3DList.get(200).getX());
Log.e("DATA", "y: " + this.vector3DList.get(200).getY());
Log.e("DATA", "z: " + this.vector3DList.get(200).getZ());
Log.e("DATA", "size of list: " + this.vector3DList.size());
}
public static boolean isEmulator() {
return Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| "google_sdk".equals(Build.PRODUCT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package com.example.michi.myapplication;

import com.orbbec.astra.Astra;
import com.orbbec.astra.DepthFrame;
import com.orbbec.astra.DepthStream;
import com.orbbec.astra.ImageStreamMode;
import com.orbbec.astra.PointFrame;
import com.orbbec.astra.PointStream;
import com.orbbec.astra.ReaderFrame;
import com.orbbec.astra.StreamReader;
import com.orbbec.astra.StreamSet;
import com.orbbec.astra.Vector3D;
import com.orbbec.astra.android.AstraAndroidContext;

import android.content.Context;
import android.util.Log;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class OrbbecCamAndroid {
private static final int updateSleepMS = 100;
private static final String logTagFrame = "ASTRA_FRAME";
private static final String logTagPointStream = "ASTRA_POINT";
private static final String logTagDepthStream = "ASTRA_DEPTH";
private boolean first_call = true;
private Context applicationContext;
private AstraAndroidContext aac;
private int width;
private int height;

public OrbbecCamAndroid(Context applicationContext, int width, int height) {
this.applicationContext = applicationContext;
this.width = width;
this.height = height;

aac = new AstraAndroidContext(this.applicationContext);
aac.initialize();
aac.openAllDevices();
}

public ArrayList<Vector3D> get3DVectors() {
final boolean[] frameFinished = {false};
final ArrayList<Vector3D> vector3DList = new ArrayList<>();

// Call depth stream once to set resoultion. Can't set resolution via point stream.
if (first_call) {
getDepthData();
first_call = false;
}

try {
Log.d(logTagPointStream, "Trying to open stream");
StreamSet streamSet = StreamSet.open();
StreamReader reader = streamSet.createReader();

reader.addFrameListener(new StreamReader.FrameListener() {
public void onFrameReady(StreamReader reader, ReaderFrame frame) {
PointFrame pf = PointFrame.get(frame);
FloatBuffer buffer = pf.getPointBuffer();

if (pf.isValid()) {
Log.d(logTagFrame, "frame is valid");
Log.d(logTagFrame, "height: " + pf.getHeight());
Log.d(logTagFrame, "width: " + pf.getWidth());

while (buffer.hasRemaining()) {
vector3DList.add(new Vector3D(buffer.get(), buffer.get(), buffer.get() * -1));
}
frameFinished[0] = true;
}
}
});

PointStream pointStream = PointStream.get(reader);
pointStream.start();

while (!frameFinished[0]) {
Astra.update();
TimeUnit.MILLISECONDS.sleep(updateSleepMS);
}

pointStream.stop();
reader.destroy();
streamSet.close();
} catch (Throwable e) {
Log.e(logTagPointStream, e.toString());
}

Log.d(logTagPointStream, "size of list: " + vector3DList.size());

return vector3DList;
}

public ArrayList<Vector3D> getDepthData() {
final boolean[] frameFinished = {false};
final ArrayList<Vector3D> vector3DList = new ArrayList<>();

try {
Log.d(logTagDepthStream, "Trying to open stream");
StreamSet streamSet = StreamSet.open();
StreamReader reader = streamSet.createReader();

reader.addFrameListener(new StreamReader.FrameListener() {

public void onFrameReady(StreamReader reader, ReaderFrame frame) {
DepthFrame df = DepthFrame.get(frame);
ShortBuffer buffer = df.getDepthBuffer();

if (df.isValid()) {
Log.d(logTagFrame, "frame is valid");
Log.d(logTagFrame, "height: " + df.getHeight());
Log.d(logTagFrame, "width: " + df.getWidth());

while (buffer.hasRemaining()) {
//TODO Check if data looks like below data
vector3DList.add(new Vector3D(
buffer.get(),
buffer.get(),
buffer.get() * -1));
}
frameFinished[0] = true;
}
}
});

DepthStream depthStream = DepthStream.get(reader);
depthStream.setMode(new ImageStreamMode(0, width, height, 100, 30));
depthStream.start();

while (!frameFinished[0]) {
Astra.update();
TimeUnit.MILLISECONDS.sleep(updateSleepMS);
}

depthStream.stop();
reader.destroy();
streamSet.close();
} catch (Throwable e) {
Log.e(logTagDepthStream, e.toString());
}

Log.d(logTagDepthStream, "size of list: " + vector3DList.size());

return vector3DList;
}

public void closeCam() {
aac.terminate();
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/getDataButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get data"
tools:layout_editor_absoluteX="159dp"
tools:layout_editor_absoluteY="435dp" />

</android.support.constraint.ConstraintLayout>

0 comments on commit 264dc73

Please sign in to comment.