Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Net;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.XR.WSA.WebCam;
- using Vuforia;
- using System.Threading.Tasks;
- using UnityEngine.UI;
- using Microsoft.MixedReality.Toolkit.Core.EventDatum.Input;
- using Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem.Handlers;
- using Newtonsoft.Json;
- public class NewPhotoCapture : MonoBehaviour
- {
- PhotoCapture photoCaptureObject = null;
- Texture2D targetTexture = null;
- public string path = "";
- CameraParameters cameraParameters = new CameraParameters();
- public Text response;
- public Uri url = new Uri("http://localhost:61433/api/Licenseplate/ReceiveFile");
- private void Awake()
- {
- // Create a PhotoCapture object
- var cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
- targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
- PhotoCapture.CreateAsync(false, captureObject =>
- {
- photoCaptureObject = captureObject;
- cameraParameters.hologramOpacity = 0.0f;
- cameraParameters.cameraResolutionWidth = cameraResolution.width;
- cameraParameters.cameraResolutionHeight = cameraResolution.height;
- cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
- VuforiaBehaviour.Instance.gameObject.SetActive(false);
- // Activate the camera
- photoCaptureObject.StartPhotoModeAsync(cameraParameters, result =>
- {
- if (!result.success)
- Debug.LogError("Couldn't start photo mode!", this);
- });
- });
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.K) || Input.GetKeyDown("k"))
- {
- response.color = Color.white;
- if (photoCaptureObject == null) return;
- // Take a picture
- Debug.Log("Capture a photo");
- response.text = "Du har tagit ett foto";
- photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
- }
- }
- private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
- {
- // Copy the raw image data into the target texture
- photoCaptureFrame.UploadImageDataToTexture(targetTexture);
- targetTexture = CropAroundCenter(targetTexture, new Vector2Int(300, 120));
- targetTexture.Apply();
- byte[] bytes = targetTexture.EncodeToPNG();
- Debug.Log("The picture is being uploaded...");
- StartCoroutine(Post(bytes, HandleUploadResult));
- }
- public static Texture2D CropAroundCenter(Texture2D input, Vector2Int newSize)
- {
- if (input.width < newSize.x || input.height < newSize.y)
- {
- Debug.LogError("You can't cut out an area of an image which is bigger than the image itself!");
- return null;
- }
- // get the pixel coordinate of the center of the input texture
- var center = new Vector2Int(input.width / 2, input.height / 2);
- // Get pixels around center
- var pixels = input.GetPixels(center.x - newSize.x / 2, center.y - newSize.y / 2, newSize.x, newSize.y, 0);
- // Create a new texture with newSize
- var output = new Texture2D(newSize.x, newSize.y);
- output.SetPixels(pixels);
- output.Apply();
- return output;
- }
- private void HandleUploadResult(string result)
- {
- var carObj = JsonConvert.DeserializeObject<Car>(result);
- if (carObj == null)
- {
- response.text = null;
- response.text = "Antingen kunde inte bilen hittas eller så uppstod det ett systemfel.";
- }
- else {
- //Debug.Log(result);
- response.text = "Registreringsnummer: " + carObj.LicensePlateNumber.ToString()
- //+ "\n" + "Färg: " + carObj.Color
- + "\n" + "Bilmärke: " + carObj.Brand
- + "\n" + "Modell: " + carObj.Model
- + "\n" + "Ägare: " + carObj.Owner;
- if (carObj.IsStolen)
- {
- response.color = Color.blue;
- response.text = response.text + "\n\n" + "Fordonet är anmäld stulen!";
- }
- if (carObj.VehicleFines >= 5000)
- {
- response.color = Color.red;
- response.text = response.text + "\n\n" + "Fordonets böter överträffar 5000 kr!";
- }
- Debug.Log("Information visas");
- }
- }
- public IEnumerator Post(byte[] bytes, Action<string> callback)
- {
- var www = new UnityWebRequest(url, "POST")
- {
- uploadHandler = new UploadHandlerRaw(bytes),
- downloadHandler = new DownloadHandlerBuffer()
- };
- www.SetRequestHeader("Content-Type", "application/json");
- yield return www.SendWebRequest();
- if (www.isNetworkError || www.isHttpError)
- {
- Debug.Log(www.error);
- }
- else
- {
- Debug.Log("Upload complete!");
- callback?.Invoke(www.downloadHandler.text);
- }
- }
- }
- public class Car
- {
- public int CarID { get; set; }
- public string LicensePlateNumber { get; set; }
- public string Color { get; set; }
- public string Brand { get; set; }
- public string Model { get; set; }
- public string Owner { get; set; }
- public int VehicleFines { get; set; }
- public bool IsStolen { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement