1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- public class ImageUploader : MonoBehaviour
- {
- //public GameObject sendingPanel;
- public GameObject errorPanel;
- public GameObject masterConnector;
- // Start is called before the first frame update
- void Start()
- {
-
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- public void UploadImageToServer()
- {
- //sendingPanel.SetActive(true); //delete sending panel since it block the image in the middle
- StartCoroutine( TakeScreenshotAndSave() );
-
- }
- private IEnumerator TakeScreenshotAndSave()
- {
- yield return new WaitForEndOfFrame();
- Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
- ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
- ss.Apply();
- // SEND THE PHOTO TO SERVER
- byte[] bytes = ss.EncodeToPNG();
- Debug.Log( "Permission result: sucess");
- string gameNumber = masterConnector.GetComponent<MasterCreateGame>().GetGameNumber();
- StartCoroutine(SendImageToServer(gameNumber, bytes));
- // To avoid memory leaks
- Destroy( ss );
- }
- public IEnumerator SendImageToServer(string gameNumber, byte[] image)
- {
- string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_image_to_game";
- // Create a Web Form
- WWWForm form = new WWWForm();
- form.AddField("game", gameNumber);
- form.AddBinaryData("img", image);
- using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
- {
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- // PROMPT ERROR
- errorPanel.SetActive(true);
- }
- else
- {
- Debug.Log("Form upload complete!");
- //sendingPanel.SetActive(false); //delete sending panel since it block the image in the middle
-
-
- }
- }
- }
- }
|