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) {
StreamSet streamSet = StreamSet.open();
StreamReader reader = streamSet.createReader();
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) {
}
depthStream.stop();
reader.destroy();
}
}
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) {
}
@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();
astraAndroidContext.openAllDevices();
boolean loopFlag = true;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (loopFlag) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
...
astraAndroidContext.terminate();