简体   繁体   English

为什么ZXing使用前置摄像头而不是后置摄像头解码条形码? 通用应用程序Win10

[英]Why ZXing Decodes Barcodes with front camera and not back? Univeral App Win10

I am creating a simple Barcode scanner using the ZXing libuary for Windows 10 Universal. 我正在使用适用于Windows 10 Universal的ZXing库创建一个简单的条形码扫描仪。

Using the front camera it will recognise a barcode and display the result at the bottom of the page. 使用前置摄像头,它将识别条形码并将结果显示在页面底部。

Unfortunatly forcing the phone to use the rear camera will never successfully decode a barcode. 不幸的是,强迫手机使用后置摄像头将永远无法成功解码条形码。

Has anyone got an explination as to why this is? 有谁知道这是为什么呢? Could it be a focusing issue? 可能是一个重点问题吗?

My test device is a Nokia Lumia 650 (Front and Rear Cameras) 我的测试设备是诺基亚Lumia 650(前后摄像头)

Below is an example of what I have: (VB.NET) 下面是我所拥有的示例:(VB.NET)

Public NotInheritable Class MainPage
Inherits Page

Private capture As New MediaCapture
Private result As Result
Private reader As IBarcodeReader

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

End Sub

''' <summary>
''' On Navigation to this page handler.
''' This is the main sub which handles the init, saving and decoding of each frame viewed by the camera.
''' the decoding is then delegated to a different function
''' </summary>
''' <param name="e"></param>
Protected Overrides Async Sub onNavigatedTo(e As NavigationEventArgs)

    ' Check for camera existance
    Dim cameras = Await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)
    If (cameras.Count < 1) Then
        ErrorMsg.Text = "No camera found"
        Exit Sub
    End If

    ' Get camera settings
    Dim settings = New MediaCaptureInitializationSettings()

    If (cameras.Count = 1) Then
        settings.VideoDeviceId = cameras(1).Id ' Front Camera
    Else
        settings.VideoDeviceId = cameras(0).Id ' Back Camera
    End If

    Await capture.InitializeAsync(settings)

    ' Set the Camera Preview
    ' Rotate the camera to fix portrait issues
    capture.SetPreviewRotation(VideoRotation.Clockwise90Degrees)

    VideoCapture.Source = capture

    Await capture.StartPreviewAsync()

    ' While there has been no result, Keep checking
    While (result Is Nothing)
        ' Createa a JPEG of the current frame in the preview finder
        Dim photoStorageFile = Await KnownFolders.PicturesLibrary.CreateFileAsync("scan.jpg", CreationCollisionOption.GenerateUniqueName)
        Await capture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoStorageFile)

        ' Open the File as a bitmap
        Dim stream = Await photoStorageFile.OpenReadAsync()
        Dim writeableBmp = New WriteableBitmap(1, 1)
        writeableBmp.SetSource(stream)
        writeableBmp = New WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight)
        stream.Seek(0)
        writeableBmp.SetSource(stream)

        ' Scan the frame for the existance of a barcode, If there is a barcode, then pass result the details
        result = ScanBitmap(writeableBmp)

        ' Delete the saved frame in order to save on memory VERY IMPORTANT!!
        Await photoStorageFile.DeleteAsync(StorageDeleteOption.PermanentDelete)
    End While

    ' Broken out of the while loop meaning there is a result!
    ' Stop the Prevew and show the barcode result
    Await capture.StopPreviewAsync()
    VideoCapture.Visibility = Visibility.Collapsed
    CaptureImage.Visibility = Visibility.Visible
    ScanResult.Text = result.Text

End Sub

''' <summary>
''' Scans the bitmap passed in with the ZXing lib in order to determine if the preview frame now has a barcode in view.
''' </summary>
''' <param name="writeableBmp">Bitmap that is intended to be decoded</param>
''' <returns>Result of the barcode</returns>
Private Function ScanBitmap(writeableBmp As WriteableBitmap) As Result
    ' Create a ZXing Barcode reader and init
    Dim barcodeReader = New BarcodeReader
    barcodeReader.Options.TryHarder = True
    barcodeReader.AutoRotate = True
    barcodeReader.TryInverted = True

    ' Pass the bitmap to the barcode reader to decode any possible barcodes
    Dim result = barcodeReader.Decode(writeableBmp)

    ' If result is something then Show this frame in the preview
    If (result IsNot Nothing) Then
        CaptureImage.Source = writeableBmp
    End If

    ' Return the result back to the main sub.
    Return result

End Function

End Class 末级

I have now discovered it has something to do with the rear camera not having a sharp enough image 我现在发现它与后置摄像头图像不够清晰有关

The Line below is what I added to get the rear camera to work. 下面的线是我添加的以使后置摄像头正常工作。 It forces the auto-focus. 强制自动聚焦。 As this would not work in every situation (Devices of which cannot focus) other checks would need to be implemented. 由于这并非在所有情况下都无法解决(设备无法专注),因此需要执行其他检查。

capture.VideoDeviceController.FocusControl.FocusAsync()

Unfortunatly, The scanning is rather slow now but it is working. 不幸的是,现在扫描速度很慢,但是可以正常工作。

Phones tend to have a much higher quality rear camera that supports autofocus. 手机往往具有支持自动对焦的更高质量的后置摄像头。 For QR code reading, you will have to manually focus the lens before getting a frame. 要读取QR码,必须先手动对焦镜头,然后再取景。 Have a look at the CameraManualControls SDK sample to learn how to operate the focus modes. 看看CameraManualControls SDK示例以了解如何操作对焦模式。

For example, this is a snippet that shows you how to do Continuous AutoFocus: 例如,下面的代码片段向您展示了如何进行连续自动对焦:

private async void CafFocusRadioButton_Checked(object sender, RoutedEventArgs e)
{
    var focusControl = _mediaCapture.VideoDeviceController.FocusControl;

    await focusControl.UnlockAsync();

    var settings = new FocusSettings { Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange };
    focusControl.Configure(settings);
    await focusControl.FocusAsync();
}

Have a look at the full sample for more information. 查看完整样本以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM