存储截图并将其从基于浏览器的应用程序上传到web服务器
本文关键字:应用程序 应用 程序上 服务器 web 浏览器 存储 | 更新日期: 2023-09-27 18:03:33
所以我只是在寻找如何从基于浏览器的应用程序上传屏幕截图到web服务器的快速初级读本。因为我不能在本地保存文件,然后上传它,我需要将其存储在纹理变量中吗?我对这方面的基本知识有点困惑,但我只是想找到正确的方向。我研究过的所有在线地址都使用指向本地文件位置的字符串变量,但这不适用于基于浏览器的应用程序,对吗?只是寻找一些关于如何开始建立POC的指导。谢谢你的帮助。
我知道的:我可以截图(但现在我只知道如何保存到本地)我可以上传文件(但只能从本地路径)
大问题:如何将截图只保存在内存中?我不确定这个问题是否正确,但我希望有人知道我想要表达的意思。
最后我想做的是截图,然后直接保存到mysql服务器。
Texture2D的Unity帮助页面。EncodeToPNG有一个捕获和上传截图的完整示例。
http://docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html// Saves screenshot as PNG file.
using UnityEngine;
using System.Collections;
using System.IO;
public class PNGUploader : MonoBehaviour {
// Take a shot immediately
IEnumerator Start () {
yield return UploadPNG();
}
IEnumerator UploadPNG() {
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
// For testing purposes, also write to a file in the project folder
// File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
// Create a Web Form
WWWForm form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("fileUpload",bytes);
// Upload to a cgi script
WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form);
yield return w;
if (w.error != null) {
Debug.Log(w.error);
} else {
Debug.Log("Finished Uploading Screenshot");
}
}
}