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
+ }
0 commit comments