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
1
Question by wonker21 · Sep 06, 2013 at 03:57 PM · webcamtexturepngreadpixelssetpixelsencode

EncodeToPng and WebCamTexture PROBLEM

Hello everyone I have a problem. My code saves or encodes Png photo very badly. It saves photo but it's messed up for some reason. Tested on Android and on PC but its the same all time. Here is what I get:

Can Someone Help Me? THANKS IN ADVANCE.

alt text

Here is my Code:

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class AndroidCamera : MonoBehaviour
 {
     public Vector2 photoSize;
     public int photoFrames;
     public GUIStyle guiStyle;
     
     private WebCamTexture webCamTexture;
     private string camName;
 
     void Start () 
     {
         WebCamDevice[] devices = WebCamTexture.devices;
         webCamTexture = new WebCamTexture(GetCamera(), (int)photoSize.x, (int)photoSize.y, photoFrames);
         renderer.material.mainTexture = webCamTexture;
         webCamTexture.Play();
         Screen.orientation = ScreenOrientation.Portrait;
     }
     
     void TakePhoto()
     {   
         Texture2D takenPhoto = new Texture2D((int)photoSize.x, (int)photoSize.y, TextureFormat.ARGB32, false);
         
         Color[] texData = webCamTexture.GetPixels();
         
         takenPhoto.SetPixels(texData);
         takenPhoto.Apply();
         
         byte[] photoData = takenPhoto.EncodeToPNG();
         Destroy(takenPhoto);
         
         //if(File.Exists(Application.persistentDataPath + "/AvatarPhoto.png"))
         //{
         //    File.Delete(Application.persistentDataPath + "/AvatarPhoto.png");
         //}
         File.WriteAllBytes(Application.dataPath + "/AvatarPhoto.png", photoData);
     }
     
     void OnGUI()
     {
         if(GUI.Button(new Rect(0, 10, Screen.width, Screen.height / 5), "TAKE PHOTO!", guiStyle))
         {
             TakePhoto();
         }
     }
 }


avatarphoto.png (304.6 kB)
Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ImranZahid · Sep 28, 2014 at 08:02 AM

Make sure you put the right image size, try this

Texture2D takenPhoto = new Texture2D((int)renderer.material.mainTexture.width, (int)renderer.material.mainTexture.height);

It worked for me!

Comment
Add comment · Show 1 · 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 Bunny83 · Sep 28, 2014 at 11:10 AM 0
Share

+1

Exactly. The result you see is clearly a wrong image size. Read the docs carefully:

The requested width, height and framerate specified by the parameters may not be supported by the chosen camera. In such cases, the closest available values will be used

So you can't rely on the size you've setup. So the image data you actually get from your webcam might be smaller / larger than your target image. That's why you have interleaved lines because either the line is too long an propergates onto the next one, shifting eferything towards the end, or the source line is too short and the target is filled partially from the next line.

Since we can kind of "see" you about 4 times i would say your actual width might be at 4 times larger. It could also be an interference size which produces this 4-images view. To be on the safe side you should use the width and height of the original webcam texture like ImranZahid did.

edit
I've shifted your image(s) back by simply enlarging the width pixel by pixel. The image is correctly shown at a width of 640, so that was the actual image width. Since you "packed" the image into a 512x512 image we can't say much about the original height since the data has been truncated. It's most likely 640x480 so there are about 45k pixels missing (`640*480 - 512*512`)

avatar image
0

Answer by Catlard · Sep 08, 2013 at 04:14 AM

I'm positive this is problem with not yielding -- it has to do with the computer attempting to perform large operations like WriteAllBytes, on line 39, all in one frame. What I would suggest is that you make TakePhoto an IEnumerator, and try yielding after that line and some of the other large operations in that vicinity. Remember that you can Yield return StartCoroutine("Somefunction") to yield until the end of a function, and yield return 0 to skip a frame. But I would just try yield return new WaitForSeconds(1) after a few of the lines, and see if that fixes it -- then start removing them. Let us know if that fixes it!

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
avatar image
0

Answer by wonker21 · Sep 08, 2013 at 09:35 AM

Hello thanks for answer but it doesnt work for me (or i'm doing it wrong). Here is code the code:

     IEnumerator TakePhoto()
     {   
         webCamTexture.Pause();
         yield return new WaitForSeconds(1.0f);
         
         Texture2D takenPhoto = new Texture2D((int)photoSize.x, (int)photoSize.y, TextureFormat.ARGB32, false);
         yield return new WaitForSeconds(1.0f);
         
         Color[] texData = webCamTexture.GetPixels();
         yield return new WaitForSeconds(1.0f);
         
         takenPhoto.SetPixels(texData);
         yield return new WaitForSeconds(1.0f);
         
         takenPhoto.Apply();
         yield return new WaitForSeconds(1.0f);
         
         byte[] photoData = takenPhoto.EncodeToPNG();
         yield return new WaitForSeconds(1.0f);
         
         Destroy(takenPhoto);
         yield return new WaitForSeconds(2.0f);
         
         File.WriteAllBytes(Application.dataPath + "/AvatarPhoto.png", photoData);
         yield return new WaitForSeconds(2.0f);
         
         Debug.Log("Done");
         yield return 0;
     }


And Here is the Photo: alt text


avatarphoto.png (290.1 kB)
Comment
Add comment · Show 1 · 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 wonker21 · Sep 08, 2013 at 10:04 AM 0
Share

Btw i'm making 512x512 photos. $$anonymous$$aybe dimensions are bad.. :( I'm lost

avatar image
0

Answer by dhurstdev · Jan 17, 2017 at 09:12 AM

Thx @ImranZahid!, I had a similar issue, but wasn't using the correct size, totally missed it here: http://answers.unity3d.com/questions/337530/how-to-save-a-snapshot-of-the-webcamtexture.html , though you both helped me finish this up... @wonker21 I would suggest removing the TextureFormat from your Texture2D declaration, and/or us TextureFormat.RGBA32, but I'm feeling like ImranZahid is right for your case too, where does photoSize come from? Try Grabbing the meshRenderer, material, mainTexture x & y:
MeshRenderer m = gameObject.GetComponent(); m.material.mainTexture.width & .height. GL

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

18 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 avatar image

Related Questions

Rendering an HD PNG off-screen? 0 Answers

Saving a Texture2D as a PNG 3 Answers

PNG/Sprite Creation Works on PC/Mac But Not On Android 0 Answers

video media encoder addframe texture format 5 expected to be 4 1 Answer

How do I set pixels with WebCamTexture.GetPixels 1 Answer


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