开发原生的 Google 眼镜应用 【已翻译100%】(22)

    xiaoxiao2023-08-20  170

    使用传感器

    Glass没有键盘或触摸屏,但仍然具有移动设备所有的标准的传感器。你可以使用标准的传感器组件来访问这些传感器。

    定位和GPS

    Glass内置有GPS。TLocationSensor具有一个OnLocationChanged事件,这一事件在GPS组件被激活时产生,在有除Distance性质的以外的位置信息改变时也会产生这一事件。在OnLocationChanged事件的处理方法中,有一个 NewLocation参数,这一参数包含新位置的经纬度。

    运动和方向追踪

    要追踪Glass和穿戴者的运动和方向,需要使用TMotionSensor和TOrientationSensor组件。它们提供设备的加速度、角度状态、前进方向、速度和运动等信息。由于Glass被用户穿戴在头上,因此设备的这些运动信息实际上被翻译成用户如何张望、头部如何运动等信息。

    实际上信息是从很多不同的传感器中获取的,包括陀螺仪、磁力计、加速计。你可以在一段时间内使用一个TTimer来轮询传感器的运动和方向信息,也可以只读取某一个时刻的信息。

    Camera处理

    TCameraComponent 可以操作相机中的帧. 可以一次抓取多个帧作为预览,也可以一次一帧. Google在抓取图像时需要保持设备的打开状态,但不一定需要展示正在捕获的图像 (尽管展示当前图像更有利于使用者).

    调用 TCameraComponent, 设置 Active属性为true, 接着相应OnSampleBufferReadyevent. 记住这一过程不是在 UI 线程里, 所以要想UI 显示更新的图像,需要使用下述代码:

    // Event handler for the CameraComponent’s SampleBufferReady event procedure TMainForm.CameraComponent1SampleBufferReady( Sender: TObject; const ATime: Int64); Begin // Use Synchronize to move the execution to the main UI thread. TThread.Synchronize(TThread.CurrentThread, GetImage); end; procedure TMainForm.GetImage; begin // imgCameraView is a TImage component on the UI for displaying the image CameraComponent1.SampleBufferToBitmap(imgCameraView.Bitmap, True); end;

    使用触摸板与touchpad进行交互的最简单的方式是交互式手势。Pan 交互式手势提供了一系列事件当你的手指在触摸板上滑动时。举个例子,你可以使用Pan手势的水平移动来完成不同选项之间的切换。

    为响应交互式手势,只需简单的拓展表单的触摸属性,然后指定独特的你想接受事件的交互式手势。 之后,当用户进行交互式手势操作时,OnGesture事件会触发这里有一个手势处理器的例子,允许用户基于交互式手势的水平位置来设置屏幕的超时时间。

    procedure TMainForm.FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo; var Handled: Boolean); var x: Integer; begin // Handle the Pan Interactive Gesture if EventInfo.GestureID = igiPan then begin // Specific processing for the beginning of the gesture – save the start location if TInteractiveGestureFlag.gfBegin in EventInfo.Flags then begin fPanStart := EventInfo.Location; fStartVal := pbTimeOut.Value; end else // Specific processing at the end of the gesture – set the timeout if TInteractiveGestureFlag.gfEnd in Eventinfo.Flags then begin SetScreenOffTimeout(Trunc(pbTimeOut.Value * 1000)); end else // Update the display based on the current gesture position begin if not (TInteractiveGestureFlag.gfInertia in EventInfo.Flags) then begin x := Trunc((fPanStart.X - EventInfo.Location.X)/10) * 10; pbTimeOut.Value := fStartVal + x; end; DisplayTimeOut(Trunc(pbTimeOut.Value)); end; end; end;

    注意一点,如果用户在触摸板上滑下那么除了Pan手势之外后退按钮事件也会被触发。你可以 自己捕获并处理后退按钮事件。用下面的代码在你的表单中简单的添加OnKeyUp事件处理器:

    if Key = vkHardwareBack then begin // Do whatever you want to do here Key := 0; // Set Key = 0 if you want to prevent the default action end;

    如果你总是设置Key为0,那么用户将不能退出应用,因此适当的考虑考虑。

    结论

    RAD Studio 和Appmethod为开发原生Glassware提供了极大的便利. 加上支持 C++ 和 Object Pascal, 选择的余地大大增加r. RAD Studio还提供了 原生的示例应用演示了上述的种种特性. 文档也详细说明了 操作步骤 . 此外还提供了 使用其它多种感应器,手势动作等等的示例. 想直接进入 GDK 查看相关的细节也很方便. 而且GDK也在一直持续更新. 可以在我后续的Delphi.org的博文中看到 相关GDK的说明.

    最新回复(0)