【问题标题】:Storing a screenshot and uploading it from a browser-based app to a web server存储屏幕截图并将其从基于浏览器的应用程序上传到 Web 服务器
【发布时间】:2015-10-09 01:56:29
【问题描述】:

所以我只是想快速了解如何从基于浏览器的应用程序将屏幕截图上传到网络服务器。由于我无法将文件保存在本地然后上传,是否需要将其存储在纹理变量中?我对此的基础知识有些困惑,但我只是想指出正确的方向。我使用指向本地文件位置的字符串变量研究在线地址的所有内容,但这不适用于基于浏览器的应用程序,对吗?只是寻找一些关于如何开始为此构建 POC 的指导。感谢您的帮助。

我知道的: 我可以截图(但现在我只知道如何将它保存到本地) 我可以上传文件(但只能从本地路径)

大问题: 如何仅将屏幕截图保存在内存中?不确定这是否是正确的问题,但我希望有人知道我想要了解的内容。

最终我想做的就是截图,然后直接保存到mysql服务器。

【问题讨论】:

    标签: c# file-upload unity3d screenshot playmaker


    【解决方案1】:

    Texture2D.EncodeToPNG 的 Unity 帮助页面有一个用于捕获和上传屏幕截图的完整示例。

    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");
            }
        }
    
    }
    

    【讨论】:

    • 好的,很酷。我将该代码放入一个新的 cs 文件中,并将其附加到一个保存按钮。我在脚本顶部看到“IEnumerator Start ()”。这是否意味着它应该在应用程序启动后立即运行?在事情成功或失败的那一刻,我没有在控制台中得到任何反馈。我需要做什么来执行这个脚本?
    • 是的,启动意味着它会在启动时立即运行。要附加到按钮,您需要删除 Start 函数并改为:void TakeScreenshot() { StartCoroutine("UploadPNG"); } 之后,将按钮的 OnClick 处理程序附加到检查器中的 TakeScreenshot。当然,您仍然需要将特定的上传逻辑编码到您自己的 URL,因为它当前正在将数据发送到示例 URL。
    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2015-07-25
    相关资源
    最近更新 更多