0

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 つのマイナーな変更を除いて、これは私のコードではありません。

問題は何でしょうか?

4

3 に答える 3

0

ビデオ デバイスのプロパティから、WriteableBitmap の正しいサイズを直接設定できます。そして、ソースセッターを待つ必要があります。

var properties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
var videoEncodingProperties = properties as VideoEncodingProperties;
writeableBmp = new WriteableBitmap((int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height);
await writeableBmp.SetSourceAsync(await photoStorageFile.OpenReadAsync());
于 2013-07-12T16:46:59.430 に答える
0

WindowsRT デモを修正しました。今すぐうまくいくはずです。リポジトリから現在のバージョンを取得できます: https://zxingnet.svn.codeplex.com/svn/trunk Damir のソリューションを使用し、その他の小さな変更をいくつか追加しました。

于 2013-01-11T20:47:57.213 に答える
0

(3000, 2000) がウェブカメラの正確な解像度ですか? 実際のカメラの解像度がそれよりも小さい場合、上記のエラーが発生する可能性があると思います。

を作成する前に、画像の正確なサイズを決定してみてくださいWriteableBitmap

await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoStorageFile);

var stream = await photoStorageFile.OpenReadAsync()
var bmp = new BitmapImage();
bmp.SetSource(stream);
var writeableBmp = new WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(stream);
于 2013-01-11T19:40:17.620 に答える