Created
March 2, 2021 19:33
-
-
Save seanhagen/a5d583258c330a8ac8884bb4fcc8cc30 to your computer and use it in GitHub Desktop.
Native Camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
public class CameraScript : MonoBehaviour { | |
// Start is called before the first frame | |
void Start() { | |
NativeCamera.RequestPermission(); | |
NativeCamera.TakePicture(CamCallback); | |
} | |
void CamCallback(string path){ | |
Debug.Log("Photo path: " + path); | |
if (path != null){ | |
var data = NativeCamera.GetImageProperties(path); | |
Debug.Log("Image data: "); | |
Debug.Log(data); | |
Debug.Log("Uploading photo!"); | |
StartCoroutine(UploadFile(path)); | |
} else { | |
Debug.Log("No photo?"); | |
} | |
} | |
IEnumerator UploadFile(string photoPath){ | |
Debug.Log("Uploading photo to sever"); | |
List<IMultipartFormSection> formData = new List<IMultipartFormSection>(); | |
byte[] contents = File.ReadAllBytes(photoPath); | |
var file = new MultipartFormFileSection("photo", contents, "photo.jpg", "images/jpeg"); | |
formData.Add(file); | |
Debug.Log("Built form fields"); | |
UnityWebRequest www = UnityWebRequest.Post("http://10.0.1.100:8080/upload", formData); | |
Debug.Log("Built request, sending"); | |
yield return www.SendWebRequest(); | |
Debug.Log("Request complete!"); | |
if (www.result == UnityWebRequest.Result.ConnectionError|| | |
www.result == UnityWebRequest.Result.ProtocolError){ | |
Debug.Log($"Unable to upload: {www.error}"); | |
} else { | |
Debug.Log("Uploaded photo!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment