在Xamarin.Forms中,ZXing.Net.Mobile视图不会延伸到UWP

时间:2017-07-25 12:03:39

标签: xamarin uwp xamarin.forms zxing

我在Xamarin.Forms应用程序中使用ZXing.Net.Mobile ZXingScannerView。我使用Android API 21+和Windows Mobile 10进行测试。在Android中,相机流完全扩展到扫描仪视图的边界,但对于UWP则不然。无论是模拟器的大小还是分辨率。我还尝试使用真正的Windows手机设备,问题仍然存在。

我将ZXingScannerView放在类似于这样的内容页面中:

<ContentPage.Content>
    <StackLayout Spacing="20" Padding="15">
        ...
        <Grid
            HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand">
            <zxing:ZXingScannerView
                x:Name="zxing"
                Result="{Binding ScannerResult, Mode=TwoWay}"
                IsScanning="{Binding ScannerScanning}"
                IsVisible="{Binding ScannerVisible}"
                IsAnalyzing="{Binding ScannerAnalyzing}"
                ScanResultCommand="{Binding ScannedCommand}"/>
            <v:MainPageZXingOverlayView />
        </Grid>
        ...
    </StackLayout>
</ContentPage.Content>

此外,我注意到view .xaml将捕获元素设置为Stretch =&#34;填充&#34;

 <CaptureElement  
        Grid.Row="0"
        Grid.Column="0"
        x:Name="captureElement"  
        Stretch="Fill" />

也在view .cs

// *after* the preview is setup, set this so that the UI layout happens
// otherwise the preview gets stuck in a funny place on screen
captureElement.Stretch = Stretch.UniformToFill;

遇到相同问题或有解决方法的人?

1 个答案:

答案 0 :(得分:0)

  

在Android中,相机流完全扩展到扫描仪视图的边界,但对于UWP则不然。无论是模拟器的大小还是分辨率。我还试过一个真正的Windows手机设备,问题仍然存在。

预览分辨率是根据相机开始扫描时的相机分辨率设置的。

根据您的要求,您可以通过自定义相机分辨率将预览扩展到边界。请参考以下代码:

private async void Button_Clicked(object sender, EventArgs e)
{                
    var options = new ZXing.Mobile.MobileBarcodeScanningOptions
    {
        CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
    };
    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
    scanner.AutoFocus();
    var result = await scanner.Scan(options);
    if (result != null)
        System.Diagnostics.Debug.WriteLine("Scanned Barcode: " + result.Text);
}
CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
    //Don't know if this will ever be null or empty
    if (availableResolutions == null || availableResolutions.Count < 1)
        return new CameraResolution() { Width = 800, Height = 600 };

    //Debugging revealed that the last element in the list
    //expresses the highest resolution. This could probably be more thorough.
    return availableResolutions[availableResolutions.Count - 1];
}

enter image description here

<强>更新

  

请注意,我使用的是ZXingScannerView本身,而不是内置的包装器。我已将我的问题更新为更清楚。

如果您使用ZXingScannerView,则还可以修改后面代码中的ZXingScannerView选项。

public MainPage()
{
    InitializeComponent();
    var options = new ZXing.Mobile.MobileBarcodeScanningOptions
    {
        CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
    };
    zxing.Options = options;
}
CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
    //Don't know if this will ever be null or empty
    if (availableResolutions == null || availableResolutions.Count < 1)
        return new CameraResolution() { Width = 800, Height = 600 };

    //Debugging revealed that the last element in the list
    //expresses the highest resolution. This could probably be more thorough.
    return availableResolutions[availableResolutions.Count - 1];
}