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;
     }
  }

此代码仅在两个地方从下载的演示中修改。

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: 

同样,这不是我的代码,除了那两个小改动。

问题可能是什么?

4

3 回答 3

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 回答
0

我修改了 WindowsRT 演示。它现在应该适合你。您可以从存储库中获取当前版本: https ://zxingnet.svn.codeplex.com/svn/trunk 我使用了 Damir 的解决方案并添加了一些其他小的更改。

于 2013-01-11T20:47:57.213 回答
0

您可以直接从视频设备属性中设置正确的 WriteableBitmap 大小。你应该等待 Source setter。

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 回答