locked
How to read Barcode reader from the camera instance in windows 8?

    Question

  • How can I read bar-code And QR code from my camera using XAML C# in windows 8 application? I had tried using the CaptureElement with the reference from this http://blog.jerrynixon.com/2012/10/walkthrough-capturing-photos-in-your.html and implement some decode logic like this

    The problem with this is that,I am not able to get how to scan the particular object of the bar-code so that it decode it and give me the proper result.

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

    Tuesday, April 08, 2014 10:43 AM

Answers

  • You'll have to write your own code that detects the barcode from an image.  Although we have barcode APIs in the Windows Store API set, they assume that the data is being detected by a physical device (a barcode scanner) and the resultant data is being sent back to the API. 

    You can likely buy a third-party SDK that does this for you.  Please see this post for more information:
    http://social.msdn.microsoft.com/Forums/windowsapps/en-US/93445a7c-07f8-4770-b1f4-00731d1e4f1b/how-to-read-barcode-reader-in-windows-8?forum=winappswithcsharp

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, April 08, 2014 1:19 PM
    Moderator

All replies

  • You'll have to write your own code that detects the barcode from an image.  Although we have barcode APIs in the Windows Store API set, they assume that the data is being detected by a physical device (a barcode scanner) and the resultant data is being sent back to the API. 

    You can likely buy a third-party SDK that does this for you.  Please see this post for more information:
    http://social.msdn.microsoft.com/Forums/windowsapps/en-US/93445a7c-07f8-4770-b1f4-00731d1e4f1b/how-to-read-barcode-reader-in-windows-8?forum=winappswithcsharp

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, April 08, 2014 1:19 PM
    Moderator
  • Thanks Matt for the response. I had refereed the link but still am not clear, can you suggest me any third party SDK for windows 8 store app ..

    From the above code I am able to get the scan result but the problem is that I have to capture only area of bar-code, if any unnecessary text or background appear then am not able to get the scan result.      

     
    Wednesday, April 09, 2014 1:49 PM
  • Yes, I am aware of what you're asking for. The problem is that you'll need to either figure out it out on your own, or buy a third-party API kit that does this for you, since we don't have it built-in to the WinRT API as it is.  I don't have any to recommend.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 10, 2014 3:05 PM
    Moderator