Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by ndmrzk · Feb 17, 2020 at 09:14 AM · camerawebcamtexture

How to get webcamtexture to fit into potrait size when camera on and when capturing screenshot?

Hi. I want my app to be able to use the front camera to take a photo and i sort of got the first part by using webcamtexture like so :

 WebCamTexture cameraTexture;
 
 public void StartCamera()
 {
     StartCoroutine(OpenCamera());
 }
 
 IEnumerator OpenCamera()
 {
     WebCamDevice[] devices = WebCamTexture.devices;
 
     for (int i = 0; i < devices.Length; i++)
     {
         if (devices[i].isFrontFacing)
         {
             cameraTexture = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
         }
     }
 
     cameraTexture.Play();
 
     Debug.Log(cameraTexture.isPlaying);
 
     yield return new WaitForEndOfFrame();
 
     float ratio = (float)cameraTexture.width / (float)cameraTexture.height;
     fit.aspectRatio = ratio;
         
     PhotoPreview.GetComponentInChildren<RawImage>().texture = cameraTexture;
    
     PhotoPreview.SetActive(true);
 
 }

I've set the aspect ratio fitter in inspector as envelope parent and in editor the camera feed is a rectangle bigger than game view. So i manually changed the uv rect in script like :

 PhotoPreview.GetComponentInChildren<RawImage>().uvRect = new Rect(0.4f, 0f, 1f, 1f);

then to take a screenshot like this :

 cameraTexture.Stop();
 //Create new  Texture2D
 Texture2D texture = new Texture2D(Screen.width, Screen.height);
 //Read the pixels in the Rect starting at 0,0 and ending at the screen's width and height
 texture.ReadPixels(new Rect(0.4f, 0f, Screen.width, Screen.height), 0, 0, false);
 texture.Apply();
 
 PhotoPreview.GetComponentInChildren<RawImage>().uvRect = new Rect(0f, 0f, 3.5f, 1f);
 
 PhotoPreview.GetComponentInChildren<RawImage>().texture = texture;
 
 yield return new WaitForEndOfFrame();

Here i also set the uv rect manually so then in scene view it looks like rectangles next to each other.

However, in gameview eveyrthing looks nice and proper but if i were to save the screenshot the image saved is distorted. Also on my phone which is a samsung, the camera feed is rotated 90 degrees to the left.

So, am i doing things correctly to get the camera feed from front facing camera? and how do i go about fixing the rotated camera feed?

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

0 Replies

· Add your reply
  • Sort: 

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

184 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 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 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 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 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 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 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 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

How to get best resolution from phone camera WebCamTexture? 2 Answers

WebCamTexture, correct resolution and RATIO.And FPS 1 Answer

WebCamTexture deforms image 0 Answers

Could not connect pins - RenderStream() UnityEngine.WebCamTexture:Play() 0 Answers

problem accessing webcamtexture.videorotation angle on windows phone 8 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