我正在尝试使用 ZXING 演示“WindowsRTDemo”,但无法正常工作。演示是从这个站点下载的。
这是我正在运行的代码:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
try
{
var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
if (cameras.Count < 1)
{
ScanResult.Text = "No camera found";
return;
}
var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[0].Id }; // 0 => front, 1 => back
await _mediaCapture.InitializeAsync(settings);
VideoCapture.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
while (_result == null)
{
var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("scan.jpg", CreationCollisionOption.GenerateUniqueName);
await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoStorageFile);
var writeableBmp = new WriteableBitmap(3000, 2000);
writeableBmp.SetSource(await photoStorageFile.OpenReadAsync());
var barcodeReader = new BarcodeReader
{
TryHarder = true,
AutoRotate = true
};
_result = barcodeReader.Decode(writeableBmp);
if (_result != null)
{
CaptureImage.Source = writeableBmp;
}
await photoStorageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
await _mediaCapture.StopPreviewAsync();
VideoCapture.Visibility = Visibility.Collapsed;
CaptureImage.Visibility = Visibility.Visible;
ScanResult.Text = _result.Text;
}
catch (Exception ex)
{
ScanResult.Text = ex.Message;
}
}
此代码仅在两个地方从下载的演示中修改。
1) 我将 VideoDeviceId 从 [1] 更改为 [0] 因为我只有一个前置摄像头。
2) 另一个变化是创建新的 WriteableBitmap。下载的演示将大小设置为 (640, 360)。当我尝试使用它时,我得到了一个关于索引超出数组边界的异常。我认为这是因为我的网络摄像头具有更高的分辨率,所以我增加了尺寸以匹配(并且我已经尝试超过)我的网络摄像头的分辨率。现在,当我运行它时,在调用barcodeReader.Decode(writeableBmp) 时出现以下异常:
System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)
at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer source, UInt32 sourceIndex, Byte[] destination, Int32 destinationIndex, Int32 count)
at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source, UInt32 sourceIndex, Int32 count)
at ZXing.BitmapLuminanceSource..ctor(WriteableBitmap writeableBitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.Build\Source\lib\BitmapLuminanceSource.Silverlight.cs:line 50
at ZXing.BarcodeReader.<.cctor>b__4(WriteableBitmap bitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.Build\Source\lib\BarcodeReader.cs:line 60
at ZXing.BarcodeReaderGeneric`1.Decode(T barcodeBitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.Build\Source\lib\BarcodeReaderGeneric.cs:line 376
at WindowsRT.MainPage.<OnNavigatedTo>d__2.MoveNext() in c:\Users\Joseph Martinez\Documents\Visual Studio 2012\Projects\WindowsRTBarcodeDemo\MainPage.xaml.cs:line 53
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<.cctor>b__3(Object state)
at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
at System.Threading.WinRTSynchronizationContext.Invoker.InvokeInContext(Object thisObj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.WinRTSynchronizationContext.Invoker.Invoke()
InnerException:
同样,这不是我的代码,除了那两个小改动。
问题可能是什么?