C# 如何在windows 8中从照相机实例读取条形码读取器?

C# 如何在windows 8中从照相机实例读取条形码读取器?,c#,.net,windows-8,windows-8.1,C#,.net,Windows 8,Windows 8.1,如何在windows 8应用程序中使用XAML C从相机读取条形码和二维码? 我试着使用CaptureElement,并从中引用它,实现一些类似这样的解码逻辑 问题是,我无法获得如何扫描条形码的特定对象,使其解码并给出正确的结果 XAML C 对于Windows Phone,只需使用即可 BarcodeScanner.Xaml.cs 如果您计划使用WinRT应用程序,您应该执行以下操作 抓拍图片并尝试识别,直到找到条形码。没有比这更好的办法了。实际上,您可以这样改进代码 var llPhoto

如何在windows 8应用程序中使用XAML C从相机读取条形码和二维码? 我试着使用CaptureElement,并从中引用它,实现一些类似这样的解码逻辑

问题是,我无法获得如何扫描条形码的特定对象,使其解码并给出正确的结果

XAML

C


对于Windows Phone,只需使用即可

BarcodeScanner.Xaml.cs 如果您计划使用WinRT应用程序,您应该执行以下操作

抓拍图片并尝试识别,直到找到条形码。没有比这更好的办法了。实际上,您可以这样改进代码

var llPhoto = await mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateJpeg());
var photo = await llPhoto.CaptureAsync();

WriteableBitmap wbmp = new WriteableBitmap((int)photo.Frame.Width, (int)photo.Frame.Height);
wbmp.SetSource(photo.Frame.AsStream().AsRandomAccessStream());

result = ScanBitmap(wbmp);
注意:如果您要在Windows phone中尝试第二种解决方案,它将太慢,例如在2-3秒内捕捉和处理每个图像

private async void Page_Loaded_1(object sender, RoutedEventArgs e)
    {
        var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
        if (cameras.Count < 1)
        {
            return;
        }
        MediaCaptureInitializationSettings settings;
        if (cameras.Count == 1)
        {
            settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[0].Id }; // 0 => front, 1 => back
        }
        else
        {
            settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[1].Id }; // 0 => front, 1 => back
        }

        await _MediaCapture.InitializeAsync(settings);

        Cap1.Source = _MediaCapture;

        await _MediaCapture.StartPreviewAsync();
    }



private async void but1_Click_1(object sender, RoutedEventArgs e)
    {
        ScanResult.Text = "";
        var name = Guid.NewGuid().ToString();
        var Opt = CreationCollisionOption.ReplaceExisting;
        var file1 = await ApplicationData.Current.LocalFolder.CreateFileAsync(name, Opt);
        var imgformat = ImageEncodingProperties.CreatePng();
        await _MediaCapture.CapturePhotoToStorageFileAsync(imgformat, file1);
        BitmapImage bitmapImage = new BitmapImage(new Uri(file1.Path));
    Img1.Source = bitmapImage;
        var stream = await file1.OpenReadAsync();

        // initialize with 1,1 to get the current size of the image
        var writeableBmp = new WriteableBitmap(1, 1);
        writeableBmp.SetSource(stream);
        // and create it again because otherwise the WB isn't fully initialized and decoding
        // results in a IndexOutOfRange
        writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
        stream.Seek(0);
        writeableBmp.SetSource(stream);

        var result = ScanBitmap(writeableBmp);
        if (result != null)
        {
            ScanResult.Text += result.ToString();
        }
        else
        {
            ScanResult.Text = "no result";
        }

    }

    private object ScanBitmap(WriteableBitmap writeableBmp)
    {
        try
        {
            var barcodeReader = new BarcodeReader
                {
                    TryHarder = true,
                    AutoRotate = true
                };

            var result = barcodeReader.Decode(writeableBmp);

            if (result != null)
            {
                Img1.Source = writeableBmp;
            }
            return result;

        }
        catch (Exception e)
        {
            throw;
        }

    }
buttonScan.Click += (sender, e) => {

    var scanner = new ZXing.Mobile.MobileBarcodeScanner(this.Dispatcher);
    var result = await scanner.Scan();

  if (result != null)
    Debug.WriteLine("Scanned Barcode: " + result.Text);
};
var llPhoto = await mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateJpeg());
var photo = await llPhoto.CaptureAsync();

WriteableBitmap wbmp = new WriteableBitmap((int)photo.Frame.Width, (int)photo.Frame.Height);
wbmp.SetSource(photo.Frame.AsStream().AsRandomAccessStream());

result = ScanBitmap(wbmp);