title: Barcode scanner with ZXing not working anymore
body: <p>I have created a barcode scanner in C# for Windows Phone 8 using <code>ZXing.NET</code>. It worked fine but since this morning, it displays the error <code>Unable to initialize camera</code>. I haven't changed the code inside the page. Anyone familair with this problem or knows a solution?</p>
  
  <p>C#:</p>
  
  <pre><code>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) =&gt; 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&lt;BarcodeFormat&gt;();
                          //supportedBarcodeFormats.Add(BarcodeFormat.QR_CODE);
                          //supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX);
                          //_bcReader.PossibleFormats = supportedBarcodeFormats;
  
                          var supportedBarcodeFormats = new List&lt;BarcodeFormat&gt;();
                          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(() =&gt;
                      {
                          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);
  
          }
  
      }
  }
  </code></pre>
  
  <p>XAML:</p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;!--LayoutRoot is the root grid where all page content is placed--&gt;
      &lt;Grid x:Name="LayoutRoot"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="*" /&gt;
              &lt;RowDefinition Height="100" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Canvas x:Name="viewfinderCanvas"&gt;
  
              &lt;!--Camera viewfinder --&gt;
              &lt;Canvas.Background&gt;
                  &lt;VideoBrush x:Name="viewfinderBrush"&gt;
                      &lt;VideoBrush.RelativeTransform&gt;
                          &lt;CompositeTransform
                              x:Name="viewfinderTransform"
                              CenterX="0.5"
                              CenterY="0.5"
                              Rotation="90"/&gt;
                      &lt;/VideoBrush.RelativeTransform&gt;
                  &lt;/VideoBrush&gt;
              &lt;/Canvas.Background&gt;
              &lt;TextBlock Height="40" HorizontalAlignment="Left" x:Name="txtDebug" VerticalAlignment="Top" Width="462" FontSize="24" FontWeight="ExtraBold" Canvas.Left="10" Canvas.Top="618" /&gt;
              &lt;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"/&gt;
              &lt;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"/&gt;
              &lt;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"/&gt;
              &lt;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"/&gt;
              &lt;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"/&gt;
              &lt;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"/&gt;
              &lt;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"/&gt;
              &lt;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"/&gt;
          &lt;/Canvas&gt;
          &lt;!--Used for debugging &gt;--&gt;
          &lt;StackPanel Grid.Row="1" Margin="20, 0"&gt;
              &lt;TextBlock x:Name="tbBarcodeType" FontWeight="ExtraBold" /&gt;
              &lt;TextBlock x:Name="tbBarcodeData" FontWeight="ExtraBold" TextWrapping="Wrap" /&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  
  &lt;/phone:PhoneApplicationPage&gt;
  </code></pre>