- Home /
How to save a snapshot of the WebcamTexture?
I'm having trouble finding a way to take snaps of my WebcamTexture every x seconds. I want to directly save the data from the Webcam directly to a PNG.
So far I've tried a technique with a plane that fills the screen with the WebcamTexture assigned. Every x seconds I can do Application.CaptureScreenshot(fileName);, but I would like a more direct way to save the pixels to a PNG format.
Anybody got some ideas? Thanks
As per your question many answer are post in bottom I just want to that if you have to take the snapshot of webcam Texture then can you save it directly in your Gmail account and their are no need any extra effort, if you want for information then you can contact to Gmail customer support.
Answer by Tim-Michels · Oct 29, 2012 at 12:24 PM
In the meantime, I figured it out. It's a lot easier than I thought. Here's my solution, maybe someone else can also use this. Cheers ;)
WebCamTexture _CamTex;
private string _SavePath = "C:/WebcamSnaps/";
int _CaptureCounter = 0;
void TakeSnapshot()
{
Texture2D snap = new Texture2D(_CamTex.width, _CamTex.height);
snap.SetPixels(_CamTex.GetPixels());
snap.Apply();
System.IO.File.WriteAllBytes(_SavePath + _CaptureCounter.ToString() + ".png", snap.EncodeToPNG());
++_CaptureCounter;
}
I don't think you need to .Apply on the texture 2D because you don't need to send all the data to the GPU for saving to a file.
Answer by Raigstain · May 09, 2017 at 09:25 PM
Hi @Tim-Michels I found your answer pretty good and useful :) just for future reference I did something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
using UnityEngine;
public class CameraTest : MonoBehaviour {
public RawImage rawimage; //Image for rendering what the camera sees.
WebCamTexture webcamTexture = null;
void Start()
{
//Save get the camera devices, in case you have more than 1 camera.
WebCamDevice[] camDevices = WebCamTexture.devices;
//Get the used camera name for the WebCamTexture initialization.
string camName = camDevices[0].name;
webcamTexture = new WebCamTexture(camName);
//Render the image in the screen.
rawimage.texture = webcamTexture;
rawimage.material.mainTexture = webcamTexture;
webcamTexture.Play();
}
void Update()
{
//This is to take the picture, save it and stop capturing the camera image.
if(Input.GetMouseButtonDown(0))
{
SaveImage();
webcamTexture.Stop();
}
}
void SaveImage()
{
//Create a Texture2D with the size of the rendered image on the screen.
Texture2D texture = new Texture2D(rawimage.texture.width, rawimage.texture.height, TextureFormat.ARGB32, false);
//Save the image to the Texture2D
texture.SetPixels(webcamTexture.GetPixels());
texture.Apply();
//Encode it as a PNG.
byte[] bytes = texture.EncodeToPNG();
//Save it in a file.
File.WriteAllBytes(Application.dataPath + "/images/testimg.png", bytes);
}
}
This is used to show you what the picture looks like without opening it before :) I hope this can be useful for someone else.
Thanks for updating this @Raigstain, Incidentally, what is a good method for loading captured images back into view? Some sort of browse and select or maybe even select last image captured?
Well, I think it depends in what you want to achieve.
For example, if you just want to show the last pic, then you already have it in the raw image or you can save the path in a string and load the file with the stream library.
I found this post where you can find a good example for loading an image: http://answers.unity3d.com/questions/432655/loading-texture-file-from-pngjpg-file-on-disk.html
I'm still new at this too, so I'm not that aware of which of the methods is the best or more efficient, but I think that this can help you :)
Answer by IdeasAV · Jan 23, 2019 at 07:45 AM
can anyone help me with how to convert the saved image to black and white
https://www.youtube.com/watch?v=huZPmi1WMZM
above shows the effect needed for unity3d
Your answer
Follow this Question
Related Questions
Creating Snapshots or Images with WebCamTexture? 2 Answers
png texture not showing correctly 2 Answers
WebCamTexture not working on Instance objects 0 Answers
Small (16x16) Textures Importing Super Low Quality 1 Answer
webcam texture format 0 Answers