前提・実現したいこと
・Unity上で、Webcameraの映像をRawImage上で半透明に表示しています。
表示されている画像を半透明にせずに写真を撮影して、JPGフォーマットで保存したい。
よろしくお願いいたします。
発生している問題・エラーメッセージ
・JPGフォーマットで保存はされるものの、得られる写真の映像がぶれて?いる。
該当のソースコード
C#
コード ```using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; // Webカメラ public class WebCameraTest : MonoBehaviour { private static int width = 512; private static int height = 512; private static int FPS =60; private float Alpha = 0.5f; // UI RawImage rawImage; WebCamTexture webCamTexture; Color32[] color32; // 撮影ボタンの表示切替 private bool _SS_Ope_ButtonActive = false; // スタート時に呼ばれる public void Start () { // Webカメラの開始 this.rawImage = GetComponent<RawImage>(); this.webCamTexture = new WebCamTexture(width, height, FPS); this.rawImage.texture = this.webCamTexture; this.rawImage.color = new Color(rawImage.color.r, rawImage.color.g, rawImage.color.b, Alpha); this.GetComponent<Renderer>().material.mainTexture = webCamTexture; this.webCamTexture.Play(); } // カメラの選択 int selectCamera = 0; // カメラの変更 public void ChangeCamera() { // カメラの取得 WebCamDevice[] webCamDevices = WebCamTexture.devices; // カメラが1つの時は無処理 if (webCamDevices.Length <= 1) return; // カメラの切り替え selectCamera++; if (selectCamera >= webCamDevices.Length) selectCamera = 0; this.webCamTexture.Stop(); this.webCamTexture = new WebCamTexture(webCamDevices[selectCamera].name, width, height, FPS); this.rawImage.texture = this.webCamTexture; this.webCamTexture.Play(); } // 写真を撮影する void Update() { if (Input.GetKeyDown(KeyCode.Space) || Input.touchCount > 0) { color32 = webCamTexture.GetPixels32(); Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; texture.SetPixels(webCamTexture.GetPixels()); texture.Apply(); // JPGでエンコードする byte[] bytes = texture.EncodeToJPG(); // エンコードが終わったら削除する Object.Destroy(texture); // 保存先の指定 File.WriteAllBytes("Assets/SnapShot/SnapShot.jpg", bytes); } void OnGUI() { if (_SS_Ope_ButtonActive == false) { return; } if (GUI.Button(new Rect(25, -190, 55,-130), "SnapShotOpeField")) { color32 = webCamTexture.GetPixels32(); Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; texture.SetPixels(webCamTexture.GetPixels()); texture.Apply(); // JPGでエンコードする byte[] bytes = texture.EncodeToJPG(); // エンコードが終わったら削除する Object.Destroy(texture); // 保存先の指定 File.WriteAllBytes("Assets/SnapShot/SnapShot.jpg", bytes); } } } } 自分で調べたことや試したこと ・渉猟する範囲で関連する項目が見つからずここに質問させて頂きました。 ・カメラのFPSに依存するかもと考え、FPSの調整などを行いましたが結果に変化がありませんでした。 使っているツールのバージョンなど補足情報 Unity2020.3.22f1
まだ回答がついていません
会員登録して回答してみよう