Quantcast
Channel: Recent Discussions — Mixed Reality Developer Forum
Viewing all articles
Browse latest Browse all 10543

PhotoCapture throwing IMediaCapture error

$
0
0

Hi! I'm pretty new to HoloLens. I'm trying to do a photo capture on the HoloLens and interact with the raw bytes. However, I get this error message when I run the program = Failed to initialize IMediaCapture (hr = 0xC00DABE0) when I try to create the asynchronous call in the beginning - PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);

Would really appreciate any help or tips! Thank you so much!


using UnityEngine;
using System.Collections;
using UnityEngine.VR.WSA.WebCam;
using System.Linq;
using System.Collections.Generic;

public class PhotoStream : MonoBehaviour {

private PhotoCapture photoCaptureObject = null;


// Use this for initialization
void Start () {
    Debug.Log("Webcam Mode is: " + WebCamMode.PhotoMode);
    PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);


}

// Update is called once per frame
void Update()
{

}

void OnPhotoCaptureCreated(PhotoCapture captureObject)
{
    Debug.Log("begin on photo capture created");
    // store object
    photoCaptureObject = captureObject;

    Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();

    // set parameters
    CameraParameters c = new CameraParameters();
    c.hologramOpacity = 0.0f;
    c.cameraResolutionWidth = cameraResolution.width;
    c.cameraResolutionHeight = cameraResolution.height;
    c.pixelFormat = CapturePixelFormat.BGRA32;

    // start photo mode
    captureObject.StartPhotoModeAsync(c, false, OnPhotoModeStarted);
}

// clean up 
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
    photoCaptureObject.Dispose();
    photoCaptureObject = null;
}

private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
{
    if (result.success)
    {
        // capture a frame to memory
        photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
    }
    else
    {
        Debug.LogError("Unable to start photo mode!");
    }
}

void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
{
    if (result.success)
    {
        List<byte> imageBufferList = new List<byte>();
        // Copy the raw IMFMediaBuffer data into our empty byte list.
        photoCaptureFrame.CopyRawImageDataIntoBuffer(imageBufferList);

        // In this example, we captured the image using the BGRA32 format.
        // So our stride will be 4 since we have a byte for each rgba channel.
        // The raw image data will also be flipped so we access our pixel data
        // in the reverse order.
        int stride = 4;
        float denominator = 1.0f / 255.0f;
        List<Color> colorArray = new List<Color>();
        for (int i = imageBufferList.Count - 1; i >= 0; i -= stride)
        {
            float a = (int)(imageBufferList[i - 0]) * denominator;
            float r = (int)(imageBufferList[i - 1]) * denominator;
            float g = (int)(imageBufferList[i - 2]) * denominator;
            float b = (int)(imageBufferList[i - 3]) * denominator;

            colorArray.Add(new Color(r, g, b, a));
            Debug.Log("color: (" + r + ", " + g + ", " + b + ", " + a + ")");
        }
        // Now we could do something with the array such as texture.SetPixels() or run image processing on the list
    }
    photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}

}


Viewing all articles
Browse latest Browse all 10543

Trending Articles