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

Commit 264dc73

Browse files
author
Michael List
committed
Put OrbbecCam code in separate class, add depth frame example
1 parent 5927f13 commit 264dc73

File tree

3 files changed

+210
-79
lines changed

3 files changed

+210
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package com.example.michi.myapplication;
22

3-
import android.support.annotation.NonNull;
3+
import android.os.Build;
44
import android.support.v7.app.AppCompatActivity;
55
import android.os.Bundle;
66
import android.util.Log;
7+
import android.view.View;
8+
import android.widget.Button;
79
import android.widget.TextView;
810

9-
import com.orbbec.astra.Astra;
10-
import com.orbbec.astra.PointFrame;
11-
import com.orbbec.astra.PointStream;
12-
import com.orbbec.astra.ReaderFrame;
13-
import com.orbbec.astra.StreamReader;
14-
import com.orbbec.astra.StreamSet;
1511
import com.orbbec.astra.Vector3D;
16-
import com.orbbec.astra.android.AstraAndroidContext;
1712

18-
import java.nio.FloatBuffer;
1913
import java.util.ArrayList;
20-
import java.util.concurrent.Executor;
21-
import java.util.concurrent.TimeUnit;
2214

2315
public class MainActivity extends AppCompatActivity {
24-
private Executor ex;
16+
private OrbbecCamAndroid orbbecCamAndroid;
17+
private Button getDataButton;
2518

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

40-
// Executor class
41-
ex = new Executor(){
33+
// Initialize OrbbecCam only if the app is not running in an emulator
34+
if (!isEmulator()) {
35+
orbbecCamAndroid = new OrbbecCamAndroid(getApplicationContext(), 640, 480);
36+
}
37+
38+
getDataButton.setOnClickListener(new View.OnClickListener(){
4239
@Override
43-
public void execute(@NonNull Runnable r) {
44-
new Thread (r).start();
40+
public void onClick(View v){
41+
getData();
4542
}
46-
};
47-
// Execute the Runnable object
48-
ex.execute(new UpdateRunnable());
43+
});
4944
}
5045

5146
/**
@@ -54,68 +49,27 @@ public void execute(@NonNull Runnable r) {
5449
*/
5550
public native String stringFromJNI();
5651

57-
/**
58-
* "Main" Runnable code (to be run inside of Astra initialized Thread)
59-
*/
60-
private class UpdateRunnable implements Runnable {
61-
boolean frameFinished = false;
62-
ArrayList<Vector3D> vector3DList = new ArrayList<>();
63-
64-
@Override
65-
public void run() {
66-
// Astra.initialize
67-
final AstraAndroidContext aac = new AstraAndroidContext(getApplicationContext());
68-
69-
70-
try {
71-
aac.initialize();
72-
aac.openAllDevices();
73-
74-
Log.e("STREAM", "open stream");
75-
StreamSet streamSet = StreamSet.open();
76-
StreamReader reader = streamSet.createReader();
52+
private void getData() {
53+
new Thread(new Runnable() {
54+
public void run() {
55+
ArrayList<Vector3D> vector3DList = orbbecCamAndroid.get3DVectors();
7756

78-
reader.addFrameListener(new StreamReader.FrameListener() {
79-
80-
public void onFrameReady(StreamReader reader, ReaderFrame frame) {
81-
PointFrame df = PointFrame.get(frame);
82-
FloatBuffer buffer = df.getPointBuffer();
83-
Log.e("FRAME", "height: " + df.getHeight());
84-
Log.e("FRAME", "width: " + df.getWidth());
85-
86-
if (df.isValid()) {
87-
Log.e("FRAME", "frame is valid");
88-
while (buffer.hasRemaining()) {
89-
vector3DList.add(new Vector3D(
90-
buffer.get(),
91-
buffer.get(),
92-
buffer.get() * -1));
93-
}
94-
frameFinished = true;
95-
}
96-
}
97-
});
98-
99-
PointStream pointStream = PointStream.get(reader);
100-
pointStream.start();
101-
102-
while (!frameFinished) {
103-
Astra.update();
104-
TimeUnit.MILLISECONDS.sleep(100);
105-
}
106-
107-
pointStream.stop();
108-
streamSet.close();
109-
} catch (Throwable e) {
110-
Log.e("ASTRA", e.toString());
111-
} finally {
112-
aac.terminate();
57+
Log.e("DATA", "x of 200th vector: " + vector3DList.get(200).getX());
58+
Log.e("DATA", "y of 200th vector: " + vector3DList.get(200).getY());
59+
Log.e("DATA", "z of 200th vector: " + vector3DList.get(200).getZ());
60+
Log.e("DATA", "size of list: " + vector3DList.size());
11361
}
62+
}).start();
63+
}
11464

115-
Log.e("DATA", "x: " + this.vector3DList.get(200).getX());
116-
Log.e("DATA", "y: " + this.vector3DList.get(200).getY());
117-
Log.e("DATA", "z: " + this.vector3DList.get(200).getZ());
118-
Log.e("DATA", "size of list: " + this.vector3DList.size());
119-
}
65+
public static boolean isEmulator() {
66+
return Build.FINGERPRINT.startsWith("generic")
67+
|| Build.FINGERPRINT.startsWith("unknown")
68+
|| Build.MODEL.contains("google_sdk")
69+
|| Build.MODEL.contains("Emulator")
70+
|| Build.MODEL.contains("Android SDK built for x86")
71+
|| Build.MANUFACTURER.contains("Genymotion")
72+
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
73+
|| "google_sdk".equals(Build.PRODUCT);
12074
}
12175
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package com.example.michi.myapplication;
2+
3+
import com.orbbec.astra.Astra;
4+
import com.orbbec.astra.DepthFrame;
5+
import com.orbbec.astra.DepthStream;
6+
import com.orbbec.astra.ImageStreamMode;
7+
import com.orbbec.astra.PointFrame;
8+
import com.orbbec.astra.PointStream;
9+
import com.orbbec.astra.ReaderFrame;
10+
import com.orbbec.astra.StreamReader;
11+
import com.orbbec.astra.StreamSet;
12+
import com.orbbec.astra.Vector3D;
13+
import com.orbbec.astra.android.AstraAndroidContext;
14+
15+
import android.content.Context;
16+
import android.util.Log;
17+
18+
import java.nio.FloatBuffer;
19+
import java.nio.ShortBuffer;
20+
import java.util.ArrayList;
21+
import java.util.concurrent.TimeUnit;
22+
23+
public class OrbbecCamAndroid {
24+
private static final int updateSleepMS = 100;
25+
private static final String logTagFrame = "ASTRA_FRAME";
26+
private static final String logTagPointStream = "ASTRA_POINT";
27+
private static final String logTagDepthStream = "ASTRA_DEPTH";
28+
private boolean first_call = true;
29+
private Context applicationContext;
30+
private AstraAndroidContext aac;
31+
private int width;
32+
private int height;
33+
34+
public OrbbecCamAndroid(Context applicationContext, int width, int height) {
35+
this.applicationContext = applicationContext;
36+
this.width = width;
37+
this.height = height;
38+
39+
aac = new AstraAndroidContext(this.applicationContext);
40+
aac.initialize();
41+
aac.openAllDevices();
42+
}
43+
44+
public ArrayList<Vector3D> get3DVectors() {
45+
final boolean[] frameFinished = {false};
46+
final ArrayList<Vector3D> vector3DList = new ArrayList<>();
47+
48+
// Call depth stream once to set resoultion. Can't set resolution via point stream.
49+
if (first_call) {
50+
getDepthData();
51+
first_call = false;
52+
}
53+
54+
try {
55+
Log.d(logTagPointStream, "Trying to open stream");
56+
StreamSet streamSet = StreamSet.open();
57+
StreamReader reader = streamSet.createReader();
58+
59+
reader.addFrameListener(new StreamReader.FrameListener() {
60+
public void onFrameReady(StreamReader reader, ReaderFrame frame) {
61+
PointFrame pf = PointFrame.get(frame);
62+
FloatBuffer buffer = pf.getPointBuffer();
63+
64+
if (pf.isValid()) {
65+
Log.d(logTagFrame, "frame is valid");
66+
Log.d(logTagFrame, "height: " + pf.getHeight());
67+
Log.d(logTagFrame, "width: " + pf.getWidth());
68+
69+
while (buffer.hasRemaining()) {
70+
vector3DList.add(new Vector3D(buffer.get(), buffer.get(), buffer.get() * -1));
71+
}
72+
frameFinished[0] = true;
73+
}
74+
}
75+
});
76+
77+
PointStream pointStream = PointStream.get(reader);
78+
pointStream.start();
79+
80+
while (!frameFinished[0]) {
81+
Astra.update();
82+
TimeUnit.MILLISECONDS.sleep(updateSleepMS);
83+
}
84+
85+
pointStream.stop();
86+
reader.destroy();
87+
streamSet.close();
88+
} catch (Throwable e) {
89+
Log.e(logTagPointStream, e.toString());
90+
}
91+
92+
Log.d(logTagPointStream, "size of list: " + vector3DList.size());
93+
94+
return vector3DList;
95+
}
96+
97+
public ArrayList<Vector3D> getDepthData() {
98+
final boolean[] frameFinished = {false};
99+
final ArrayList<Vector3D> vector3DList = new ArrayList<>();
100+
101+
try {
102+
Log.d(logTagDepthStream, "Trying to open stream");
103+
StreamSet streamSet = StreamSet.open();
104+
StreamReader reader = streamSet.createReader();
105+
106+
reader.addFrameListener(new StreamReader.FrameListener() {
107+
108+
public void onFrameReady(StreamReader reader, ReaderFrame frame) {
109+
DepthFrame df = DepthFrame.get(frame);
110+
ShortBuffer buffer = df.getDepthBuffer();
111+
112+
if (df.isValid()) {
113+
Log.d(logTagFrame, "frame is valid");
114+
Log.d(logTagFrame, "height: " + df.getHeight());
115+
Log.d(logTagFrame, "width: " + df.getWidth());
116+
117+
while (buffer.hasRemaining()) {
118+
//TODO Check if data looks like below data
119+
vector3DList.add(new Vector3D(
120+
buffer.get(),
121+
buffer.get(),
122+
buffer.get() * -1));
123+
}
124+
frameFinished[0] = true;
125+
}
126+
}
127+
});
128+
129+
DepthStream depthStream = DepthStream.get(reader);
130+
depthStream.setMode(new ImageStreamMode(0, width, height, 100, 30));
131+
depthStream.start();
132+
133+
while (!frameFinished[0]) {
134+
Astra.update();
135+
TimeUnit.MILLISECONDS.sleep(updateSleepMS);
136+
}
137+
138+
depthStream.stop();
139+
reader.destroy();
140+
streamSet.close();
141+
} catch (Throwable e) {
142+
Log.e(logTagDepthStream, e.toString());
143+
}
144+
145+
Log.d(logTagDepthStream, "size of list: " + vector3DList.size());
146+
147+
return vector3DList;
148+
}
149+
150+
public void closeCam() {
151+
aac.terminate();
152+
}
153+
154+
public int getWidth() {
155+
return width;
156+
}
157+
158+
public void setWidth(int width) {
159+
this.width = width;
160+
}
161+
162+
public int getHeight() {
163+
return height;
164+
}
165+
166+
public void setHeight(int height) {
167+
this.height = height;
168+
}
169+
}

app/src/main/res/layout/activity_main.xml

+8
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@
1616
app:layout_constraintRight_toRightOf="parent"
1717
app:layout_constraintTop_toTopOf="parent" />
1818

19+
<Button
20+
android:id="@+id/getDataButton"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:text="Get data"
24+
tools:layout_editor_absoluteX="159dp"
25+
tools:layout_editor_absoluteY="435dp" />
26+
1927
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)