gpt4 book ai didi

c# - 根据设备方向转动相机图片 (Xamarin.iOS)

转载 作者:行者123 更新时间:2023-11-28 21:03:40 37 4
gpt4 key购买 nike

我想制作一个 Xamarin.iOS 应用程序,我可以在其中像相机一样拍摄照片...我的应用程序仅支持纵向。

如果我拍摄照片时相机是风景,我想将拍摄的照片变成肖像。

有人知道我该怎么做吗?

代码

    public async void CapturePhoto()
{

var videoConnection = stillImageOutput.ConnectionFromMediaType(AVMediaType.Video);
var sampleBuffer = await stillImageOutput.CaptureStillImageTaskAsync(videoConnection);

var jpegImageAsBytes = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer).ToArray();

string base64StringImage = Convert.ToBase64String(jpegImageAsBytes);

FaceRecognition faceRecognition = new FaceRecognition();
int result = faceRecognition.SendPhoto(base64StringImage);
}

最佳答案

试试这个:

var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation;

if (currentOrientation == UIInterfaceOrientation.Portrait)
{
videoConnection.VideoOrientation = AVCaptureVideoOrientation.Portrait;
}
else if (currentOrientation == UIInterfaceOrientation.LandscapeRight)
{
videoConnection.VideoOrientation = AVCaptureVideoOrientation.LandscapeRight;
}
//xxx

更新:

如果应用仅支持方向或者您锁定了屏幕,还有另一种检测设备方向的旧方法。 Core Motion

public void LockOrientation()
{
CMMotionManager CMManager = new CMMotionManager();
CMManager.DeviceMotionUpdateInterval = 0.2f;
CMManager.StartDeviceMotionUpdates(NSOperationQueue.MainQueue, (motion, error) => {
if (Math.Abs(motion.Gravity.X) > Math.Abs(motion.Gravity.Y))
{
Console.WriteLine("Lan");
if (motion.Gravity.X > 0)
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
Console.WriteLine("Left");
}
else
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeRight), new NSString("orientation"));
Console.WriteLine("Right");
}
}
else
{
if (motion.Gravity.Y >= 0)
{
Console.WriteLine("Down");
}
else
{
Console.WriteLine("UP");
}
}

CMManager.StopDeviceMotionUpdates();
});
}

引用here

关于c# - 根据设备方向转动相机图片 (Xamarin.iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46950069/

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