[英]How to Avoid a Distorted Android Camera Preview With ZXing.Net.Mobile in xamarin Forms
I am Applying ZXing.Net.Mobile
for barcode scan and when I run the application I get stretched camera preview.我正在应用
ZXing.Net.Mobile
进行条码扫描,当我运行该应用程序时,我得到了拉伸的相机预览。
my code looks like...我的代码看起来像...
<Grid>
<forms:ZXingScannerView x:Name="_scanView" OnScanResult="_scanView_OnOnScanResult" IsScanning="true"
HeightRequest="300"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
/>
</Grid>
How can I avoid this?我怎样才能避免这种情况?
Update更新
I tried https://laptrinhx.com/how-to-avoid-a-distorted-android-camera-preview-with-zxing-net-mobile-4270557032/我试过https://laptrinhx.com/how-to-avoid-a-distorted-android-camera-preview-with-zxing-net-mobile-4270557032/
interface界面
public interface IZXingHelper
{
MobileBarcodeScanningOptions.CameraResolutionSelectorDelegate TestingDelegate { get; set; }
}
implementation in Android在安卓中的实现
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)});
}
}
implementation in mypage.xcml.cs class在 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;
}
there I am getting Target Invocation Error
in my page.xaml.cs class
in below line.我在下面一行的
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
};
can I get a help to fix this?我能得到帮助来解决这个问题吗?
looking to your code I guess you must add WidthRequest = "300"
.查看您的代码,我想您必须添加
WidthRequest = "300"
。
As you have just requested for the Height parameter, the height is set to "300" but you haven't mentioned for width parameter & you have set VerticleOptions="CenterAndExpand"
so it will occupy the whole width of the screen giving you a stretch effect.正如您刚刚要求的高度参数,高度设置为“300”,但您没有提到宽度参数,并且您已经设置了
VerticleOptions="CenterAndExpand"
因此它将占据屏幕的整个宽度,让您舒展影响。 You must specify WidthRequest
according to your IU appearance.您必须根据您的 IU 外观指定
WidthRequest
。
Hint - Let the barcode scanner open in full screen as it is the standard way to use barcode scanning, you can remove HeightRequest= "300"
from your code.提示 - 让条码扫描器全屏打开,因为这是使用条码扫描的标准方式,您可以从代码中删除
HeightRequest= "300"
。
Since you had used Grid .由于您使用过 Grid 。 Don't forget to set the Row and Column .
不要忘记设置Row和Column 。
For example , if you want to put the scan view in the center of the Grid .例如,如果您想将扫描视图放在网格的中心。
<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>
If you do want to set the fix Height .如果您确实想设置修复 Height 。 Use StackLayout is better .
使用 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.