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

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

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

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

Unity

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

Q&A

解決済

2回答

1256閲覧

Unity: webcameraで得た半透明表示の写真撮影がうまくできません。

MRPM

総合スコア3

C#

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

Unity

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

0グッド

0クリップ

投稿2022/01/17 01:16

前提・実現したいこと
・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

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

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

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

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

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

guest

回答2

0

Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);

を、

Texture2D texture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.ARGB32, false);

に変える。

カメラの解像度は、

this.webCamTexture = new WebCamTexture(width, height, FPS);

で指定した値に、最も近いサポートされている解像度になるので
widthとheightは512ではなくなっていると思います。

https://docs.unity3d.com/jp/current/ScriptReference/WebCamTexture-requestedHeight.html
https://docs.unity3d.com/jp/current/ScriptReference/WebCamTexture-requestedWidth.html

投稿2022/01/17 13:05

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

MRPM

2022/01/18 01:10

ご提示いただいた方法でうまくさつえいすることができました。画面サイズについては注意が向いておらず大変参考になりました。ご丁寧にありがとうございます。
guest

0

ベストアンサー

幅が間違っているようです。
幅を640にして並べ替えたものがこちらです。
イメージ説明
width = 512; を640にすると直るのではないでしょうか。

投稿2022/01/17 12:05

ikadzuchi

総合スコア3047

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

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

MRPM

2022/01/18 01:11

ご提示いただきました解像度に変更し、目的とする写真を得ることができました。大変参考になりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問