- Home /
This question was
closed Feb 21, 2016 at 05:19 PM by
KnightRiderGuy for the following reason:
The question is answered, right answer was accepted
Question by
KnightRiderGuy · Feb 21, 2016 at 03:55 PM ·
c#webcamsave-to-filecapturewrite data
Write Webcam Capture Tp Specific Folder
I have a webcam capture script but I am having trouble trying to figure out how to get it to write the image to somewhere more easy to find like a folder on my desktop where I can store and retrieve the web camera captures from.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class WebCamScript : MonoBehaviour {
public RawImage rawimage;
WebCamTexture webCamTexture;
private string absolutePath = "./Surveillance/CamCapture";
string fileName = "filename";
void Start () {
if (!System.IO.Directory.Exists (absolutePath))
System.IO.Directory.CreateDirectory (absolutePath);
if (Application.isEditor)
absolutePath = "Assets/WebcamSnaps";
WebCamDevice[] cam_devices = WebCamTexture.devices;
webCamTexture = new WebCamTexture(cam_devices[0].name, 480, 640, 30);
rawimage.texture = webCamTexture;
if(webCamTexture != null){
//webCamTexture.Play();
}
}
void Update ()
{
}
public void GoWebCam01(){
//Start streaming the images captured by the webcam into the texture
webCamTexture.Play();
}
public void GoWebCamStop(){
//Start streaming the images captured by the webcam into the texture
webCamTexture.Stop();
}
//NOTE: This method seems to capture a web camera photo but freezes the camera image.
public void TakePhoto()
{
webCamTexture.Pause();
BlitImage();
}
void BlitImage()
{
Texture2D destTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.ARGB32, false);
Color[] textureData = webCamTexture.GetPixels();
destTexture.SetPixels(textureData);
destTexture.Apply();
byte[] pngData = destTexture.EncodeToPNG();
//if(File.Exists(Application.persistentDataPath+"/capturedPic2.png"))
if(File.Exists("WebcamSnaps" + "photo.png"))
{
//File.Delete(Application.persistentDataPath+"/capturedPic2.png");
File.Delete("WebcamSnaps" + "photo.png");
}
//File.WriteAllBytes(Application.persistentDataPath+"/capturedPic2.png",pngData);
File.WriteAllBytes("WebcamSnaps" + "photo.png",pngData);
//Debug.Log("pic saved to"+Application.persistentDataPath);
Debug.Log("WebcamSnaps");
}
}
Comment
Best Answer
Answer by KnightRiderGuy · Feb 21, 2016 at 05:19 PM
OK after playing with it for a bit I figured out what it was.
void BlitImage()
{
Texture2D destTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.ARGB32, false);
Color[] textureData = webCamTexture.GetPixels();
destTexture.SetPixels(textureData);
destTexture.Apply();
byte[] pngData = destTexture.EncodeToPNG();
//if(File.Exists(Application.persistentDataPath+"/capturedPic2.png"))
//if(File.Exists("WebcamSnaps" + "photo.png"))
if(File.Exists(absolutePath + "surveillanceCapture01.png"))
{
//File.Delete(Application.persistentDataPath+"/capturedPic2.png");
//File.Delete("WebcamSnaps" + "photo.png");
File.Delete(absolutePath + "surveillanceCapture01.png");
}
//File.WriteAllBytes(Application.persistentDataPath+"/capturedPic2.png",pngData);
//File.WriteAllBytes("WebcamSnaps" + "photo.png",pngData);
File.WriteAllBytes(absolutePath + "surveillanceCapture01.png", pngData);
//Debug.Log("pic saved to"+Application.persistentDataPath);
//Debug.Log("WebcamSnaps");
Debug.Log("File Saved to Desktop/Surveillance/CamCapture/");
}
Follow this Question
Related Questions
How to save&load every Transform data of a child of a GameObject? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Recording video from webCam 1 Answer
WebCamTexture IOS crashing? 1 Answer