SHARE
TWEET

PhotoCapture Script

FearlessFox Apr 26th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Net;
  9. using System.Text;
  10. using UnityEngine;
  11. using UnityEngine.Networking;
  12. using UnityEngine.XR.WSA.WebCam;
  13. using Vuforia;
  14. using System.Threading.Tasks;
  15. using UnityEngine.UI;
  16. using Microsoft.MixedReality.Toolkit.Core.EventDatum.Input;
  17. using Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem.Handlers;
  18. using Newtonsoft.Json;
  19.  
  20. public class NewPhotoCapture : MonoBehaviour
  21. {
  22.     PhotoCapture photoCaptureObject = null;
  23.     Texture2D targetTexture = null;
  24.     public string path = "";
  25.     CameraParameters cameraParameters = new CameraParameters();
  26.     public Text response;
  27.     public Uri url = new Uri("http://localhost:61433/api/Licenseplate/ReceiveFile");
  28.  
  29.     private void Awake()
  30.     {
  31.         // Create a PhotoCapture object
  32.         var cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
  33.         targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
  34.  
  35.         PhotoCapture.CreateAsync(false, captureObject =>
  36.         {
  37.             photoCaptureObject = captureObject;
  38.             cameraParameters.hologramOpacity = 0.0f;
  39.             cameraParameters.cameraResolutionWidth = cameraResolution.width;
  40.             cameraParameters.cameraResolutionHeight = cameraResolution.height;
  41.             cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
  42.  
  43.             VuforiaBehaviour.Instance.gameObject.SetActive(false);
  44.  
  45.             // Activate the camera
  46.             photoCaptureObject.StartPhotoModeAsync(cameraParameters, result =>
  47.             {
  48.                 if (!result.success)
  49.                     Debug.LogError("Couldn't start photo mode!", this);
  50.             });
  51.         });
  52.     }
  53.  
  54.     private void Update()
  55.     {
  56.         if (Input.GetKeyDown(KeyCode.K) || Input.GetKeyDown("k"))
  57.         {
  58.             response.color = Color.white;
  59.             if (photoCaptureObject == null) return;
  60.  
  61.             // Take a picture
  62.             Debug.Log("Capture a photo");
  63.             response.text = "Du har tagit ett foto";
  64.  
  65.             photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
  66.         }
  67.     }
  68.  
  69.     private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
  70.     {
  71.         // Copy the raw image data into the target texture
  72.         photoCaptureFrame.UploadImageDataToTexture(targetTexture);
  73.         targetTexture = CropAroundCenter(targetTexture, new Vector2Int(300, 120));
  74.  
  75.         targetTexture.Apply();
  76.  
  77.         byte[] bytes = targetTexture.EncodeToPNG();
  78.                
  79.         Debug.Log("The picture is being uploaded...");
  80.  
  81.        
  82.         StartCoroutine(Post(bytes, HandleUploadResult));
  83.     }
  84.  
  85.     public static Texture2D CropAroundCenter(Texture2D input, Vector2Int newSize)
  86.     {
  87.         if (input.width < newSize.x || input.height < newSize.y)
  88.         {
  89.             Debug.LogError("You can't cut out an area of an image which is bigger than the image itself!");
  90.             return null;
  91.         }
  92.  
  93.         // get the pixel coordinate of the center of the input texture
  94.         var center = new Vector2Int(input.width / 2, input.height / 2);
  95.  
  96.         // Get pixels around center
  97.         var pixels = input.GetPixels(center.x - newSize.x / 2, center.y - newSize.y / 2, newSize.x, newSize.y, 0);
  98.  
  99.         // Create a new texture with newSize
  100.         var output = new Texture2D(newSize.x, newSize.y);
  101.  
  102.         output.SetPixels(pixels);
  103.         output.Apply();
  104.  
  105.         return output;
  106.     }
  107.            
  108.     private void HandleUploadResult(string result)
  109.     {
  110.         var carObj = JsonConvert.DeserializeObject<Car>(result);
  111.  
  112.         if (carObj == null)
  113.         {
  114.             response.text = null;
  115.             response.text = "Antingen kunde inte bilen hittas eller så uppstod det ett systemfel.";
  116.         }
  117.         else {
  118.         //Debug.Log(result);
  119.        
  120.         response.text = "Registreringsnummer: " + carObj.LicensePlateNumber.ToString()
  121.             //+ "\n" + "Färg: " + carObj.Color
  122.             + "\n" + "Bilmärke: " + carObj.Brand
  123.             + "\n" + "Modell: " + carObj.Model
  124.             + "\n" + "Ägare: " + carObj.Owner;
  125.  
  126.         if (carObj.IsStolen)
  127.         {
  128.             response.color = Color.blue;
  129.             response.text = response.text + "\n\n" + "Fordonet är anmäld stulen!";
  130.         }
  131.  
  132.         if (carObj.VehicleFines >= 5000)
  133.         {
  134.             response.color = Color.red;
  135.             response.text = response.text + "\n\n" + "Fordonets böter överträffar 5000 kr!";
  136.         }
  137.  
  138.         Debug.Log("Information visas");
  139.         }
  140.     }
  141.    
  142.     public IEnumerator Post(byte[] bytes, Action<string> callback)
  143.     {
  144.         var www = new UnityWebRequest(url, "POST")
  145.         {
  146.             uploadHandler = new UploadHandlerRaw(bytes),
  147.             downloadHandler = new DownloadHandlerBuffer()
  148.         };
  149.         www.SetRequestHeader("Content-Type", "application/json");
  150.  
  151.         yield return www.SendWebRequest();
  152.  
  153.            if (www.isNetworkError || www.isHttpError)
  154.            {
  155.                 Debug.Log(www.error);
  156.            }
  157.            else
  158.            {
  159.                 Debug.Log("Upload complete!");
  160.             callback?.Invoke(www.downloadHandler.text);
  161.            }
  162.     }
  163. }
  164.  
  165. public class Car
  166. {
  167.     public int CarID { get; set; }
  168.     public string LicensePlateNumber { get; set; }
  169.     public string Color { get; set; }
  170.     public string Brand { get; set; }
  171.     public string Model { get; set; }
  172.     public string Owner { get; set; }
  173.     public int VehicleFines { get; set; }
  174.     public bool IsStolen { get; set; }
  175. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top