| using System; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.Linq; |
| using UnityEngine; |
| using UnityEngine.Windows.WebCam; |
| |
| public class MyPhotoCapture : MonoBehaviour |
| { |
| PhotoCapture photoCaptureObject = null; |
| |
| internal bool captureIsActive; |
| |
| private void Start() |
| { |
| |
| } |
| |
| public void StartCapture() |
| { |
| if (!captureIsActive) |
| { |
| captureIsActive = true; |
| PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated); |
| } |
| else |
| { |
| captureIsActive = false; |
| } |
| } |
| |
| void OnPhotoCaptureCreated(PhotoCapture captureObject) |
| { |
| photoCaptureObject = captureObject; |
| |
| var cameraResolution = PhotoCapture.SupportedResolutions |
| .OrderByDescending((res) => res.width * res.height) |
| .First(); |
| |
| var cameraParams = new CameraParameters() |
| { |
| hologramOpacity = 0f, |
| cameraResolutionWidth = cameraResolution.width, |
| cameraResolutionHeight = cameraResolution.height, |
| pixelFormat = CapturePixelFormat.JPEG |
| }; |
| |
| captureObject.StartPhotoModeAsync(cameraParams, OnPhotoModeStarted); |
| |
| } |
| |
| private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result) |
| { |
| if (result.success) |
| { |
| |
| photoCaptureObject.TakePhotoAsync((photoCaptureResult, frame) => |
| { |
| if (photoCaptureResult.success) |
| { |
| Debug.Log("Photo capture done."); |
| |
| var buffer = new List<byte>(); |
| frame.CopyRawImageDataIntoBuffer(buffer); |
| StartCoroutine(CustomVisionAnalyser.Instance.AnalyseLastImageCaptured(buffer.ToArray())); |
| } |
| photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode); |
| }); |
| } |
| else |
| { |
| Debug.LogError("Unable to start photo mode!"); |
| } |
| } |
| |
| void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result) |
| { |
| photoCaptureObject.Dispose(); |
| photoCaptureObject = null; |
| |
| captureIsActive = false; |
| } |
| } |