Beispiel #1
0
    /// <summary>
    /// TakePhotoAsync  callback. Here we can verify the completed photo, and we are stopping the photo mode.
    /// </summary>
    /// <param name="result"></param>
    /// <param name="photoCaptureFrame"></param>
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        Debug.Log("8. TakePhotoAsync completed");

        if (!VerifyPhotoResult(result))
        {
            Debug.LogError("VerifyPhotoResult is incorrect");
            return;
        }
        Debug.Log("9. Photo result verified");

        try
        {
            // Copy the raw image data into the target texture
            photoCaptureFrame.UploadImageDataToTexture(_targetTexture);
            Debug.Log("10. Uploaded ImageDataToTexture");


            byte[] imagePngBytes = _targetTexture.EncodeToPNG();
            Debug.Log("11. targetTexture.EncodeToPNG");

            // here you can write the imagePngBytes into a file if you want

            Debug.Log("14. Start StopPhotoModeAsync");
            // Deactivate the camera
            _photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
        }
        catch (Exception e)
        {
            Debug.LogError($"EXCEPTION: OnCapturedPhotoToMemory, {e.Message}");
        }
    }
 void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
 {
     //Debug.Log("OnCapturedPhotoToMemory: start");
     if (result.success)
     {
         //Debug.Log("OnCapturedPhotoToMemory: success");
         // Create our Texture2D for use and set the correct resolution
         Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
         //Debug.Log("OnCapturedPhotoToMemory: create texture");
         Texture2D targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
         // Copy the raw image data into our target texture
         //Debug.Log("OnCapturedPhotoToMemory: upload image to texture");
         photoCaptureFrame.UploadImageDataToTexture(targetTexture);
         // Do as we wish with the texture such as apply it to a material, etc.
         Debug.Log("OnCapturedPhotoToMemory: put it to the material (adjusted): " + cameraResolution.width + " " + cameraResolution.height);
         Transform photo = transform.Find("PhotoDisplay");
         Debug.Log("OnCapturedPhotoToMemory: old size: " + photo.localScale.x + " " + photo.localScale.y + " " + photo.localScale.z);
         Debug.Log("OnCapturedPhotoToMemory: old pos: " + photo.localPosition.x + " " + photo.localPosition.y + " " + photo.localPosition.z);
         photo.localScale    = new Vector3((cameraResolution.width / 100), (cameraResolution.height / 100), photo.localScale.z);
         photo.localPosition = new Vector3(cameraResolution.width / 200, cameraResolution.height / 200, photo.localPosition.z);
         Debug.Log("OnCapturedPhotoToMemory: new size: " + photo.localScale.x + " " + photo.localScale.y + " " + photo.localScale.z);
         Debug.Log("OnCapturedPhotoToMemory: new pos: " + photo.localPosition.x + " " + photo.localPosition.y + " " + photo.localPosition.z);
         photo.gameObject.GetComponent <Renderer>().material.mainTexture = targetTexture;
         photo.transform.Rotate(new Vector3(0, 0, 180));
     }
     // Clean up
     Debug.Log("OnCapturedPhotoToMemory: clean up");
     photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
 }
Beispiel #3
0
 void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
 {
     this.debug.PrintPlus("in oncapturephototomemory");
     if (result.success)
     {
         // Create our Texture2D for use and set the correct resolution
         Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
         //create new texture object
         Texture2D targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
         // Copy the raw image data into our target texture
         photoCaptureFrame.UploadImageDataToTexture(targetTexture);
         //stop the photo mode
         //photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
         //set the new texture as the sourceimage of the image processor object
         this.debug.PrintPlus("setting imageprocessor texture");
         this._imageProcessor._sourceImage = targetTexture;
         //this.capturedImage = this.targetTexture;
         //this.debug.Print("capturedphoto");
     }
     else
     {
         //an error occurred, so we need to reset processing
         this.debug.PrintPlus("could not capture photo...resetting");
         this.ResetProcessing();
     }
     // finally implement clean up operations
     photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
 }
Beispiel #4
0
    void OnCapturedPhotoToMemory(PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        Debug.Log("2. TakePhotoAsync completed");

        if (!result.success)
        {
            Debug.LogError("VerifyPhotoResult is incorrect");
            return;
        }
        Debug.Log("3. Photo result verified");
        try
        {
            Debug.Log("4. Uploaded ImageDataToTexture");
            photoCaptureFrame.UploadImageDataToTexture(_targetTexture);

            Debug.Log("5. Set Pixels to UI Texture");
            toShow.SetPixels(_targetTexture.GetPixels());
            toShow.Apply();

            Debug.Log("6. Sending texture to UI");
            PhotoResultMethodHandler(toShow);
            _isPhotoBeingProcessed = false;
        }
        catch (Exception e)
        {
            Debug.LogError($"EXCEPTION: OnCapturedPhotoToMemory, {e.Message}");
        }
    }
Beispiel #5
0
        void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
        {
            if (result.success)
            {
                // Create our Texture2D for use and set the correct resolution
                Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
                Texture2D  targetTexture    = new Texture2D(cameraResolution.width, cameraResolution.height);
                // Copy the raw image data into our target texture
                photoCaptureFrame.UploadImageDataToTexture(targetTexture);

                Result = Instantiate(Result, Camera.main.transform.position, Camera.main.transform.rotation);
                Result.GetComponent <Renderer>().material.mainTexture = targetTexture;

                // Do as we wish with the texture such as apply it to a material, etc.

                //Result.GetComponent<Renderer>().material.mainTexture = targetTexture;

                this.result = ToBase64DataUrl(targetTexture);
            }



            // Clean up
            photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
        }
Beispiel #6
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            Debug.Log("OnCapturedPhotoToMemory = " + result.success);
            List <byte> buffer = new List <byte>();
            // Create our Texture2D for use and set the correct resolution
            Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width / 8 * res.height / 8).First();
            Texture2D  targetTexture    = new Texture2D(cameraResolution.width, cameraResolution.height);
            // Copy the raw image data into our target texture
            photoCaptureFrame.UploadImageDataToTexture(targetTexture);
            Renderer renderer = GameObject.FindGameObjectWithTag("DisplayCube").GetComponent <Renderer>();
            renderer.material.mainTexture = targetTexture;
            Debug.Log("Photo Uploaded to Texture");

            Matrix4x4 cameraToWorldMatrix;

            photoCaptureFrame.CopyRawImageDataIntoBuffer(buffer);
            Debug.Log("Raw Image copied into buffer");
            //Check if we can receive the position where the photo was taken
            if (!photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix))
            {
                return;
            }
            Debug.Log("past if");

            //Start a coroutine to handle the server request
            StartCoroutine(UploadAndHandlePhoto(buffer.ToArray(), cameraToWorldMatrix));

            Debug.Log("Photo saved to texture");
        }
        // Clean up
        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
Beispiel #7
0
    private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            //if (!Manager.is_ACI)
            //{
            //	photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
            //}
            // Copy the raw image data into our target texture
            photoCaptureFrame.UploadImageDataToTexture(targetTexture);
            targetTexture.wrapMode = TextureWrapMode.Clamp;

            // Take another photo
            // photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);

            // Write to file
            //Debug.Log("take ohoto");
            imageBufferBytesArray = targetTexture.EncodeToJPG(JPG_QUALITY);

            // Thread
            //CreateWebRequests();

            StartCoroutine("PostPhoto");
            StartCoroutine("GetFaces");
        }
    }
Beispiel #8
0
    private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            // Create our Texture2D for use and set the correct resolution
            Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
            Texture2D  targetTexture    = new Texture2D(cameraResolution.width, cameraResolution.height);

            // Copy the raw image data into our target texture
            photoCaptureFrame.UploadImageDataToTexture(targetTexture);

            // encode as JPEG to send to cognitiva service api's
            var imageBytes = targetTexture.EncodeToJPG();

            // Get information for the image from cognitive services
            GetTagsAndFaces(imageBytes);
            ReadWords(imageBytes);
        }
        else
        {       // show error
            DiagnosticsPanelTyper.TypeText("DIAGNOSTIC\n**************\n\nFailed take picture.\nError: " + result.hResult);
            InfoPanelTyper.TypeText("ABORT");
        }
        // stop handling the picture
        _photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
Beispiel #9
0
        /// <summary>
        /// Called when a photo has been captured to memory, if successfull,
        /// it'll copy the photo to the target texture
        /// </summary>
        /// <param name="result">Result of the photo process</param>
        /// <param name="photoCaptureFrame">Contains the photo information</param>
        private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
        {
            if (result.success)
            {
                if (targetTexture != null)
                {
                    Destroy(targetTexture);
                }

                targetTexture = new Texture2D(HorizontalCameraResolution, VerticalCameraResolution, TextureFormat.RGB24, false);
                // Copy the raw image data into our target texture
                photoCaptureFrame.UploadImageDataToTexture(targetTexture);

                if (OnFrameCapture != null)
                {
                    OnFrameCapture(targetTexture.GetRawTextureData().ToList(), photoWidth, photoHeight);
                }
            }
            else
            {
                Debug.LogError("Failed to capturing image");
            }

            photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
        }
Beispiel #10
0
 /*
  * private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
  * {
  *  if (result.success)
  *  {
  *      string filename = string.Format(@"CapturedImage{0}_n.jpg", Time.time);
  *      string filePath = System.IO.Path.Combine(Application.persistentDataPath, filename);
  *
  *
  *
  *      photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
  *      gui.text = "Picture saved to:" + filePath;
  *  }
  *  else
  *  {
  *      Debug.LogError("Unable to start photo mode!");
  *      gui.text = "Unable to start photo mode!";
  *  }
  * }*/
 /*
  * void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result)
  * {
  *  if (result.success)
  *  {
  *      Debug.Log("Saved Photo to disk!");
  *      photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
  *  }
  *  else
  *  {
  *      Debug.Log("Failed to save Photo to disk");
  *      gui.text = "Failed to save Photo to disk";
  *  }
  * }
  */
 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));
          * }*/
         gui.text = "Do Something";
         // Now we could do something with the array such as texture.SetPixels() or run image processing on the list
         photoCaptureFrame.UploadImageDataToTexture(ImageTexture);
         mat.mainTexture = ImageTexture;
     }
     photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
 }
Beispiel #11
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            Debug.Log("Photo Captured");
            // Create our Texture2D for use and set the correct resolution
            // GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
            Renderer   quadRenderer     = quad.GetComponent <Renderer>() as Renderer;
            Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
            Texture2D  targetTexture    = new Texture2D(cameraResolution.width, cameraResolution.height);

            Debug.Log(cameraResolution.width + " " + cameraResolution.height);

            // Copy the raw image data into our target texture
            photoCaptureFrame.UploadImageDataToTexture(targetTexture);

            Texture2D newImage = CropImage(targetTexture, cameraResolution.width / 2, cameraResolution.height / 2);

            quadRenderer.material = new Material(Shader.Find("Unlit/Texture"));

            //quad.transform.parent = this.transform;
            //  quad.transform.localPosition = new Vector3(0.0f, 0.0f, 3.0f);

            quadRenderer.material.SetTexture("_MainTex", newImage);
            text.SetActive(false);
        }
        // Clean up
        Debug.Log("Cleaning it up");
        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
Beispiel #12
0
    private static void OnCapturePhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (!result.success)
        {
            return;
        }

        photoCaptureFrame.UploadImageDataToTexture(capturedTexture);

        Message message = new Message()
        {
            hmdPosition  = GetHMDPosition(),
            hmdRotation  = GetHMDRotation(),
            imageRawData = capturedTexture.EncodeToJPG(100)
        };

        if (Client.Instance != null && Client.Instance.IsServerRunning())
        {
            Client.Instance.SendToServer(message);
        }

        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);

        Capturing = false;
    }
Beispiel #13
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        //https://answers.unity.com/questions/42843/referencing-non-static-variables-from-another-scri.html

        //Notify other functions that we are done with the camera
        cameraBusy = false;

        //Convert the raw image capture into a texture (required by unity for some reason)
        photoCaptureFrame.UploadImageDataToTexture(targetTexture);

#if IMAGECAPTURE_DEBUG
        Debug.Log("ImageCapture.OnCapturedPhotoToMemory: Uploaded to texture");
#endif

        //Convert into jpeg data for sending over the network
        byte[] jpegData = ImageConversion.EncodeToJPG(targetTexture, 1);

#if IMAGECAPTURE_DEBUG
        Debug.Log("ImageCapture.OnCapturedPhotoToMemory: jpeg data is " + System.Convert.ToBase64String(jpegData));
#endif

        //Send the captured image as a Texture2D over to the TCPImageSend script for processing
        CreateRequest(jpegData);
        //PostJpeg(jpegData);

#if IMAGECAPTURE_DEBUG
        Debug.Log("ImageCapture.OnCapturedPhotoToMemory: Called CreateRequest");
#endif
    }
Beispiel #14
0
    /// <summary>
    /// On image capture
    /// </summary>
    /// <param name="result"></param>
    /// <param name="photoCaptureFrame"></param>
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        // Copy the raw image data into the target texture
        photoCaptureFrame.UploadImageDataToTexture(targetTexture);

        previewImage.texture = targetTexture;

        try
        {
            byte[] imageData = targetTexture.EncodeToJPG(90);
            //WriteImageToDisk(imageData);

            Matrix4x4 cameraToWorldMatrix;
            Matrix4x4 projectionMatrix;

            photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix);
            photoCaptureFrame.TryGetProjectionMatrix(0, 5, out projectionMatrix);

            StartCoroutine(SendImageToServer(imageData, cameraToWorldMatrix, projectionMatrix));
        }
        catch (Exception e)
        {
            DebugManager.Instance.PrintToInfoLog("Error in OnCapturedPhotoToMemory:" + e.ToString());
        }
    }
        /// <summary>
        /// Called when a photo has been captured to memory, if successfull,
        /// it'll copy the photo to the target texture
        /// </summary>
        /// <param name="result">Result of the photo process</param>
        /// <param name="photoCaptureFrame">Contains the photo information</param>
        //private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result)
        private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
        {
            //Debug.Log("################### CameraCaptureHololens -> OnCapturedPhotoToMemory() -> start function!");
            if (result.success)
            {
                //Debug.Log("################### CameraCaptureHololens -> OnCapturedPhotoToMemory() -> success");
                if (targetTexture != null)
                {
                    Destroy(targetTexture);
                }

                targetTexture = new Texture2D(HorizontalCameraResolution, VerticalCameraResolution, TextureFormat.RGB24, false);
                // Copy the raw image data into our target texture
                photoCaptureFrame.UploadImageDataToTexture(targetTexture);

                if (OnFrameCapture != null)
                {
                    // delegated on MarkerDetectionHololens.ProcessImage()
                    OnFrameCapture(targetTexture.GetRawTextureData().ToList(), photoWidth, photoHeight);
                }
            }
            else
            {
                Debug.LogError("Failed to capturing image");
            }

            photoCaptureFrame.Dispose();

            photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);

            //Debug.Log("################### CameraCaptureHololens -> OnCapturedPhotoToMemory() -> end function!");
        }
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            List <byte> imageBufferList = new List <byte>();

            Debug.Log("OnCapturedPhotoToMemory Copy Started");

            photoCaptureFrame.CopyRawImageDataIntoBuffer(imageBufferList);

            Debug.Log("OnCapturedPhotoToMemory " + imageBufferList.Count);

            Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
            Texture2D  texture          = new Texture2D(cameraResolution.width, cameraResolution.height);
            photoCaptureFrame.UploadImageDataToTexture(texture);

            UIManager.Instance.LoadPhotoToTexture(texture);
        }
        else
        {
            Debug.Log("Failed to save Photo to memory");
        }

        photoCapture.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
Beispiel #17
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        // Copy the raw image data into the target texture
        photoCaptureFrame.UploadImageDataToTexture(targetTexture);

        // Create a GameObject to which the texture can be applied
        Renderer quadRenderer = gameObject.GetComponent <Renderer>() as Renderer;

        gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 15f);

        quadRenderer.material.SetTexture("_MainTex", targetTexture);

//        int width = Screen.width;
//        int height = Screen.height;
//
//        targetTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
//
//        targetTexture.Apply();
//
//        byte[] bytes = targetTexture.EncodeToPNG();

//        StartCoroutine(upload(bytes));

        // Deactivate the camera
        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
Beispiel #18
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        // Copy the raw image data into the target texture
        photoCaptureFrame.UploadImageDataToTexture(targetTexture);

        // Create a GameObject to which the texture can be applied
        Renderer quadRenderer = gameObject.GetComponent <Renderer>() as Renderer;
        int      width        = Screen.width;
        int      height       = Screen.height;

        targetTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);

        targetTexture.Apply();

        byte[] bytes = targetTexture.EncodeToPNG();
        Pic    myPic = new Pic();

        myPic.image = Convert.ToBase64String(bytes);
        string json = JsonUtility.ToJson(myPic);

        StartCoroutine(PostRequest(url, json));

        quadRenderer.material.SetTexture("_MainTex", targetTexture);

        // Deactivate the camera
        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
    /*
     * private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
     * {
     * if(result.success)
     * {
     *   string filename = string.Format(@"CapturedImage{0}_n.jpg", Time.time);
     *   string filepath = System.IO.Path.Combine(Application.persistentDataPath, filename);
     *   photoCaptureObject.TakePhotoAsync(filepath, PhotoCaptureFileOutputFormat.JPG,OnCapturedPhotoToDisk);
     *   Debug.Log("TakePhoto Succeed!"+filepath);
     * }
     * else
     * {
     *   Debug.LogError("Unable to start photo mode!");
     * }
     *
     * }
     *
     * void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result)
     * {
     * if(result.success)
     * {
     *   Debug.Log("Saved Photo to Disk!");
     *   photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
     * }
     * else
     * {
     *   Debug.Log("Failed to save photo to disk!");
     * }
     * }
     */



    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix);
        worldToCameraMatrix = cameraToWorldMatrix.inverse;
        photoCaptureFrame.TryGetProjectionMatrix(out projectionMatrix);

        photoCaptureFrame.UploadImageDataToTexture(targetTexture);

        GameObject quad         = GameObject.CreatePrimitive(PrimitiveType.Quad);
        Renderer   quadRenderer = quad.GetComponent <Renderer>() as Renderer;

        quadRenderer.material = new Material(Shader.Find("Unlit/Texture"));


        Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1));
        Vector3    position = cameraToWorldMatrix.MultiplyPoint(Vector3.zero);

        Debug.Log("cameraToWorldMatrix: " + cameraToWorldMatrix);
        Debug.Log("Camera Position in World: " + position);

        quad.transform.parent = this.transform;
        //转化为面向用户这一步在Unity Editor出错,即无法设定为相机朝向的反向,在HoloLens上有待尝试
        //quad.transform.position = position;
        //quad.transform.rotation = rotation;
        quad.transform.localPosition = new Vector3(0.0f, 0.0f, 0.1f);
        quad.transform.rotation      = this.transform.rotation;

        quadRenderer.material.SetTexture("_MainTex", targetTexture);

        photoCaptureObj.StopPhotoModeAsync(OnStoppedPhotoMode);

        Debug.Log("Capture Photo to Memory Succeed!");
    }
 void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
 {
     //Debug.Log("copying to memory");
     photoCaptureFrame.UploadImageDataToTexture(targetTexture);
     photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
     mc.GetPhoto(targetTexture);
     //Debug.Log("done");
 }
    /// <summary>
    /// Called when a photo was taken
    /// </summary>
    /// <param name="result"></param>
    /// <param name="photoCaptureFrame"></param>
    private void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        // copy to texture
        photoCaptureFrame.UploadImageDataToTexture(CurrentPhoto);

        // decativate camera
        capturer.StopPhotoModeAsync(OnPhotoModeStoppped);
    }
Beispiel #22
0
    private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        // Copy the raw image data into the target texture
        photoCaptureFrame.UploadImageDataToTexture(targetTexture);

        // Deactivate the camera
        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
Beispiel #23
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            // Create our Texture2D for use and set the correct resolution
            //Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
            Texture2D targetTexture = new Texture2D(textureWidth, textureHeight);//cameraResolution.width, cameraResolution.height);
            // Copy the raw image data into our target texture
            photoCaptureFrame.UploadImageDataToTexture(targetTexture);

            // Get view and projection matrices from camera, as well as position and lookVector
            Matrix4x4 inverseV;
            Matrix4x4 P;
            Matrix4x4 VP;
            bool      vSuccess         = photoCaptureFrame.TryGetCameraToWorldMatrix(out inverseV);
            bool      pSuccess         = photoCaptureFrame.TryGetProjectionMatrix(out P);
            Vector3   cameraWorldPos   = inverseV.MultiplyPoint(Vector3.zero);
            Vector3   cameraLookVector = inverseV.MultiplyVector(Vector3.forward);
            Debug.Log("RGB Camera View Matrix: " + (vSuccess ? "Found" : "NULL"));
            Debug.Log("RGB Camera Projection Matrix: " + (pSuccess ? "Found" : "NULL"));
            Debug.Log("RGB Camera Position: " + cameraWorldPos);
            Debug.Log("RGB Camera LookVector: " + cameraLookVector);

            //depthCamera.transform.position = (cameraWorldPos - Camera.main.gameObject.transform.position);
            //depthCamera.transform.LookAt(cameraWorldPos + cameraLookVector);
            //Debug.Log("Applied position and lookvector to depth camera");
            //camera.projectionMatrix = P;
            //Debug.Log("Applied projection matrix to depth camera");
            ////camera.worldToCameraMatrix = inverseV.inverse;
            //camera.Render();
            ////camera.RenderWithShader(Shader.Find("DepthOnly"), "depth");
            //Debug.Log("Rendered without depth shader");

            TakeScreenshot(targetTexture);
            Debug.Log("Snapshot Taken");
            // Update snapshot VP matrix before updating the shader
            if (vSuccess && pSuccess)
            {
                VP = P * inverseV.inverse;
                //Snapshot snapshot = snapshots[snapshots.Count - 1];
                //snapshot.vp = VP;
                //snapshot.position = new Vector4(cameraWorldPos.x, cameraWorldPos.y, cameraWorldPos.z, 1);
                //snapshots[snapshots.Count - 1] = snapshot;
                vpArray[numProjectors]    = VP;
                invVPArray[numProjectors] = (inverseV);
                posArray[numProjectors]   = new Vector4(cameraWorldPos.x, cameraWorldPos.y, cameraWorldPos.z, 1);
            }
            Debug.Log("Snapshot Updated");
            UpdateShader();
            Debug.Log("Updated Shader");

            // Free memory
            photoCaptureFrame.Dispose();
        }
        // Clean up
        //photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }
    void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (m_Canvas == null)
        {
            m_Canvas         = GameObject.CreatePrimitive(PrimitiveType.Quad);
            m_Canvas.name    = "PhotoCaptureCanvas";
            m_CanvasRenderer = m_Canvas.GetComponent <Renderer>() as Renderer;
            // m_CanvasRenderer.material = new Material(Shader.Find("AR/HolographicImageBlend"));

            //        m_CanvasRenderer.material = new Material(Shader.Find("HolographicImageBlend"));
            //Material m = new Material(Shader.Find("HolographicImageBlend"));

            //            m_CanvasRenderer.material = new Material(Shader.Find("Unlit/HolographicImageBlend"));

            Material m = new Material(Shader.Find("Unlit/HolographicImageBlend"));

            if (m != null)
            {
                m_CanvasRenderer.material = m;
            }
            else
            {
                Debug.Log("Shader Material PROBLEM!!!");
            }
        }

        Matrix4x4 cameraToWorldMatrix;

        photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix);
        Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse;

        Matrix4x4 projectionMatrix;

        photoCaptureFrame.TryGetProjectionMatrix(out projectionMatrix);

        photoCaptureFrame.UploadImageDataToTexture(m_Texture);
        m_Texture.wrapMode = TextureWrapMode.Clamp;

        m_CanvasRenderer.sharedMaterial.SetTexture("_MainTex", m_Texture);
        m_CanvasRenderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix);
        m_CanvasRenderer.sharedMaterial.SetMatrix("_CameraProjectionMatrix", projectionMatrix);
        m_CanvasRenderer.sharedMaterial.SetFloat("_VignetteScale", 1.0f);

        // Position the canvas object slightly in front
        // of the real world web camera.
        Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2);

        // Rotate the canvas object so that it faces the user.
        Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1));

        m_Canvas.transform.position = position;
        m_Canvas.transform.rotation = rotation;

        Debug.Log("Took picture!");
        m_CapturingPhoto = false;
    }
Beispiel #25
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        _photoCapture.TakePhotoAsync(OnCapturedPhotoToMemory);

        if (result.success)
        {
            photoCaptureFrame.UploadImageDataToTexture(_sourceTexture);
            FindQRCode(photoCaptureFrame);
        }
    }
    private byte[] ConvertAndShowOnDebugPane(PhotoCaptureFrame photoCaptureFrame)
    {
        // get image in byte array and crop it

        //  List<byte> raw = new List<byte>(targetTexture.EncodeToJPG());

        try
        {
            var fullTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
            //targetTexture.LoadRawTextureData(imageBufferList.ToArray()); // use memory stream array to create the texture
            photoCaptureFrame.UploadImageDataToTexture(fullTexture);
            // crop a  portion of the image at the center


            double cropFactor = Math.Tan(angleRadian / 2f) / Math.Tan(horizontalCameraAngleRadian / 2f);

            int     dx            = (int)(cameraResolution.width * cropFactor);
            int     dy            = (int)(cameraResolution.height * cropFactor);
            int     x             = (cameraResolution.width - dx) / 2;
            int     y             = (cameraResolution.height - dy) / 2;
            Color[] pix           = fullTexture.GetPixels(x, y, dx, dy);
            var     targetTexture = new Texture2D(dx, dy);
            targetTexture.SetPixels(pix);
            targetTexture.Apply();
            // Create a gameobject that we can apply our texture to
            if (showPicture == true)
            {
                GameObject newElement   = Instantiate <GameObject>(PhotoPrefab);
                GameObject quad         = newElement.transform.Find("Quad").gameObject;
                Renderer   quadRenderer = quad.GetComponent <Renderer>() as Renderer;
                quadRenderer.material.mainTexture = targetTexture;

                Vector3 cameraForward = scanContext.origin.forward;
                cameraForward.Normalize();
                var dist = 1.0f;
                newElement.transform.position = scanContext.origin.position + (cameraForward * dist);

                newElement.transform.rotation = Quaternion.LookRotation(cameraForward, scanContext.origin.up); // align with camera up
                Vector3 scale = newElement.transform.localScale;
                scale.x = 2f * dist * (float)Math.Tan(angleRadian / 2f);
                scale.y = scale.x * ratio; // scale the entire photo on height
                newElement.transform.localScale = scale;
            }

            List <byte> raw = new List <byte>(targetTexture.EncodeToJPG());
            return(raw.ToArray());
        }
        catch (System.Exception e)
        {
            info.SetText("error " + e.Message);
            return(null);
        }

        //return raw.ToArray();
    }
Beispiel #27
0
    private List <byte> ConvertAndShowOnDebugPane(PhotoCaptureFrame photoCaptureFrame)
    {
        var targetTexture = new Texture2D(_cameraResolution.width, _cameraResolution.height);

        photoCaptureFrame.UploadImageDataToTexture(targetTexture);
        Destroy(_debugPane.GetComponent <Renderer>().material.mainTexture);

        _debugPane.GetComponent <Renderer>().material.mainTexture = targetTexture;
        _debugPane.transform.parent.gameObject.SetActive(true);
        return(new List <byte>(targetTexture.EncodeToJPG()));
    }
Beispiel #28
0
        /// <summary>
        /// Grab worldToCamera matrix and projection matrix from the Hololens
        /// camera for correct texture placement, clip and save the texture as
        /// an image, then properly clean up (dispose of) the PhotoCaptureFrame
        /// storing relevant image data.
        /// </summary>
        /// <param name="result">
        /// Information about the success of taking the picture.
        /// </param>
        /// <param name="photoCaptureFrame">
        /// The information/image associated with the picture taken.
        /// </param>
        static void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
        {
            // After the first photo, we want to lock in the current exposure and white balance settings.
            if (lockCameraSettings && currentPhoto == 0)
            {
                //#if WINDOWS_UWP
#if WINDOWS_UWP
                //#if UNITY_WSA_10_0
                unsafe {
                    //This is how you access a COM object.
                    VideoDeviceController vdm = (VideoDeviceController)Marshal.GetObjectForIUnknown(photoCaptureObj.GetUnsafePointerToVideoDeviceController());
                    //Locks current exposure for all future images
                    vdm.ExposureControl.SetAutoAsync(false); //Figureout how to better handle the Async
                    //Locks the current WhiteBalance for all future images
                    vdm.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Fluorescent);

                    //vdm.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Manual);
                }
#endif
            }

            // Grab appropriate matrices, and write them to the local storage
            // arrays at the correct index
            Matrix4x4 worldToCameraMatrix;
            Matrix4x4 projectionMatrix;
            bool      matricesExtracted = ExtractCameraMatrices(photoCaptureFrame, out worldToCameraMatrix, out projectionMatrix);
            if (matricesExtracted)
            {
                WriteMatricesToArrays(worldToCameraMatrix, projectionMatrix, currentPhoto);

                // Set up local class texture to save as a picture/texture - Hololens camera requires BGRA32 format
                m_Texture = new Texture2D(m_CameraParameters.cameraResolutionWidth, m_CameraParameters.cameraResolutionHeight, TextureFormat.BGRA32, false);
                photoCaptureFrame.UploadImageDataToTexture(m_Texture);
                m_Texture          = ClipTexture(m_Texture);
                m_Texture.wrapMode = TextureWrapMode.Clamp;
                SaveTexture();

                // Reset displayed message to remind user how to take photos or end texturing process
                TextManager.SetText(Messages.PhotoPrompt + Messages.AppendNumPhotosTaken());
            }
            else
            {
                TextManager.SetText(Messages.MatrixFail);
            }
            // Clean up camera memory
            photoCaptureFrame.Dispose();

            // Automatically shut down the operation if the maximum number of
            // textures is reached.
            if (currentPhoto >= MaxPhotoNum)
            {
                StopTextureCapture();
            }
        }
Beispiel #29
0
    void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    {
        if (result.success)
        {
            Texture2D texture = new Texture2D(cameraResolution.width, cameraResolution.height);
            photoCaptureFrame.UploadImageDataToTexture(texture);


            // Now we could do something with the array such as texture.SetPixels() or run image processing on the lis
            StartCoroutine(IdentifyLandmark(texture.EncodeToJPG()));
        }
    }
Beispiel #30
0
    private void OnCapturePhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame frame)
    {
        if (result.success)
        {
            var texture = new Texture2D(cameraResolution.width, cameraResolution.height, TextureFormat.RGB24, false);
            frame.UploadImageDataToTexture(texture);

            Texture = texture;
        }

        photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    }