Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Tim-Michels · Oct 25, 2012 at 08:08 AM · texturescreenpngwebcamsnapshot

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image anwarehatela556 · Mar 15, 2018 at 07:18 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

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;
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image franktinsley · Sep 26, 2013 at 07:01 PM 0
Share

Thanks for this!!

avatar image SarthakHShah7 · Mar 29, 2017 at 07:05 AM 0
Share

Thank you for giving simple and shortest solution.

avatar image Senpaiii · Mar 15, 2018 at 06:25 PM 0
Share

how can do this for mobile ??

avatar image H-Alex · Jan 03, 2019 at 12:30 PM 0
Share

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.

avatar image
4

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · May 10, 2017 at 01:03 PM 0
Share

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?

avatar image Raigstain KnightRiderGuy · May 10, 2017 at 06:51 PM 0
Share

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 :)

avatar image Senpaiii · Mar 15, 2018 at 06:09 PM 0
Share

This way to Saving File Working For Android phone ??

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges