Question by
smokegun · Jan 18, 2017 at 05:08 PM ·
c#texture2dwebcamtexture
WebCamTextures square
I want to make a picture in 1024x1024 this is needed!.
The first issue is displaying the WebCamTexture on a square rawImage this pushes the image together and ruins the original image. I want to get a square of the webcamtexture and center it instead of pushing it together/cropping.
The second issue is to save it with a resolution of 1024/1024 no matter what.
my code: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class CameraController : MonoBehaviour {
public string deviceName;
private WebCamTexture wct;
public GameObject rawImageARF;
public Text t;
// Use this for initialization
void Start()
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
wct = new WebCamTexture(deviceName, 1024, 1024, 90);
rawImageARF.GetComponent<RawImage>().texture = wct;
wct.Play();
}
void Update()
{
float videoRatio = (float)wct.width / (float)wct.height;
rawImageARF.GetComponent<AspectRatioFitter>().aspectRatio = videoRatio;
t.text = "Ratio: " + videoRatio;
if (Input.GetKeyDown(KeyCode.Mouse0))
TakePicture();
}
void TakePicture ()
{
Texture2D snap = new Texture2D(wct.width, wct.height);
snap.SetPixels(wct.GetPixels());
snap.Apply();
System.IO.File.WriteAllBytes(Application.dataPath + "/pictures/" + DateTime.Now.ToString().Replace('/', '-').Replace(':', '-') + ".png", snap.EncodeToPNG());
Debug.Log("picture saved");
}
}
Comment
Your answer
Follow this Question
Related Questions
Unity WebCamTexture looks squashed in RawImage 0 Answers
Stream webcam via Multiplayer networking or websocket 2 Answers
Convert a java script in a c#??? thank you 1 Answer
RenderTexture to Texture2D 0 Answers