- Home /
How do I set pixels with WebCamTexture.GetPixels
I am currently trying to create a still image 2D texture with my webcam, but I am unsure of how to use the data from WebCamTexture.GetPixels. I see for Texture2D it has both .GetPixels and .SetPixels but no SetPixels for WebCamTexture. Do I need to convert the WebCamTexture to Texture2D? Is that even possible?
Any help is much appreciated!
Joe
Comment
Answer by ninthjarl · Feb 16, 2013 at 03:34 PM
WebCamTexture cam_texture;
void Start () {
WebCamDevice[] cam_devices = WebCamTexture.devices;
cam_texture = new WebCamTexture(cam_devices[0].name, 480, 640, 30);
if(cam_texture != null)
cam_texture.Play();
}
void OnGUI()
{
if(GUI.Button(new Rect(110,10, 100, 60), new GUIContent("capture")))
{
cam_texture.Pause();
BlitImage();
}
}
void BlitImage()
{
Texture2D destTexture = new Texture2D(cam_texture.width, cam_texture.height, TextureFormat.ARGB32, false);
Color[] textureData = cam_texture.GetPixels();
destTexture.SetPixels(textureData);
destTexture.Apply();
byte[] pngData = destTexture.EncodeToPNG();
if(File.Exists(Application.persistentDataPath+"/capturedPic2.png"))
{
File.Delete(Application.persistentDataPath+"/capturedPic2.png");
}
File.WriteAllBytes(Application.persistentDataPath+"/capturedPic2.png",pngData);
Debug.Log("pic saved to"+Application.persistentDataPath);
}
Your answer
Follow this Question
Related Questions
Projector with WebCamTexture as texture 0 Answers
Creating Snapshots or Images with WebCamTexture? 2 Answers
LoadImageIntoTexture + GetPixels + SetPixels 0 Answers
WebcamTexture GetPixel not working 1 Answer