- Home /
WebCamTexture.width and height returning [16,16] unless reinitialized
Hello,
I am trying to store a picture from a camera with WebCamTexture. The function Savepicture(string path) uses a Texture2D to set pixels retrieved from the WebCamTexture class field, I end up getting a 16X16 image. I narrowed down the problem to the WebCamTexture. When I reinitialize it, the problem no longer occurs. I am not entirely sure why this is occurring, shouldn't I just be able to initialize WebCamTexture once during runtime?
 public class AndroidCameraController: MonoBehaviour, ICameraController
 {
     private WebCamTexture _cameraTexture;
     private WebCamDevice[] _devices;
     private string _deviceName;
     private Renderer _renderer;
     private Texture2D _snapShot;
     public WebCamTexture CameraTexture
     {
         get
         {
             if (_cameraTexture == null)
             {
                 _cameraTexture = new WebCamTexture(FrontFacingDeviceName,400,300,15);
                 RendererMaterial.material.mainTexture = _cameraTexture;
             }
             return _cameraTexture;
         }
         set { _cameraTexture = value; }
     }
     public WebCamDevice[] Devices
     {
         get
         {
             if (_devices == null)
             {
                 _devices = WebCamTexture.devices;
             }
             return _devices;
         }
     }
     public Renderer RendererMaterial
     {
         get
         {
             if (_renderer == null)
             {
                 _renderer = GetComponent<Renderer>();
             }
             return _renderer;
         }
     }
     /// <summary>
     /// we'll only grab the front facing device's name
     /// </summary>
     public string FrontFacingDeviceName
     {
         get
         {
             if (_deviceName == null)
             {
                 for (int i = 0; i < Devices.Length; i++)
                 {
                     if (Devices[i].isFrontFacing)
                     {
                         _deviceName = Devices[i].name;
                         break;
                     }
                 }
                 
             }
             return _deviceName;
         }
         set
         {
             throw new NotImplementedException();
         }
     }
     public Texture2D SnapShot
     {
         get
         {
             if (_snapShot == null)
             {
                 _snapShot = new Texture2D(CameraTexture.width,CameraTexture.height);
             }
             return _snapShot;
         }
         set { _snapShot = value; }
     }
     public void SavePicture(string path)
     {
         SnapShot = new Texture2D(CameraTexture.width, CameraTexture.height);
         SnapShot.SetPixels(CameraTexture.GetPixels());
         SnapShot.Apply();
         System.IO.File.WriteAllBytes(path, SnapShot.EncodeToPNG());
     }
   
     public void LoadPicture(string path, Action<bool> callbackAction)
     {
         try
         {
             if (SnapShot.LoadImage(File.ReadAllBytes(path)))
             {
                 RendererMaterial.material.mainTexture = SnapShot;
                 callbackAction(true);
             }
             
         }
         catch (FileNotFoundException e)
         {
             callbackAction(false);
         }
       
      
     }
     public void DisplayCameraStream()
     {
         CameraTexture.Play();
     }
     public void HideCameraStream()
     {
         CameraTexture.Stop();
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                