[英]How to Avoid a Distorted Android Camera Preview With ZXing.Net.Mobile in xamarin Forms
我正在应用ZXing.Net.Mobile
进行条码扫描,当我运行该应用程序时,我得到了拉伸的相机预览。
我的代码看起来像...
<Grid>
<forms:ZXingScannerView x:Name="_scanView" OnScanResult="_scanView_OnOnScanResult" IsScanning="true"
HeightRequest="300"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
/>
</Grid>
我怎样才能避免这种情况?
更新
界面
public interface IZXingHelper
{
MobileBarcodeScanningOptions.CameraResolutionSelectorDelegate TestingDelegate { get; set; }
}
在安卓中的实现
public class TestingResolution : IZXingHelper
{
public MobileBarcodeScanningOptions.CameraResolutionSelectorDelegate TestingDelegate { get; set; }
public CameraResolution SelectLowestResolutionMatchingDisplayAspectRatio(List<CameraResolution> availableResolutions)
{
CameraResolution result = null;
//a tolerance of 0.1 should not be recognizable for users
double aspectTolerance = 0.1;
//calculating our targetRatio
var targetRatio = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Width;
var targetHeight = DeviceDisplay.MainDisplayInfo.Height;
var minDiff = double.MaxValue;
//camera API lists all available resolutions from highest to lowest, perfect for us
//making use of this sorting, following code runs some comparisons to select the lowest resolution that matches the screen aspect ratio
//selecting the lowest makes QR detection actual faster most of the time
foreach (var r in availableResolutions)
{
//if current ratio is bigger than our tolerance, move on
//camera resolution is provided landscape ...
//if (Math.Abs(((double)r.Width / r.Height) - targetRatio) > aspectTolerance)
// continue;
//else
if (Math.Abs(r.Height - targetHeight) < minDiff)
minDiff = Math.Abs(r.Height - targetHeight);
result = r;
}
return result;
}
public TestingResolution()
{
List<CameraResolution> resolution = new List<CameraResolution>
{
new CameraResolution()
{
Height = 200,
Width = 300
}
};
TestingDelegate.Invoke(new List<CameraResolution> {SelectLowestResolutionMatchingDisplayAspectRatio(resolution)});
}
}
在 mypage.xcml.cs 类中的实现
public PartialPagexaml()
{
InitializeComponent();
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE},
CameraResolutionSelector = DependencyService.Get<IZXingHelper>().TestingDelegate
};
_scanView.Options = options;
}
我在下面一行的my page.xaml.cs class
中收到Target Invocation Error
。
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE},
CameraResolutionSelector = DependencyService.Get<IZXingHelper>().TestingDelegate
};
我能得到帮助来解决这个问题吗?
查看您的代码,我想您必须添加WidthRequest = "300"
。
正如您刚刚要求的高度参数,高度设置为“300”,但您没有提到宽度参数,并且您已经设置了VerticleOptions="CenterAndExpand"
因此它将占据屏幕的整个宽度,让您舒展影响。 您必须根据您的 IU 外观指定WidthRequest
。
提示 - 让条码扫描器全屏打开,因为这是使用条码扫描的标准方式,您可以从代码中删除HeightRequest= "300"
。
由于您使用过 Grid 。 不要忘记设置Row和Column 。
例如,如果您想将扫描视图放在网格的中心。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.33*" />
<RowDefinition Height="0.33*" />
<RowDefinition Height="0.33*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.33*" />
<ColumnDefinition Width="0.33*" />
<ColumnDefinition Width="0.33*" />
</Grid.ColumnDefinitions>
<forms:ZXingScannerView Grid.Row="1" Grid.Column="1" x:Name="_scanView" OnScanResult="_scanView_OnOnScanResult" IsScanning="true"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
/>
</Grid>
如果您确实想设置修复 Height 。 使用 StackLayout 更好。
<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<!-- Place new controls here -->
<forms:ZXingScannerView x:Name="_scanView"
OnScanResult="_scanView_OnOnScanResult"
IsScanning="true"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
HeightRequest="300" WidthRequest="400"
/>
</StackLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.