Search Unity

How to save a rotated WebCamTexture to png?

Discussion in 'Editor & General Support' started by roseportalgames, Dec 13, 2018.

  1. roseportalgames

    roseportalgames

    Joined:
    Aug 9, 2017
    Posts:
    173
    I use this code to rotate the raw image texture displayed in my Unity game of the WebCamTexture:

    Code (CSharp):
    1. // change as user rotates iPhone or Android:
    2.  
    3.         int cwNeeded = webCamTexture.videoRotationAngle;
    4.         // Unity helpfully returns the _clockwise_ twist needed
    5.         // guess nobody at Unity noticed their product works in counterclockwise:
    6.         int ccwNeeded = -cwNeeded;
    7.  
    8.         // IF the image needs to be mirrored, it seems that it
    9.         // ALSO needs to be spun. Strange: but true.
    10.         if (webCamTexture.videoVerticallyMirrored) ccwNeeded += 180;
    11.  
    12.         // you'll be using a UI RawImage, so simply spin the RectTransform
    13.         rawImage.rectTransform.localEulerAngles = new Vector3(0f, 0f, ccwNeeded);
    14.  
    15.         // Do aspect ratio in a different way...
    16.         /*
    17.         float videoRatio = (float)webCamTexture.width / (float)webCamTexture.height;
    18.         AspectRatioFitter rawImageARF = rawImage.GetComponent<AspectRatioFitter>();
    19.         rawImageARF.aspectRatio = videoRatio;
    20.         */
    21.  
    22.         if (webCamTexture.videoVerticallyMirrored)
    23.             rawImage.uvRect = new Rect(1, 0, -1, 1);  // means flip on vertical axis
    24.         else
    25.             rawImage.uvRect = new Rect(0, 0, 1, 1);  // means no flip
    Then I save it with this code:

    Code (CSharp):
    1. Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, true);
    2. Color[] rpixels = result.GetPixels(0);
    3.         float incX = ((float)1 / source.width) * ((float)source.width / targetWidth);
    4.         float incY = ((float)1 / source.height) * ((float)source.height / targetHeight);
    5.         for (int px = 0; px < rpixels.Length; px++)
    6.         {
    7.             rpixels[px] = source.GetPixelBilinear(incX * ((float)px % targetWidth),
    8.                               incY * ((float)Mathf.Floor(px / targetWidth)));
    9.         }
    10.         result.SetPixels(rpixels, 0);
    11.         result.Apply();
    12.         //Encode it as a PNG.
    13.         byte[] bytes = result.EncodeToPNG();
    Now if I take a photo in portrait mode, the resulting png is rotated 90 degrees. It is not a portrait photo, but a landscape photo.
    How can I also get the Texture2D that I EncodeToPng to properly use the 90 degrees rotation of landscape vs portrait photography?
    Thanks. :)
     
  2. NDAP

    NDAP

    Joined:
    Mar 23, 2017
    Posts:
    2
    hi did you manage to fix this?
     
  3. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    having the same problem,, any update?
     
  4. shreyasjagannath08

    shreyasjagannath08

    Joined:
    May 20, 2019
    Posts:
    1
    same. Need a solution