我正在开发一个需要Qrcode scannig的Windows Phone 8应用程序,而我正在使用Zxing库来扫描Qr代码。
在这个应用程序中,我需要在扫描完成后返回上一页。
我正在使用以下代码,
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
_phoneCamera = new PhotoCamera();
_phoneCamera.Initialized += cam_Initialized;
_phoneCamera.AutoFocusCompleted += _phoneCamera_AutoFocusCompleted;
CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;
viewfinderBrush.SetSource(_phoneCamera);
_scanTimer = new DispatcherTimer();
_scanTimer.Interval = TimeSpan.FromMilliseconds(1500);
_scanTimer.Tick += (o, arg) => ScanForBarcode();
base.OnNavigatedTo(e);
}
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
_scanTimer.Stop();
if (cameraInit)
{
Dispatcher.BeginInvoke(() =>
{
if (_phoneCamera != null)
{
_phoneCamera.CancelFocus();
_phoneCamera.Dispose();
_phoneCamera.Initialized -= cam_Initialized;
_phoneCamera = null;
cameraInit = false;
}
});
}
}
void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
{
if (e.Succeeded)
{
cameraInit = true;
this.Dispatcher.BeginInvoke(() =>
{
_phoneCamera.FlashMode = FlashMode.Auto;
_previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height);
_barcodeReader = new BarcodeReader();
(int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height, 0, 100);
var supportedBarcodeFormats = new List<BarcodeFormat>();
supportedBarcodeFormats.Add(BarcodeFormat.QR_CODE);
supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX);
_barcodeReader.Options.PossibleFormats = supportedBarcodeFormats;
_barcodeReader.Options.TryHarder = true;
_barcodeReader.ResultFound += _bcReader_ResultFound;
_scanTimer.Start();
});
}
else
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Unable to initialize the camera");
});
}
}
void _bcReader_ResultFound(Result obj)
{
Dispatcher.BeginInvoke(() =>
{
VibrateController.Default.Start(TimeSpan.FromMilliseconds(100));
a = obj.Text;
_scanTimer.Stop();
NavigationService.GoBack(); //here I am going back to previos page
});
}
private void ScanForBarcode()
{
_phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
_previewBuffer.Invalidate();
_barcodeReader.Decode(_previewBuffer);
}
此代码工作正常,但有时在捕获qrcode时会挂起应用程序。
EDITED
我在没有调试模式的情况下运行应用程序时会出现此问题。 当应用程序无响应时,一段时间后它会崩溃,但不会给出任何错误消息。
所以请帮我解决这个问题。在此先感谢。
答案 0 :(得分:0)
我不确定,但您是否有可能尝试在UI线程上扫描QR码,从而导致挂起。尝试在不同的线程上为它做。
Windows Phone操作系统不喜欢无响应的UI线程,可能导致应用程序意外关闭。