Alessio Marzoli
Alessio Marzoli

Reputation: 167

How to modify the size of a WebCamTexture?

I get the texture from my webcamtexture in Unity, but it's too high for my project. I don't need it 1280x720 , but i would decrease is size. How can i change it parameters in Unity ? Thank you. Thank you in advice.

Upvotes: 5

Views: 9827

Answers (2)

Chris
Chris

Reputation: 508

webCamTexture.requestedWidth and webCamTexture.requestedHeight returns the width/height that you originally tried to initialize the WebCamTexture with. they don't reflex the real width / height. Nor, does the webCamTexture.width/webCamTexture.height at all times. The problem is that the size of the webCamTexture is determined after some time in some instances due to the diffirent hardware and resolutions of the device camera, which is not the same aspect as the screen of the device, often.

What you CAN do is to get the pixels (webCamTexture.GetPixels()) 1 time and then after a frame you can use webCamTexture.width/.height to return the captured size of the texture.

An example of such code from the plugin CameraCaptureKit (https://www.assetstore.unity3d.com/en/#!/content/56673) which has a function to do what you describe.

// ANDREAS added this: In some cases we apparently don't get correct width and height until we have tried to read pixels
    // from the buffer.
    void TryDummySnapshot( ) {
        if(!gotAspect) {
            if( webCamTexture.width>16 ) {
                if( Application.platform == RuntimePlatform.IPhonePlayer ) {
                    if(verbose)Debug.Log("Already got width height of WebCamTexture.");
                } else { 
                    if(verbose)Debug.Log("Already got width height of WebCamTexture. - taking a snapshot no matter what.");
                    var tmpImg = new Texture2D( webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false );
                    Color32[] c = webCamTexture.GetPixels32();
                    tmpImg.SetPixels32(c);
                    tmpImg.Apply();
                    Texture2D.Destroy(tmpImg);
                }
                gotAspect = true;
            } else {
                if(verbose)Debug.Log ("Taking dummy snapshot");
                var tmpImg = new Texture2D( webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false );
                Color32[] c = webCamTexture.GetPixels32();
                tmpImg.SetPixels32(c);
                tmpImg.Apply();
                Texture2D.Destroy(tmpImg);
            }
        }
    }

Getting back on the point of your question, when I Follow the iOS code you should be able to set a smaller texture size of WebCamTexture by setting the "requestedWidth/requstedHeight" to something smaller, Unity will then( at least on iOS) try and select a camera resolution which is closest to your requsted texturesize.

That happens in CameraCapture.mm

- (NSString*)pickPresetFromWidth:(int)w height:(int)h
{
static NSString* preset[] =
{
    AVCaptureSessionPreset352x288,
    AVCaptureSessionPreset640x480,
    AVCaptureSessionPreset1280x720,
    AVCaptureSessionPreset1920x1080,
};
static int presetW[] = { 352, 640, 1280, 1920 };

//[AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];


#define countof(arr) sizeof(arr)/sizeof(arr[0])

static_assert(countof(presetW) == countof(preset), "preset and preset width arrrays have different elem count");

int ret = -1, curW = -10000;
for(int i = 0, n = countof(presetW) ; i < n ; ++i)
{
    if( ::abs(w - presetW[i]) < ::abs(w - curW) && [self.captureSession canSetSessionPreset:preset[i]] )
    {
        ret = i;
        curW = presetW[i];
    }
}

NSAssert(ret != -1, @"Cannot pick capture preset");
return ret != -1 ? preset[ret] : AVCaptureSessionPresetHigh;

#undef countof
 }

Upvotes: 4

Dover8
Dover8

Reputation: 595

You can use the requested height and requested width to adjust the size of texture you want from the camera. However you need to do this while the camera is not running, and you will get back the closest supported size by the device itself.

WebCamTexture.requestedWidth

WebCamTexture.requestedHeight

Upvotes: 1

Related Questions