Skip to content

Instantly share code, notes, and snippets.

@seanhagen
Created March 2, 2021 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhagen/a5d583258c330a8ac8884bb4fcc8cc30 to your computer and use it in GitHub Desktop.
Save seanhagen/a5d583258c330a8ac8884bb4fcc8cc30 to your computer and use it in GitHub Desktop.
Native Camera
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