locked
Barcode Reader Code for Surface Pro Webcam

    Question

  • I am building out a Windows 8.1 app to read barcodes, utilizing the Back webcam of a Surface Pro (first generation);

    If the Surface Pro device, running the app, is held up to a sample barcode on a computer monitor - the scanning works properly and captures the barcode;

    However, if a real object (such as a box of Kleenex) is held up to the Surface Pro device, running the app, webcam focus seems to be an issue - and the barcode scanning process is not implemented;

    A code sample is below for the start preview button - any insight, guidance is appreciated;

     private async void StartPreview_Click(object sender, RoutedEventArgs e)
            {
                //var resultCamera = await GetMediaCapture();
    
                Windows.Devices.Enumeration.Panel location = Windows.Devices.Enumeration.Panel.Back;
    
                var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    
                foreach (var camera in cameras)
                {
                    if (camera.EnclosureLocation.Panel == location)
                    {
                        var mcs = new MediaCaptureInitializationSettings();
                        mcs.VideoDeviceId = camera.Id;
                        mcs.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
                        mcs.StreamingCaptureMode = StreamingCaptureMode.Video;
                        var mc = new MediaCapture();
                        await mc.InitializeAsync(mcs);
    
                        VideoStream.Source = mc;
                        await mc.StartPreviewAsync();
                        mc.SetPreviewMirroring(false);
                        ShowSettings.Visibility = Visibility.Visible;
                        StartPreview.IsEnabled = false;
    
                        var properties = mc.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
    
                        var videoEncodingProperties = properties as VideoEncodingProperties;
                        if (videoEncodingProperties != null)
                        {
                            var writeableBitmap = new WriteableBitmap((int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height);
    
                            await Task.Delay(1000);
    
                            bool barCodeNotFound = true;
    
                            while (barCodeNotFound)
                            {
                                stream.Seek(0);
                                await mc.CapturePhotoToStreamAsync(imageEncoding, stream);
    
                                stream.Seek(0);
    
                                await writeableBitmap.SetSourceAsync(stream);
    
                                var result = ScanBitmap(writeableBitmap);
                                if (result != null)
                                {
                                    ScanResult.Text = result.Text;
                                    barCodeNotFound = false;
                                    await mc.StopPreviewAsync(); 
                                    mc = null;
                                    previewStarted = false;
                                    VideoStream.Source = null;
                                    StartPreview.IsEnabled = true;
                                }
                            }
                        }
    
                        // return mc;
                    }
                }


    robert hellestrae

    Wednesday, May 28, 2014 2:19 PM

Answers

  • This is probably a hardware limitation. Check on the Surface Pro forums, but I believe the Surface Pro has limited near focus ability.
    • Marked as answer by rrobhelle Saturday, May 31, 2014 8:30 PM
    Wednesday, May 28, 2014 2:34 PM
    Owner
  • The end user forums are at http://answers.microsoft.com . There is a Surface forum there that may know about the camera. I don't have a Surface Pro myself (but I really want one! :) ).

    There won't be a way to work around hardware limitations in code. The barcode would need to be read from far enough out that the camera can focus.

    --Rob

    • Marked as answer by rrobhelle Saturday, May 31, 2014 8:31 PM
    Wednesday, May 28, 2014 9:59 PM
    Owner

All replies

  • This is probably a hardware limitation. Check on the Surface Pro forums, but I believe the Surface Pro has limited near focus ability.
    • Marked as answer by rrobhelle Saturday, May 31, 2014 8:30 PM
    Wednesday, May 28, 2014 2:34 PM
    Owner
  • Thanks for the feedback - is a link available for the Surface Pro forum?

    Is any C# code, related to the MediaCapture class, available to work with the focus process of the back webcam on the Surface Pro device?


    robert hellestrae

    Wednesday, May 28, 2014 2:40 PM
  • The end user forums are at http://answers.microsoft.com . There is a Surface forum there that may know about the camera. I don't have a Surface Pro myself (but I really want one! :) ).

    There won't be a way to work around hardware limitations in code. The barcode would need to be read from far enough out that the camera can focus.

    --Rob

    • Marked as answer by rrobhelle Saturday, May 31, 2014 8:31 PM
    Wednesday, May 28, 2014 9:59 PM
    Owner
  • How did you get on?
    Would you mind sharing your ScanBitmap method please? :P
    Monday, January 12, 2015 5:55 PM