Skip to content

Instantly share code, notes, and snippets.

@burrussmp
Last active April 26, 2020 21:08
Show Gist options
  • Save burrussmp/2115f9432a7add61eafc14642c7777e8 to your computer and use it in GitHub Desktop.
Save burrussmp/2115f9432a7add61eafc14642c7777e8 to your computer and use it in GitHub Desktop.
c# code to capture image from unity
IEnumerator TakePhoto(string type) // Start this Coroutine on some button click
{
// NOTE - you almost certainly have to do this here:
yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
tex.Apply();
//Encode to a PNG
byte[] bytes = tex.EncodeToPNG();
WWWForm form = new WWWForm();
//form.AddField("frameCount", Time.frameCount.ToString());
// form.AddField("Width",Screen.width);
form.AddField("Type",type);
form.AddField("x",bboxX);
form.AddField("y",bboxY);
form.AddField("width",widthTarget);
form.AddField("height",heightTarget);
form.AddBinaryData("file", bytes);
print("sending picture");
// Upload to a cgi script
var w = UnityWebRequest.Post("http://SERVERP:PORT", form);
yield return w.SendWebRequest();
if (w.isNetworkError || w.isHttpError)
print(w.error);
else {
string response = w.downloadHandler.text;
if (response[0] == '1'){
sideCaptured = true;
}
if (response[1] == '1'){
frontCaptured = true;
}
}
yield return null;
// send to server
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment