Astra SDK  v2.1.3
使用java接口

windows和linux平台

准备工作

引入开发包中的java/astra-wrapper.jar到你的工程.

现在开始吧!

以深度为例, 流程和之前的c++接口类似.

import com.orbbec.astra.*;
import java.nio.ShortBuffer;
public class Main {
public static int frameCount = 0;
public static void main(String[] args) {
Astra.initialize();//初始化 Astra SDK
StreamSet streamSet = StreamSet.open();//构造StreamSet
StreamReader reader = streamSet.createReader();//创建StreamReader
DepthStream depthStream = DepthStream.get(reader);//取得深度流
for (ImageMode mode: depthStream.getAvailableModes()) {
System.out.println("mode: " + mode.toString());
}
reader.addFrameListener(new StreamReader.FrameListener() {
@Override
public void onFrameReady(StreamReader reader, ReaderFrame frame) {
frameCount++;
DepthFrame depthFrame = DepthFrame.get(frame);
ShortBuffer depthData = depthFrame.getDepthBuffer();//拿到深度数据
int width = depthFrame.getWidth();
int height = depthFrame.getHeight();
short middle = depthData.get(width * (height / 2) + (width / 2));
System.out.println("Frame #" + depthFrame.getFrameIndex() + ", value:" + middle);
}
});//注册监听类
depthStream.start();//启动深度流
while (frameCount < 100) {
Astra.update();//更新循环
}
depthStream.stop();//关闭深度流
reader.destroy();//销毁StreamReader
Astra.terminate();//关闭 Astra SDK
}
}
Definition: ApiStatus.cs:4

android平台

准备工作

Android Studio的配置参考 Android Studio 环境搭建 .

现在开始吧!

安卓平台仅在初始化和关闭sdk时与java有所不同, 其他方面是一致的.

AstraAndroidContext astraAndroidContext = new AstraAndroidContext(this.getApplication(), new AstraDeviceManagerListener() {
@Override
public void onOpenAllDevicesCompleted(Iterable<UsbDevice> iterable) {
//在这里进行打开streamset, 创建流和注册监听.
}
@Override
public void onOpenDeviceCompleted(UsbDevice usbDevice, boolean b) {
}
@Override
public void onNoDevice() {
Toast.makeText(MainActivity.this, "device not found", Toast.LENGTH_LONG).show();
}
@Override
public void onPermissionDenied(UsbDevice usbDevice) {
Toast.makeText(MainActivity.this, "PermissionDenied", Toast.LENGTH_LONG).show();
}
});
astraAndroidContext.initialize();//初始化 Astra SDK
astraAndroidContext.openAllDevices();//请求usb设备权限
//在另一个线程调用 Astra.update.最好使用互斥锁保证不同时调用sdk的api.
boolean loopFlag = true;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (loopFlag) {
Astra.update();
try {
Thread.sleep(20);//可以降低cpu开销但不是必须的
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
...
//activity结束时关闭 Astra SDK
astraAndroidContext.terminate();