我使用ZXing.NET
在Windows Phone 8的C#中创建了条形码扫描程序。它工作正常,但从今天早上起,它显示错误Unable to initialize camera
。我没有更改页面内的代码。有这个问题的家人还是知道解决方案?
C#:
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);
}
}
}
XAML:
<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>