Upload images on server
Hi,
I need to take jpg or png screenshots at time intervals and store it on the file system and on a server; with this code I solved the file system saving:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
public class ScreenRecorder : MonoBehaviour {
public int framerate = 30;
public bool JPGImage = true;
public int superSize;
int frameCount = 0;
bool recording = false;
int ImageCounter = 0;
int TimeCounter = 0;
string imageFileDir = "";
void Start () {
imageFileDir = Application.persistentDataPath+"/Capture/";
frameCount = 0;
}
public void StartStopRecording () {
GameObject StartStopButtonGO;
StartStopButtonGO = GameObject.Find ("RecText");
if (!recording) {
ImageCounter = 0;
SetImageCounter (ImageCounter);
SetTimeCounter (TimeCounter);
DeleteImageDir (imageFileDir);
}
recording = !recording;
if (recording) {
Time.captureFramerate = framerate;
frameCount = -1;
StartStopButtonGO.GetComponent<Text>().text = "STOP";
Debug.Log ("Start recording");
} else {
StartStopButtonGO.GetComponent<Text>().text = "REC";
Debug.Log ("Stop recording");
}
}
void Update () {
StartCoroutine(TakeShoot ());
}
void SetImageCounter (int ImageCount) {
GameObject ImageCounterGO;
ImageCounterGO = GameObject.Find ("ImageCount");
ImageCounterGO.GetComponent<Text> ().text = ImageCount.ToString ();
}
void SetTimeCounter (int TimeCount) {
GameObject TimeCounterGO;
TimeCounterGO = GameObject.Find ("TimeCount");
TimeCounterGO.GetComponent<Text> ().text = TimeCount.ToString ();
}
IEnumerator TakeShoot() {
byte[] bytes;
string fullImageFileName = imageFileDir + "/";
string imageName = "frame";
if (recording) {
if (frameCount > 0 && frameCount % 30 == 0) {
yield return new WaitForEndOfFrame ();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height - 100; // Screen.height-button height
Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (new Rect (0, 100, width, height), 0, 0);
tex.Apply ();
imageName = "frame" + frameCount.ToString ("0000");
if (JPGImage) {
bytes = tex.EncodeToJPG (100);
imageName += ".jpg";
} else {
bytes = tex.EncodeToPNG ();
imageName += ".png";
}
fullImageFileName += imageName;
Destroy (tex);
File.WriteAllBytes (fullImageFileName, bytes);
ImageCounter++;
SetImageCounter (ImageCounter);
yield return null;
}
frameCount++;
if (frameCount % 60 == 0) {
TimeCounter++;
SetTimeCounter (TimeCounter);
}
}
}
void DeleteImageDir(string ImagePath) {
if (Directory.Exists (ImagePath)) {
var OldImageFiles = Directory.GetFiles(ImagePath);
for (int i = 0; i < OldImageFiles.Length; i++)
File.Delete (OldImageFiles [i]);
} else {
Directory.CreateDirectory (ImagePath);
}
}
}
Now I'm trying to upload images on a server and I found this examples:
https://docs.unity3d.com/ScriptReference/WWWForm.html
but what about the PERL script "http://www.my-server.com/cgi-bin/screenshot.pl"?
Or this: https://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html
but what about the CGI script "WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form);"?
So, there is a way to load an image or, in general, a file on a server?
Thank you!
[EDIT] Finally I found these examples:
https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest(v=vs.110).aspx https://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class
This way I can upload files but I need also to show the upload progress; in the second example I can add
Debug.Log (string.Format("Progress: {0}", ((100*localFileStream.Position) / localFileStream.Length)));
in requestStream.Write cycle but the upload process stucks the app so messages are written only at the end of the entire upload process.
Some idea?
Thank you