前提・実現したいこと
・Unity上で、Webcameraの映像をRawImage上で半透明に表示しています。
表示されている画像を半透明にせずに写真を撮影して、JPGフォーマットで保存したい。
よろしくお願いいたします。
発生している問題・エラーメッセージ
・JPGフォーマットで保存はされるものの、得られる写真の映像がぶれて?いる。
該当のソースコード
C#
1コード 2```using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6using System.IO; 7 8// Webカメラ 9public class WebCameraTest : MonoBehaviour 10{ 11 private static int width = 512; 12 private static int height = 512; 13 private static int FPS =60; 14 private float Alpha = 0.5f; 15 16 // UI 17 RawImage rawImage; 18 WebCamTexture webCamTexture; 19 Color32[] color32; 20 21 // 撮影ボタンの表示切替 22 private bool _SS_Ope_ButtonActive = false; 23 24 // スタート時に呼ばれる 25 public void Start () 26 { 27 // Webカメラの開始 28 this.rawImage = GetComponent<RawImage>(); 29 this.webCamTexture = new WebCamTexture(width, height, FPS); 30 this.rawImage.texture = this.webCamTexture; 31 this.rawImage.color = new Color(rawImage.color.r, rawImage.color.g, rawImage.color.b, Alpha); 32 this.GetComponent<Renderer>().material.mainTexture = webCamTexture; 33 this.webCamTexture.Play(); 34 } 35 36 // カメラの選択 37 int selectCamera = 0; 38 39 // カメラの変更 40 public void ChangeCamera() 41 { 42 // カメラの取得 43 WebCamDevice[] webCamDevices = WebCamTexture.devices; 44 45 // カメラが1つの時は無処理 46 if (webCamDevices.Length <= 1) return; 47 48 // カメラの切り替え 49 selectCamera++; 50 if (selectCamera >= webCamDevices.Length) selectCamera = 0; 51 this.webCamTexture.Stop(); 52 this.webCamTexture = new WebCamTexture(webCamDevices[selectCamera].name, width, height, FPS); 53 this.rawImage.texture = this.webCamTexture; 54 this.webCamTexture.Play(); 55 } 56 57 58 // 写真を撮影する 59 void Update() 60 { 61 62 if (Input.GetKeyDown(KeyCode.Space) || Input.touchCount > 0) 63 { 64 color32 = webCamTexture.GetPixels32(); 65 Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); 66 GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; 67 texture.SetPixels(webCamTexture.GetPixels()); 68 texture.Apply(); 69 70 // JPGでエンコードする 71 byte[] bytes = texture.EncodeToJPG(); 72 // エンコードが終わったら削除する 73 Object.Destroy(texture); 74 // 保存先の指定 75 File.WriteAllBytes("Assets/SnapShot/SnapShot.jpg", bytes); 76 } 77 78 void OnGUI() 79 { 80 if (_SS_Ope_ButtonActive == false) { return; } 81 82 if (GUI.Button(new Rect(25, -190, 55,-130), "SnapShotOpeField")) 83 { 84 color32 = webCamTexture.GetPixels32(); 85 Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); 86 GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; 87 texture.SetPixels(webCamTexture.GetPixels()); 88 texture.Apply(); 89 90 // JPGでエンコードする 91 byte[] bytes = texture.EncodeToJPG(); 92 // エンコードが終わったら削除する 93 Object.Destroy(texture); 94 // 保存先の指定 95 File.WriteAllBytes("Assets/SnapShot/SnapShot.jpg", bytes); 96 } 97 } 98 } 99} 100 101自分で調べたことや試したこと 102・渉猟する範囲で関連する項目が見つからずここに質問させて頂きました。 103・カメラのFPSに依存するかもと考え、FPSの調整などを行いましたが結果に変化がありませんでした。 104 105使っているツールのバージョンなど補足情報 106Unity2020.3.22f1
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/01/18 01:10