gpt4 book ai didi

c# - 如何修改 WebCamTexture 的大小?

转载 作者:行者123 更新时间:2023-11-30 12:26:30 27 4
gpt4 key购买 nike

我从 Unity 中的网络摄像头纹理中获取纹理,但它对我的项目来说太高了。我不需要它 1280x720 ,但我会减小尺寸。我如何在 Unity 中更改它的参数?谢谢。谢谢你的建议。

最佳答案

webCamTexture.requestedWidth 和 webCamTexture.requestedHeight 返回您最初尝试初始化 WebCamTexture 的宽度/高度。他们不反射(reflect)真实的宽度/高度。 webCamTexture.width/webCamTexture.height 也不会始终如此。问题是 webCamTexture 的大小在某些情况下是在一段时间后确定的,这是由于设备摄像头的硬件和分辨率不同,这通常与设备屏幕的外观不同。

您可以做的是获取像素 (webCamTexture.GetPixels()) 1 次,然后在一帧之后您可以使用 webCamTexture.width/.height 返回捕获的纹理大小。

来自插件 CameraCaptureKit ( https://www.assetstore.unity3d.com/en/#!/content/56673 ) 的此类代码示例,它具有执行您所描述的功能。

// 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);
}
}
}

回到你的问题,当我遵循 iOS 代码时,你应该能够通过将“requestedWidth/requstedHeight”设置为更小的值来设置更小的 WebCamTexture 纹理大小,然后 Unity 将(至少在 iOS 上) ) 尝试选择最接近您要求的纹理大小的相机分辨率。

这发生在 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
}

关于c# - 如何修改 WebCamTexture 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28466221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com