Black line at the bottom of the WebCamTexture image

When receiving video input from a webcam via WebCamTexture, the bottom line of the returned image is completely black (RGB = 0,0,0). I tried several different webcams and got the same result with all of them. I am getting the correct image when using the Windows 10 Camera app and also when getting a webcam in processing or Java.

A black line (always 1 pixel high and wide, like an image) appears when displaying a video on a canvas, saving a snapshot to disk, and also when viewing data directly using GetPixels32 ().

Here is the black line at the bottom of the image:

enter image description here

I have verified that the returned image is the correct size, i.e. the black line is not an extra line. This is always the lowest line of the image that is black.

I have included the C # code that I am using below.

What is the reason for this black line and is there a way to avoid it?

I have searched for any information on this issue but found nothing on the internet. I am a complete newbie to Unity and would appreciate any help.

I am using Unity version 5.6.2 but had the same problem with 5.5

public class CamController : MonoBehaviour
{
    private WebCamTexture webcamTexture;
    private WebCamDevice[] devices;

    void Start()
    {
        //start webcam
        webcamTexture = new WebCamTexture();
        devices = WebCamTexture.devices;
        webcamTexture.deviceName = devices[0].name;
        webcamTexture.Play();
    }

    void Update()
    {
        //if user presses C capture cam image
        if (Input.GetKeyDown(KeyCode.C))
            captureImage();
    }

    void captureImage()
    {
        //get webcam pixels
        Color32[] camPixels;
        camPixels = webcamTexture.GetPixels32();

        //print pixel data for first and second (from bottom) lines of image to console
        for (int y = 0; y < 2; y++)
        {
            Debug.Log("Line: " + y);
            for (int x = 0; x < webcamTexture.width; x++)
            {
                Debug.Log(x + " - " + camPixels[y * webcamTexture.width + x]);
            }
        }

        //save webcam image as png
        Texture2D brightBGTexture = new Texture2D(webcamTexture.width, webcamTexture.height);
        brightBGTexture.SetPixels32(camPixels, 0);
        brightBGTexture.Apply();
        byte[] pngBytes = brightBGTexture.EncodeToPNG();
        File.WriteAllBytes(Application.dataPath + "/../camImage.png", pngBytes);
    }
}

      

+3


source to share


1 answer


After the call, SetPixels32

you must call Texture2D.Apply

to apply the changes to Texture2D

.

You must before encoding Texture2D

to png.

//save webcam image as png
Texture2D brightBGTexture = new Texture2D(webcamTexture.width, webcamTexture.height);
brightBGTexture.SetPixels32(camPixels, 0);
brightBGTexture.Apply();
byte[] pngBytes = brightBGTexture.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../camImage.png", pngBytes);

      



EDIT

Even when called, the Texture2D.Apply()

problem still exists. This is a bug with the API WebCamTexture

and you must file a bug report through the editor.

+1


source







All Articles