I'm using my code below to poll the ML2 RGB camera in a project under Unity Ed. 2022.2.19f1, Win10, MLSDK v1.4.0-dev2 ML Unity v1.11.0.
I'm able to a) get capture permissions, b) enable and c) connect to the camera, but am failing to set up the still image stream to prepare capture here:
// Initialize CaptureConfig
MLCamera.CaptureConfig captureConfig = new MLCamera.CaptureConfig();
captureConfig.CaptureFrameRate = MLCamera.CaptureFrameRate._15FPS;captureConfig.StreamConfigs = new MLCamera.CaptureStreamConfig[1];
captureConfig.StreamConfigs[0] = new MLCamera.CaptureStreamConfig
{
CaptureType = MLCamera.CaptureType.Image,
OutputFormat = MLCamera.OutputFormat.JPEG
};// Prepare for capture
MLResult result = captureCamera.PrepareCapture(captureConfig, out MLCamera.Metadata _);
Logcat shows:
Error ml_camera_client validateAndSetStreamConfig Stream Config Invalid Image
ml_camera_client prepareCapture device state = Idle numStreamConfigs = 0
ml_camera_client Stream 0 type = resolution = state = Invalid
ml_camera_client Stream 1 type = resiolution = state = Invalid
I'm not sure how to proceed. A couple questions:
- In the case of still image capture, I believe we still proceed with 'stream' setup -- is that correct?
- I have not found the supported methods to feed or detect image size, et cetera, in a 'stream' context. I'm using the Unity camera examples as a basis.
Thanks for any reference or corrections. My full code follows:
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using System.Collections;
using UnityEngine.InputSystem;
using System;
using static UnityEngine.XR.MagicLeap.MLCameraBase;
using System.Collections.Generic;public class SimpleCameraCapture : MonoBehaviour
{
[SerializeField] private int width = 1280;
[SerializeField] private int height = 720;
[SerializeField] private MLCamera.CaptureFrameRate frameRate = MLCamera.CaptureFrameRate._15FPS;private MLCamera captureCamera; private MLCamera.CaptureStreamConfig captureStreamConfig; private bool cameraDeviceAvailable = false; private void Start() { MLPermissions.Callbacks permissionCallbacks = new MLPermissions.Callbacks(); permissionCallbacks.OnPermissionGranted += OnPermissionGranted; permissionCallbacks.OnPermissionDenied += OnPermissionDenied; permissionCallbacks.OnPermissionDeniedAndDontAskAgain += OnPermissionDeniedAndDontAskAgain; MLPermissions.RequestPermission(MLPermission.Camera, permissionCallbacks); } private void OnPermissionGranted(string permission) { StartCoroutine(EnableMLCamera()); } private void OnPermissionDenied(string permission) { } private void OnPermissionDeniedAndDontAskAgain(string permission) { } private IEnumerator EnableMLCamera() { while (!cameraDeviceAvailable) { MLResult result = MLCamera.GetDeviceAvailabilityStatus(MLCamera.Identifier.Main, out cameraDeviceAvailable); if (!(result.IsOk && cameraDeviceAvailable)) { yield return new WaitForSeconds(1.0f); } } ConnectCamera(); } private void ConnectCamera() { MLCamera.ConnectContext context = MLCamera.ConnectContext.Create(); context.Flags = MLCamera.ConnectFlag.CamOnly; context.EnableVideoStabilization = false; captureCamera = MLCamera.CreateAndConnect(context); if (captureCamera != null) { captureStreamConfig = new MLCamera.CaptureStreamConfig { CaptureType = MLCamera.CaptureType.Image, OutputFormat = MLCamera.OutputFormat.JPEG }; captureCamera.OnRawImageAvailable += OnCaptureRawImageComplete; } } private void OnCaptureRawImageComplete(MLCamera.CameraOutput output, MLCamera.ResultExtras resultExtras, MLCamera.Metadata metadataHandle) { } void Update() { if (Keyboard.current.spaceKey.wasPressedThisFrame && captureCamera != null) { MLCamera.CaptureConfig captureConfig = new MLCamera.CaptureConfig(); captureConfig.CaptureFrameRate = MLCamera.CaptureFrameRate._15FPS; captureConfig.StreamConfigs = new MLCamera.CaptureStreamConfig[1]; captureConfig.StreamConfigs[0] = new MLCamera.CaptureStreamConfig { CaptureType = MLCamera.CaptureType.Image, OutputFormat = MLCamera.OutputFormat.JPEG }; MLResult result = captureCamera.PrepareCapture(captureConfig, out MLCamera.Metadata _); if (!result.IsOk) { return; } captureCamera.PreCaptureAEAWB(); result = captureCamera.CaptureImage(1); if (!result.IsOk) { return; } } } void OnDestroy() { if (captureCamera != null) { captureCamera.OnRawImageAvailable -= OnCaptureRawImageComplete; captureCamera.Disconnect(); } }
}