問題の背景:
要件は、シーンを切り取り、UIを含まず、スクリーンショットをBase64形式でWebディスプレイに保存することです。
スキーム:
カメラのスクリーンショットを指定します。
1 /// <summary> 2 /// 指定相机切屏 3 /// </ summary> 4 /// <param name = "camera"> </ param> 5 /// <param name = "rect"> </ param> 6 /// <returns> </ returns> 7 public byte [] CaptureScreen(Camera camera、Rect rect) 8 { 9 RenderTexture rt = new RenderTexture(camera.pixelWidth、camera.pixelHeight、0 ); 10 11 camera.targetTexture = rt; 12 camera.Render(); 14 RenderTexture.active = rt; 15 Texture2D screenShot = new Texture2D(camera.pixelWidth、camera.pixelHeight、TextureFormat.RGBA32、false ); 16 17 screenShot.ReadPixels(RECT、0、0 ); 18 screenShot.Apply(); 19 20 camera.targetTexture = null ; 21 RenderTexture.active = null ; 22 GameObject.Destroy(rt); 23 24 バイト []バイト=screenShot.EncodeToPNG(); 25 26 戻りバイト。 27 }
ここではメモリに書き込みませんでした。必要がないため、直接アップロードしました。
Base64への転送は非常に簡単です。
1 /// <summary> 2 /// 图片流转Base64 3 /// </ summary> 4 /// <param name = "bytesArr"> </ param> 5 /// <returns> </ returns> 6 public String Texture2DToBase64(byte [] bytesArr) 7 { 8 string strbaser64 = Convert.ToBase64String(bytesArr); 9 10は strbaser64を返します。 11 }
Base64:
ネットワークで8ビットバイトコードを送信するために使用される最も一般的なエンコード方式の1つであるBase64エンコードは、バイナリから文字へのプロセスであり、HTTP環境でより長い識別情報を転送するために使用できます。Base64エンコードは読み取り不可能で、デコード後にのみ読み取ることができます。Base64は、上記の利点により、コンピューターのさまざまな分野で広く使用されています(百科事典)
スクリーンショットの例: