質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2051閲覧

Unity)WebCamTextureが横向きに保存される

navesanta

総合スコア198

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/01/13 06:10

UnityでWebCamTextureを使って撮影した画像を保存するアプリを
作成したいのですが撮影時の向きが横になってしまうので

c#

1Vector3 angles = rectTrans.eulerAngles; 2angles.y = WebCamTexture.devices[index].isFrontFacing?180: 0; 3angles.z = -active.videoRotationAngle; 4rectTrans.eulerAngles = angles;

で向き調整をしてうまくいきました。

しかしactive.GetPixelsで書き出してEncodeToPNG()を使って保存した
データを見ると最初の横になった時点での保存になってしまいます。

この挙動はシュミレータでは横にならず問題なく
デバイステストの時のみ横になって保存されます。
解決方法わかる方お教えください。

環境)
PC: Windows10
Unity2019.4.0f1
言語:C#

c#

1public class WebCamera : MonoBehaviour 2{ 3 [SerializeField]RawImage rawImage = null; 4 [SerializeField]GameObject xButton; 5 [SerializeField]GameObject returnButton; 6 List<PhotoType> photoList; 7 List<WebCamTexture> webCamTextures; 8 WebCamTexture active; 9 Texture2D texture2D; 10 public int FPS = 15; 11 int fileNum; 12 string authId; 13 string fileName; 14 string dataPath; 15 bool isLoadFinished = false; 16 Vector3 angles; 17 18 void Start() 19 { 20 xButton.SetActive(true); 21 webCamTextures = new List<WebCamTexture>(); 22 photoList = new List<PhotoType>(); 23 photoList = PlayerPrefsUtility.LoadList<PhotoType>("photolist"); 24 fileNum = PlayerPrefs.GetInt("filenum", 0); 25 //利用できるデバイスカメラを生成する 26 foreach(var d in WebCamTexture.devices) 27 { 28 WebCamTexture texture = new WebCamTexture( 29 d.name, 30 Screen.currentResolution.width, 31 Screen.currentResolution.height, 32 FPS); 33 webCamTextures.Add(texture); 34 } 35 //cameraを再生開始、RawImageにWebCamTextureを設定する 36 const int index = 0; 37 active = webCamTextures[index]; 38 // active = webCamTextures[0]; 39 rawImage.texture = active; 40 active.Play(); 41 42 RectTransform rectTrans = rawImage.GetComponent<RectTransform>(); 43 //向き調整 44 Vector3 angles = rectTrans.eulerAngles; 45 angles.y = WebCamTexture.devices[index].isFrontFacing?180: 0; 46 angles.z = -active.videoRotationAngle; 47 rectTrans.eulerAngles = angles; 48 49 //サイズ調整 50 float scale = Screen.width / (float)active.height; 51 Vector2 size = rectTrans.sizeDelta; 52 size.x = active.width * scale; 53 size.y = active.height * scale; 54 rectTrans.sizeDelta = size; 55 } 56public void OnClickShot() 57{ 58 if( active == null ) 59 { 60 return; 61 } 62 63 if( !active.isPlaying ) 64 { 65 return; 66 } 67 // Debug.Log("0active.width/" + active.width + " active.height/" + active.height ); 68 active.Pause(); 69 xButton.SetActive(false); 70 returnButton.SetActive(true); 71} 72public void OnClickSave() 73{ 74 authId = PlayerPrefs.GetString("authid", ""); 75 Debug.Log("authid2" + authId); 76 //webCamTextureをtexture2Dに変換する 77 texture2D = new Texture2D (700, 700, TextureFormat.RGBA32, false); 78 Color[] pix = active.GetPixels(360, 0, 700, 700); 79 texture2D.SetPixels(pix); 80 texture2D.Apply(); 81 //texture2Dをpngに変換する 82 byte[] png = texture2D.EncodeToPNG(); 83 //pngとtexture2Dの競合はWriteAllBytes時にメモリーリークになるのでtexture2Dを削除する 84 Destroy(texture2D); 85 //Application.persistentDataPath(data/data)に保存する。その際iCloud対象から外す 86 dataPath = Application.persistentDataPath + "/" + fileName +".png"; 87 photoList.Add(new PhotoType(fileName, 0, "", GetDateTime(), 0)); 88 PlayerPrefsUtility.SaveList<PhotoType>("photolist", photoList); 89 90 fileNum++; 91 PlayerPrefs.SetInt("filenum", fileNum); //filenum連番管理 92 PlayerPrefs.SetString("filename", fileName); //editorの背景用画像セット 93 PlayerPrefs.Save(); 94 95 // UnityEngine.iOS.Device.SetNoBackupFlag(dataPath); //iosには必要 96 File.WriteAllBytes(dataPath, png); 97 active.Stop(); 98 } 99 int GetDateTime() 100 { 101 DateTime now = DateTime.Now; 102 return now.Year * 10000 + now.Month * 100 + now.Day; 103 } 104} 105

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

ネイティブスクリプトを作成して対応できました。

投稿2021/02/19 07:46

navesanta

総合スコア198

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問