"We offer new support options and therefor the forums are now in read-only mode! Please check out our Support Center for more information." - Vuforia Engine Team

Hololens - GetCameraImage

Hi,

 

I try to get camera image in Editor using the configuration for Hololens (Optical see-Through) but the result is black screen or unknown image, i don't know what im doing wrong.

 

I'm using Unity 5.6.1p4 (Windows 10 - x64).

 

This is my code used for get camera image:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

using Vuforia;
using Image = Vuforia.Image;

public class VuforiaCameraImageAccess : MonoBehaviour
{
    public static VuforiaCameraImageAccess Instance;

    public Texture2D currentVideoTexture;
    public RawImage currentVideoRawImage;
    public bool currentVideoTextureReady;
    private float updateImageTimer = 0.0f;

    #region PRIVATE_MEMBERS

    public Image.PIXEL_FORMAT mPixelFormat = Image.PIXEL_FORMAT.UNKNOWN_FORMAT;
    public Vuforia.Image image;
    private byte[] pixels;

    private bool mAccessCameraImage = true;
    private bool mFormatRegistered = false;

    #endregion // PRIVATE_MEMBERS

    #region MONOBEHAVIOUR_METHODS

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {

#if UNITY_EDITOR
        mPixelFormat = Image.PIXEL_FORMAT.GRAYSCALE; // Need Grayscale for Editor
#else
        mPixelFormat = Image.PIXEL_FORMAT.RGB888; // Use RGB888 for mobile
#endif

        // Register Vuforia life-cycle callbacks:
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
        VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
        VuforiaARController.Instance.RegisterOnPauseCallback(OnPause);

    }

    void Update()
    {
        if (currentVideoTextureReady)
        {
            if (currentVideoTexture == null)
            {
                Debug.Log("Create currentVideoTexture");
                currentVideoTexture = new Texture2D(image.Width, image.Height);
            }
            else
            {
                updateImageTimer += Time.deltaTime;

                if (updateImageTimer >= 1.0f)
                {
                    Debug.Log("Update currentVideoTexture");
                    updateImageTimer = 0.0f;

                    //currentVideoTexture.LoadRawTextureData(pixels);
                    //currentVideoTexture.LoadImage(pixels);
                    image.CopyToTexture(currentVideoTexture);

                    currentVideoRawImage.texture = currentVideoTexture;
                }
            }
        }
    }

    #endregion // MONOBEHAVIOUR_METHODS

    #region PRIVATE_METHODS

    void OnVuforiaStarted()
    {

        // Try register camera image format
        if (CameraDevice.Instance.SetFrameFormat(mPixelFormat, true))
        {
            Debug.Log("Successfully registered pixel format " + mPixelFormat.ToString());

            mFormatRegistered = true;
        }
        else
        {
            Debug.LogError(
                "\nFailed to register pixel format: " + mPixelFormat.ToString() +
                "\nThe format may be unsupported by your device." +
                "\nConsider using a different pixel format.\n");

            mFormatRegistered = false;
        }

    }

    /// 
    /// Called each time the Vuforia state is updated
    /// 
    void OnTrackablesUpdated()
    {
        if (mFormatRegistered)
        {
            if (mAccessCameraImage)
            {
                image = CameraDevice.Instance.GetCameraImage(mPixelFormat);

                if (image != null)
                {
                    //Debug.Log(
                    //    "\nImage Format: " + image.PixelFormat +
                    //    "\nImage Size:   " + image.Width + "x" + image.Height +
                    //    "\nBuffer Size:  " + image.BufferWidth + "x" + image.BufferHeight +
                    //    "\nImage Stride: " + image.Stride + "\n"
                    //);

                    pixels = image.Pixels;

                    if (pixels != null && pixels.Length > 0)
                    {
                        if (image.IsValid())
                        {
                            currentVideoTextureReady = true;
                        }
                        //Debug.Log(
                        //    "\nImage pixels: " +
                        //    pixels[0] + ", " +
                        //    pixels[1] + ", " +
                        //    pixels[2] + ", ...\n"
                        //);
                    }
                }
            }
        }
    }

    /// 
    /// Called when app is paused / resumed
    /// 
    void OnPause(bool paused)
    {
        if (paused)
        {
            Debug.Log("App was paused");
            UnregisterFormat();
        }
        else
        {
            Debug.Log("App was resumed");
            RegisterFormat();
        }
    }

    /// 
    /// Register the camera pixel format
    /// 
    void RegisterFormat()
    {
        if (CameraDevice.Instance.SetFrameFormat(mPixelFormat, true))
        {
            Debug.Log("Successfully registered camera pixel format " + mPixelFormat.ToString());
            mFormatRegistered = true;
        }
        else
        {
            Debug.LogError("Failed to register camera pixel format " + mPixelFormat.ToString());
            mFormatRegistered = false;
        }
    }

    /// 
    /// Unregister the camera pixel format (e.g. call this when app is paused)
    /// 
    void UnregisterFormat()
    {
        Debug.Log("Unregistering camera pixel format " + mPixelFormat.ToString());
        CameraDevice.Instance.SetFrameFormat(mPixelFormat, false);
        mFormatRegistered = false;
    }

    #endregion //PRIVATE_METHODS
}


 

Regards.-