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;
}
}
このコードは、ダウンロードしたデモから 2 か所だけ変更されています。
1) 前面カメラしかないため、VideoDeviceId を [1] から [0] に変更しました。
2) もう 1 つの変更点は、新しい 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:
繰り返しますが、これら 2 つのマイナーな変更を除いて、これは私のコードではありません。
問題は何でしょうか?