如何从Windows 8中的相机实例读取条形码阅读器?

时间:2014-04-08 05:13:31

标签: c# .net windows-8 windows-8.1

如何在Windows 8应用程序中使用XAML C#从相机中读取条形码和QR码? 我曾尝试将 CaptureElement 与此http://blog.jerrynixon.com/2012/10/walkthrough-capturing-photos-in-your.html的引用结合使用,并实现一些像这样的解码逻辑

问题在于,我无法了解如何扫描条形码的特定对象,以便对其进行解码并给出正确的结果。

XAML

<StackPanel Orientation="Horizontal" Height="900" Width="900">
        <StackPanel>
            <TextBlock FontSize="20" Text="Preview"></TextBlock>
            <CaptureElement x:Name="Cap1" Height="700" Width="250"></CaptureElement>
        </StackPanel>
        <Button x:Name="but1" Click="but1_Click_1" Content="Take" Height="150" Width="250"></Button>
        <StackPanel>
            <TextBlock FontSize="20" Text="Snapshot"></TextBlock>
            <Image x:Name="Img1" Height="600" Width="600"></Image>
            <TextBlock x:Name="ScanResult" FontSize="20" />
        </StackPanel>
    </StackPanel>

C#

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

    }

1 个答案:

答案 0 :(得分:0)

对于Windows Phone,只需使用Zxing.Mobile

BarcodeScanner.Xaml.cs

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

如果您计划使用WinRT应用,则应执行this之类的操作。

拍摄照片并尝试识别直到找到条形码。没有更好的方法来做到这一点。实际上你可以改进这样的代码

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手机中尝试第二种解决方案,那么拍摄速度会太慢在2-3秒内处理每个图像。