C# ZXing条码扫描器不再工作

C# ZXing条码扫描器不再工作,c#,windows-phone-8,zxing,C#,Windows Phone 8,Zxing,我使用ZXing.NET为Windows Phone 8创建了一个C#条形码扫描仪。它工作正常,但从今天早上开始,它显示错误无法初始化相机。我没有更改页面内的代码。有人熟悉这个问题或者知道解决方法吗 C#: 使用系统; 使用System.Collections.Generic; 使用System.Linq; Net系统; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Navigation; 使用Microsoft.Ph

我使用
ZXing.NET
为Windows Phone 8创建了一个C#条形码扫描仪。它工作正常,但从今天早上开始,它显示错误
无法初始化相机
。我没有更改页面内的代码。有人熟悉这个问题或者知道解决方法吗

C#:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用微软设备;
使用系统线程;
使用System.Windows.Media.Imaging;
使用ZXing;
使用System.Windows.Threading;
使用ZXing.QrCode;
使用System.Xml.Linq;
名称空间PhoneApp2
{
公共部分类扫描:PhoneApplicationPage
{
私人照相/摄像机;
专用IBarcodeReader\u条形码阅读器;
私人调度员扫描计时器;
私有可写位图_previewBuffer;
公共扫描()
{
初始化组件();
}
受保护的覆盖无效OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//初始化摄影机对象
_phoneCamera=新的光电摄像机();
_phoneCamera.Initialized+=cam_Initialized;
CameraButtons.ShutterKeyHalfPressed+=CameraButtons\u ShutterKeyHalfPressed;
//在UI中显示摄影机提要
viewfinderBrush.SetSource(_phoneCamera);
//该计时器将用于每250ms扫描一次摄像头缓冲区,并扫描任何条形码
_scanTimer=新的调度程序();
_scanTimer.Interval=TimeSpan.From毫秒(250);
_scanTimer.Tick+=(o,arg)=>ScanForBarcode();
基地。导航到(e);
}
void CameraButtons\u ShutterKeyHalf Pressed(对象发送器,事件参数e)
{
_phoneCamera.Focus();
}
受保护的覆盖无效OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
//我们正在离开此页面,将不会扫描任何条形码
_scanTimer.Stop();
如果(_phoneCamera!=null)
{
//清理
_phoneCamera.Dispose();
_phoneCamera.Initialized-=cam_Initialized;
CameraButtons.ShutterKeyHalfPressed-=CameraButtons\u ShutterKeyHalfPressed;
}
}
已初始化无效cam_(对象发送器,Microsoft.Devices.CameraOperationCompletedEventArgs e)
{
尝试
{
如果(如成功)
{
this.Dispatcher.BeginInvoke(委托()
{
_phoneCamera.FlashMode=FlashMode.Off;
_previewBuffer=新的WriteableBitmap((int)\u phoneCamera.PreviewResolution.Width,(int)\u phoneCamera.PreviewResolution.Height);
_条形码阅读器=新条形码阅读器();
//默认情况下,条形码阅读器将扫描所有支持的条形码类型
//如果我们想限制应用程序可以读取的条形码类型,
//我们可以通过将每种格式添加到此列表对象来实现
//var supportedBarcodeFormats=新列表();
//supportedBarcodeFormats.Add(BarcodeFormat.QR_代码);
//supportedBarcodeFormats.Add(BarcodeFormat.DATA_矩阵);
//_bcReader.PossibleFormats=受支持的条形码格式;
var supportedBarcodeFormats=新列表();
supportedBarcodeFormats.Add(BarcodeFormat.EAN_8);
supportedBarcodeFormats.Add(BarcodeFormat.EAN_13);
supportedBarcodeFormats.Add(BarcodeFormat.UPC_A);
supportedBarcodeFormats.Add(BarcodeFormat.UPC_E);
supportedBarcodeFormats.Add(BarcodeFormat.UPC\u EAN\u扩展名);
_barcodeReader.PossibleFormats=支持的条形码格式;
_barcodeReader.TryHarder=true;
_条形码阅读器.ResultFound+=\u bcReader\u ResultFound;
_scanTimer.Start();
});
}
其他的
{
Dispatcher.BeginInvoke(()=>
{
MessageBox.Show(“无法初始化摄像头”);
});
}
}
catch(异常myExc)
{
Console.WriteLine(myExc.Message);
}
}
无效_bcReader_ResultFound(结果对象)
{
//如果发现新条形码,则振动设备并在UI中显示条形码详细信息
如果(!obj.Text.Equals(tbBarcodeData.Text))
{
VibrationController.Default.Start(TimeSpan.From毫秒(100));
tbBarcodeType.Text=obj.BarcodeFormat.ToString();
tbBarcodeData.Text=obj.Text;
}
}
专用void ScanForBarcode()
{
如果(_phoneCamera.IsFocusSupported==true)
{
//在捕获未进行时进行聚焦。
尝试
{
_phoneCamera.Focus();
}
捕获(异常focusError)
{
//捕获正在进行时无法聚焦。
this.Dispatcher.BeginInvoke(委托()
{
txtDebug.Text=focuseror.Message;
});
}
}
//抓拍相机快照
_GetPreviewBufferArgb32(_previewBuffer.Pixels);
_previewBuffer.Invalidate();
//扫描捕获的快照
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Devices;
using System.Threading;
using System.Windows.Media.Imaging;
using ZXing;
using System.Windows.Threading;
using ZXing.QrCode;
using System.Xml.Linq;

namespace PhoneApp2
{
    public partial class Scan : PhoneApplicationPage
    {
        private PhotoCamera _phoneCamera;
        private IBarcodeReader _barcodeReader;
        private DispatcherTimer _scanTimer;
        private WriteableBitmap _previewBuffer;

        public Scan()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Initialize the camera object
            _phoneCamera = new PhotoCamera();
            _phoneCamera.Initialized += cam_Initialized;

            CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;

            //Display the camera feed in the UI
            viewfinderBrush.SetSource(_phoneCamera);


            // This timer will be used to scan the camera buffer every 250ms and scan for any barcodes
            _scanTimer = new DispatcherTimer();
            _scanTimer.Interval = TimeSpan.FromMilliseconds(250);
            _scanTimer.Tick += (o, arg) => ScanForBarcode();

            base.OnNavigatedTo(e);
        }

        void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e)
        {
            _phoneCamera.Focus();
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            //we're navigating away from this page, we won't be scanning any barcodes
            _scanTimer.Stop();

            if (_phoneCamera != null)
            {
                // Cleanup
                _phoneCamera.Dispose();
                _phoneCamera.Initialized -= cam_Initialized;
                CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;
            }
        }

        void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
        {
            try
            {
                if (e.Succeeded)
                {
                    this.Dispatcher.BeginInvoke(delegate()
                    {
                        _phoneCamera.FlashMode = FlashMode.Off;
                        _previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height);

                        _barcodeReader = new BarcodeReader();

                        // By default, BarcodeReader will scan every supported barcode type
                        // If we want to limit the type of barcodes our app can read, 
                        // we can do it by adding each format to this list object

                        //var supportedBarcodeFormats = new List<BarcodeFormat>();
                        //supportedBarcodeFormats.Add(BarcodeFormat.QR_CODE);
                        //supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX);
                        //_bcReader.PossibleFormats = supportedBarcodeFormats;

                        var supportedBarcodeFormats = new List<BarcodeFormat>();
                        supportedBarcodeFormats.Add(BarcodeFormat.EAN_8);
                        supportedBarcodeFormats.Add(BarcodeFormat.EAN_13);
                        supportedBarcodeFormats.Add(BarcodeFormat.UPC_A);
                        supportedBarcodeFormats.Add(BarcodeFormat.UPC_E);
                        supportedBarcodeFormats.Add(BarcodeFormat.UPC_EAN_EXTENSION);
                        _barcodeReader.PossibleFormats = supportedBarcodeFormats;

                        _barcodeReader.TryHarder = true;

                        _barcodeReader.ResultFound += _bcReader_ResultFound;
                        _scanTimer.Start();
                    });
                }
                else
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("Unable to initialize the camera");
                    });
                }
            }
            catch (Exception myExc)
            {
                Console.WriteLine(myExc.Message);
            }
        }

        void _bcReader_ResultFound(Result obj)
        {
            // If a new barcode is found, vibrate the device and display the barcode details in the UI
            if (!obj.Text.Equals(tbBarcodeData.Text))
            {
                VibrateController.Default.Start(TimeSpan.FromMilliseconds(100));
                tbBarcodeType.Text = obj.BarcodeFormat.ToString();
                tbBarcodeData.Text = obj.Text;
            }
        }

        private void ScanForBarcode()
        {
            if (_phoneCamera.IsFocusSupported == true)
            {
                //Focus when a capture is not in progress.
                try
                {
                    _phoneCamera.Focus();
                }
                catch (Exception focusError)
                {
                    // Cannot focus when a capture is in progress.
                    this.Dispatcher.BeginInvoke(delegate()
                    {
                        txtDebug.Text = focusError.Message;
                    });
                }
            }


            //grab a camera snapshot
            _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
            _previewBuffer.Invalidate();

            //scan the captured snapshot for barcodes
            //if a barcode is found, the ResultFound event will fire
            _barcodeReader.Decode(_previewBuffer);

        }

    }
}
<phone:PhoneApplicationPage
    x:Class="PhoneApp2.Scan"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>

        <Canvas x:Name="viewfinderCanvas">

            <!--Camera viewfinder -->
            <Canvas.Background>
                <VideoBrush x:Name="viewfinderBrush">
                    <VideoBrush.RelativeTransform>
                        <CompositeTransform
                            x:Name="viewfinderTransform"
                            CenterX="0.5"
                            CenterY="0.5"
                            Rotation="90"/>
                    </VideoBrush.RelativeTransform>
                </VideoBrush>
            </Canvas.Background>
            <TextBlock Height="40" HorizontalAlignment="Left" x:Name="txtDebug" VerticalAlignment="Top" Width="462" FontSize="24" FontWeight="ExtraBold" Canvas.Left="10" Canvas.Top="618" />
            <Path Data="M443.885,209.007 L248.921,209.007" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="5" Canvas.Left="26" Canvas.Top="201.36" Width="143.77"/>
            <Path Data="M443.885,209.007 L248.921,209.007" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="5" Canvas.Left="310.23" Canvas.Top="201.36" Width="143.77"/>
            <Path Data="M443.885,209.007 L248.921,209.007" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="5" Canvas.Left="310.23" Canvas.Top="493.36" Width="143.77"/>
            <Path Data="M443.885,209.007 L248.921,209.007" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="5" Canvas.Left="26" Canvas.Top="493.36" Width="143.77"/>
            <Path Data="M387.983,239.245 L387.983,399.76" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="101.779" Canvas.Left="449" Canvas.Top="199" Width="5"/>
            <Path Data="M387.983,239.245 L387.983,399.76" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="101.779" Canvas.Left="26" Canvas.Top="399.081" Width="5"/>
            <Path Data="M387.983,239.245 L387.983,399.76" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="101.779" Canvas.Left="26" Canvas.Top="199" Width="5"/>
            <Path Data="M387.983,239.245 L387.983,399.76" Stretch="Fill" Stroke="White" StrokeThickness="5" UseLayoutRounding="False" Height="101.779" Canvas.Left="449" Canvas.Top="399.081" Width="5"/>
        </Canvas>
        <!--Used for debugging >-->
        <StackPanel Grid.Row="1" Margin="20, 0">
            <TextBlock x:Name="tbBarcodeType" FontWeight="ExtraBold" />
            <TextBlock x:Name="tbBarcodeData" FontWeight="ExtraBold" TextWrapping="Wrap" />
        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>