我当前正在使用BarcodeScanner
中显示的自定义预览的ContentDialog
。
我确实使用了Microsoft文档中提供的代码,并且一切正常,但是只有一次。
这是我的整个BarcodeScanner
服务类别:
public class BarcodeScannerUtil : IBarcodeScanner
{
private static Windows.Devices.PointOfService.BarcodeScanner _scanner = null;
private static ClaimedBarcodeScanner _claimedBarcodeScanner = null;
private static Action<string> _callback;
MediaCapture mediaCapture;
bool isPreviewing;
DisplayRequest displayRequest = new DisplayRequest();
CameraPreviewDialog preview = new CameraPreviewDialog();
public async Task ClaimScannerAsync()
{
string selector = Windows.Devices.PointOfService.BarcodeScanner.GetDeviceSelector();
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);
if (_scanner == null)
_scanner = await Windows.Devices.PointOfService.BarcodeScanner.FromIdAsync(deviceCollection[0].Id);
if (_scanner != null)
{
if (_claimedBarcodeScanner == null)
_claimedBarcodeScanner = await _scanner.ClaimScannerAsync();
if (_claimedBarcodeScanner != null)
{
_claimedBarcodeScanner.DataReceived += ClaimedBarcodeScanner_DataReceivedAsync;
_claimedBarcodeScanner.ReleaseDeviceRequested += ClaimedBarcodeScanner_ReleaseDeviceRequested;
_claimedBarcodeScanner.IsDecodeDataEnabled = true;
_claimedBarcodeScanner.IsDisabledOnDataReceived = true;
await _claimedBarcodeScanner.EnableAsync();
//await _claimedBarcodeScanner.ShowVideoPreviewAsync();
await _claimedBarcodeScanner.StartSoftwareTriggerAsync();
await StartPreviewAsync();
Debug.WriteLine("Barcode Scanner claimed");
}
}
}
private MediaCaptureInitializationSettings InitCaptureSettings()
{
var _captureInitSettings = new MediaCaptureInitializationSettings();
_captureInitSettings.VideoDeviceId = _scanner.VideoDeviceId;
_captureInitSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
_captureInitSettings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
return _captureInitSettings;
}
private async Task StartPreviewAsync()
{
try
{
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(InitCaptureSettings());
displayRequest.RequestActive();
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
}
catch (UnauthorizedAccessException)
{
await new ErrorDialog("Impossible d'acceder à la caméra, veuillez vérifier les permissions de l'application.").ShowAsync();
}
try
{
preview.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
isPreviewing = true;
ContentDialogResult resPreview = await preview.ShowAsync();
//clic sur le bouton annuler
if (resPreview == ContentDialogResult.Secondary)
{
await CleanupCameraAsync();
await _claimedBarcodeScanner.StopSoftwareTriggerAsync();
await _claimedBarcodeScanner.DisableAsync();
}
}
catch (System.IO.FileLoadException)
{
mediaCapture.CaptureDeviceExclusiveControlStatusChanged += _mediaCapture_CaptureDeviceExclusiveControlStatusChanged;
}
}
private async Task CleanupCameraAsync()
{
if (mediaCapture != null)
{
if (isPreviewing)
{
await mediaCapture.StopPreviewAsync();
}
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
preview.Source = null;
if (displayRequest != null)
{
displayRequest.RequestRelease();
}
mediaCapture.Dispose();
mediaCapture = null;
});
}
}
public void Subscribe(Action<string> callback)
{
// it makes sense to have only one foreground barcode reader client at a time
_callback = callback;
}
/// <summary>
/// Retire une action callback du BarcodeScanner
/// </summary>
public void Unsubscribe()
{
_callback = null;
}
private async void ClaimedBarcodeScanner_DataReceivedAsync(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs args)
{
await CleanupCameraAsync();
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
preview.Hide();
});
if (_callback == null)
return;
var barcode = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, args.Report.ScanDataLabel);
_callback(barcode);
}
private void ClaimedBarcodeScanner_ReleaseDeviceRequested(object sender, ClaimedBarcodeScanner e)
{
// always retain the device
e.RetainDevice();
}
private async void _mediaCapture_CaptureDeviceExclusiveControlStatusChanged(MediaCapture sender, MediaCaptureDeviceExclusiveControlStatusChangedEventArgs args)
{
if (args.Status == MediaCaptureDeviceExclusiveControlStatus.SharedReadOnlyAvailable)
{
await new ErrorDialog("Impossible d'acceder à la caméra car elle est utilisée par une autre application.").ShowAsync();
}
else if (args.Status == MediaCaptureDeviceExclusiveControlStatus.ExclusiveControlAvailable && !isPreviewing)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await StartPreviewAsync();
});
}
}
}
当我显示预览时,我正在使用IsDisabledOnDataReceived = true;
,但是当我第二次重新打开条形码扫描器时,在{{ 1}}
这真的很奇怪,因为当我单击显示预览的System.Exception : 'The request is invalid in the current state. Started'
的“取消”按钮时,它做的是完全相同的事情,之后就没有问题了。
我已经搜索了一个小时,不知道它可能来自什么。
答案 0 :(得分:0)
最后我自己找到了答案:
我将CleanupCameraAsync()
事件中的OnReceived
移动到了内容对话框结果中
ContentDialogResult resPreview = await preview.ShowAsync();
//clic sur le bouton annuler
if (resPreview == ContentDialogResult.Secondary)
{
await _claimedBarcodeScanner.StopSoftwareTriggerAsync();
await _claimedBarcodeScanner.DisableAsync();
await CleanupCameraAsync();
}
else
{
await CleanupCameraAsync();
}
因此,当我使用按钮关闭对话框时,它将起作用,并且如果通过其他任何方式关闭,CleanupCameraAsync()
仍会触发。
此外,我在CleanupCameraAsync()
中添加了几行以重置BarcodeScanner
的所有组件
private async Task CleanupCameraAsync()
{
if (mediaCapture != null)
{
if (isPreviewing)
{
await mediaCapture.StopPreviewAsync();
}
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
preview.Source = null;
if (displayRequest != null)
{
displayRequest.RequestRelease();
}
mediaCapture.Dispose();
mediaCapture = null;
/*******added these lines*********/
displayRequest = new DisplayRequest();
preview = new CameraPreviewDialog();
_scanner = null;
_claimedBarcodeScanner = null;
});
}
}
这不是很干净,但是每次我叫BarcodeScanner
时,它都会具有默认行为。
如果您有更好的解决方案,请告诉我。