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 5argon · Mar 29, 2015 at 07:19 PM · androidcamerauiwebcamtexturephone

How to get best resolution from phone camera WebCamTexture?

I'm writing a camera application in Unity with uGUI. I use the WebCamTexture with uGUI's RawImage like so

 WebCamTexture  webcam = new WebCamTexture();
 webcam.Play();
 rawImage.texture = webcam;
 rawImage.SetNativeSize();

Now to get its data I used

 public byte[] GetPNG()
 {
     Texture2D tex = new Texture2D(webcam.width,webcam.height);
     tex.SetPixels(webcam.GetPixels());
     tex.Apply();
     return tex.EncodeToPNG();
 }


But I noticed the resolution is very very low. Like 640x480. Even if I set request size on

 WebCamTexture  webcam = new WebCamTexture(10000,10000,30);

I debugged webcam width and height and it is still like 1920x1080 while my Android phone is capable for taking more megapixels. And not to mention it is VERY sluggish when I requested 10000 10000 (and got back only 1920x1080) maybe this is a bug? When I requested precisely 1920x1080 it is not laggy. (but still bad resolution)

This is camera app developed with Unity. Very low resolution.

alt text

This is the native android camera app. Notice that the preview image is much more sharper, which is reflected in quality of resulting file too.

alt text

Comment
Add comment · Show 3
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 sstadelman · Jan 14, 2016 at 10:37 AM 0
Share

Hi, were you able to make any progress on this issue?

avatar image 5argon sstadelman · Jan 14, 2016 at 10:53 AM 0
Share

Unfortunately no, and I already stopped development of this app so I didn't inspect further...

avatar image Fattie · Feb 29, 2016 at 12:31 AM 0
Share

@sargon solution below - sorry you gave up on it !

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fattie · Feb 29, 2016 at 12:31 AM

Regarding this ridiculous problem caused by unity ...

answer http://answers.unity3d.com/answers/1148424/view.html

Comment
Add comment · Show 2 · 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 HamFar · Jul 14, 2017 at 07:52 AM 0
Share

@Fattie Hi there, Do you know if I always need both .texture and .mainTexture assignments when working with a WebCamTexture? WebCamTexture webcamTexture = new WebCamTexture(); rawimage.texture = webcamTexture; <===this rawimage.material.mainTexture = webcamTexture; <===and this webcamTexture.Play();

avatar image costingrigore6 · Oct 19, 2020 at 04:43 PM 0
Share

Hello @Fattie I am still getting the same issue where the image will be written to my phone to a resolution of 640x480. I have tried the script that you provided but it still doesn't seem to solve my problem.

 public Texture2D TakePhoto()  // Start this Coroutine on some button click
 {
     rotationVector.z = -activeCameraTexture.videoRotationAngle;
     image.rectTransform.localEulerAngles = rotationVector;

     // Set AspectRatioFitter's ratio
     float videoRatio =
         (float)activeCameraTexture.width / (float)activeCameraTexture.height;
     imageFitter.aspectRatio = videoRatio;

     // Unflip if vertically flipped
     image.uvRect =
         activeCameraTexture.videoVertically$$anonymous$$irrored ? fixedRect : defaultRect;

     imageParent.localScale =
         activeCameraDevice.isFrontFacing ? fixedScale : defaultScale;
     string name = string.Format("{0}_Capture{1}.png", Application.productName, System.DateTime.Now.ToString("yyyy-$$anonymous$$$$anonymous$$-dd_HH-mm-ss"));
     // Take photo and save it to persistent data folder
     Texture2D photo = new Texture2D(activeCameraTexture.width, activeCameraTexture.height);
     photo.SetPixels(activeCameraTexture.GetPixels());
     photo.Apply();
     byte[] bytes = photo.EncodeToPNG();
     //File.WriteAllBytes(Application.persistentDataPath, bytes);
     NativeGallery.SaveImageToGallery(bytes, Application.productName + " Captures", name);
     return photo;
     
 }

 // Set the device camera to use and start it
 public void SetActiveCamera(WebCamTexture cameraToUse)
 {
     if (activeCameraTexture != null)
     {
         activeCameraTexture.Stop();
     }

     activeCameraTexture = cameraToUse;
     activeCameraDevice = WebCamTexture.devices.FirstOrDefault(device =>
         device.name == cameraToUse.deviceName);

     image.texture = activeCameraTexture;
     image.material.mainTexture = activeCameraTexture;

     activeCameraTexture.Play();
 }

 void Update()
 {
     // Skip making adjustment for incorrect camera data
     if (activeCameraTexture.width < 100)
     {
         Debug.Log("Still waiting another frame for correct info...");
         return;
     }

     rotationVector.z = -activeCameraTexture.videoRotationAngle;
     image.rectTransform.localEulerAngles = rotationVector;

     float videoRatio =
         (float)activeCameraTexture.width / (float)activeCameraTexture.height;
     imageFitter.aspectRatio = videoRatio;

     image.uvRect =
         activeCameraTexture.videoVertically$$anonymous$$irrored ? fixedRect : defaultRect;

     // $$anonymous$$irror front-facing camera's image horizontally to look more natural
     imageParent.localScale =
         activeCameraDevice.isFrontFacing ? fixedScale : defaultScale;
 }

}

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

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

Related Questions

Unwanted delay with WebCamTexture on Phone 0 Answers

Possible to use an external camera (USB) while running on Android? 1 Answer

WebCamTexture crashes on android 0 Answers

problem accessing webcamtexture.videorotation angle on windows phone 8 0 Answers

webcam texture device not identified 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