【问题标题】:Windows Phone change CameraTypeWindows Phone 更改 CameraType
【发布时间】:2013-07-02 17:45:18
【问题描述】:

我正在尝试在我的应用中更改我的相机的 CameraType(FrontFacing/Primary)。

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Canvas x:Name="viewfinderCanvas" Width="720" Height="480" 
               HorizontalAlignment="Left" >

        <!--Camera viewfinder -->
        <Canvas.Background>
            <VideoBrush x:Name="viewfinderBrush" />
        </Canvas.Background>

        <!-- Brackets for Touch Focus -->
        <TextBlock 
            x:Name="focusBrackets" 
            Text="[   ]" 
            FontSize="40"
            Visibility="Collapsed"/>

    </Canvas>

    <!--Button StackPanel to the right of viewfinder>-->
    <StackPanel Grid.Column="1" >
        <Button Content="Front" Name="btCameraType" Click="changeFacing_Clicked" FontSize="26" FontWeight="ExtraBold" Height="75"/>
    </StackPanel>

    <!--Used for debugging >-->
    <TextBlock Height="40" HorizontalAlignment="Left" Margin="8,428,0,0" Name="txtDebug" VerticalAlignment="Top" Width="626" FontSize="24" FontWeight="ExtraBold" />
</Grid>

这是背后的代码:

private void changeFacing_Clicked(object sender, RoutedEventArgs e)
{
    if (cam.CameraType == CameraType.FrontFacing)
        cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
    else
        cam = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
    viewfinderBrush.Dispatcher.BeginInvoke(delegate()
    {
        viewfinderBrush.SetSource(cam);
    });
}

所以我实际上只是在用户单击按钮时更改 CameraType。问题是当用户点击按钮几次(比如 2 秒内点击 5 次)时,程序无法处理它并停止工作......关于如何避免这个问题的任何解决方案?

我也尝试过启用/禁用按钮,但我仍然可以点击按钮..

【问题讨论】:

    标签: asynchronous camera windows-phone


    【解决方案1】:

    问题是当用户点击按钮多次 (所以就像在 2 秒内 5 次),程序无法处理它并且它 停止工作

    根据我的经验,我注意到PhotoCamera 类倾向于抛出大量异常,有时是出于不明原因。

    我可能会得到一些反对意见,但我会这样做:将代码放在 try...catch 块中,如下所示:

    try
    {
        if (cam.CameraType == CameraType.FrontFacing)
            cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
        else
            cam = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
        viewfinderBrush.Dispatcher.BeginInvoke(delegate()
        {
            viewfinderBrush.SetSource(cam);
        });
    }
    catch (Exception) { }
    

    当然,在使用前置摄像头之前,你需要检查设备是否有:

    PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing)
    

    编辑
    基于 cmets,try..catch 方法是不够的。 这是一个更丑陋的解决方案,应该可以工作:

    DateTime lastChange = DateTime.MinValue;
    private void changeFacing_Clicked(object sender, RoutedEventArgs e)
    {
        TimeSpan elapsedTime = DateTime.Now - lastChange;
        if (elapsedTime.TotalMilliseconds < 2000) // If the last change occured less than 2 seconds ago, ignore it
            return;
    
        if (cam.CameraType == CameraType.FrontFacing)
            cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
        else
            cam = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
    
        viewfinderBrush.Dispatcher.BeginInvoke(delegate()
        {
            viewfinderBrush.SetSource(cam);
        });
    
        lastChange = DateTime.Now;
    }
    

    【讨论】:

    • 我也想过try-catch,但结果是一样的。多次单击按钮(更改 CameraType)将冻结应用程序。没有例外,没有“调试错误”....没有。它只是冻结/停止工作(但应用程序仍然打开),Visual Studio 也没有给出错误或看到应用程序被冻结,所以我必须通过单击“停止调试”手动停止应用程序。 --- 当我在新的 CameraType 尚未设置时单击按钮时,实际上会发生这种情况。
    • 哇...在这种情况下,一个更丑陋的解决方案是检查最后一次更改是否不是在 X 秒之前进行的。
    • 是的,那将是一个“丑陋”的解决方案...:/ ...我也尝试在finally{} 使用 System.Threading.Thread.Sleep(2500),但是CameraType 在睡眠后发生变化?...不明白。无论如何,我会尝试使用检查 X 秒的方法,或者可能只使用主摄像头,如果它不起作用......
    • @Rudi 我注意到了类似的行为,但只是附加了调试器。永远不会在最终应用程序中。尝试在设备上部署应用程序,手动启动它(不使用调试器),看看是否可以重现问题。如果不能,请不要费心寻找修复方法。
    • 我编辑了我的答案,向你展示我会怎么做,告诉我它是否有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多