使用ZXing在WP8.1应用程序中读取条形码

时间:2015-08-16 17:00:40

标签: c# windows-phone-8 barcode zxing

我正在为WP8.1编写我的第一个应用程序。该应用只需要扫描条形码。

        public async void ScanBarcodeAsync(Windows.Storage.StorageFile Afile)
    {
        WriteableBitmap bitmap;
        BitmapDecoder decoder;

        using (IRandomAccessStream str = await Afile.OpenReadAsync())
        {
            decoder = await BitmapDecoder.CreateAsync(str);
            bitmap = new WriteableBitmap(Convert.ToInt32(decoder.PixelWidth),
                                         Convert.ToInt32(decoder.PixelHeight));
            await bitmap.SetSourceAsync(str);
        }

        ZXing.BarcodeReader reader = new BarcodeReader();
        /*reader.Options.PossibleFormats = new ZXing.BarcodeFormat[]
        {
            ZXing.BarcodeFormat.CODE_128,
            ZXing.BarcodeFormat.CODE_39
        };*/
        reader.Options.TryHarder = true;
        reader.AutoRotate = true;

        var results = reader.Decode(bitmap);
        if (results != null)
        {
            edtBarcode.Text = results.Text; 
        }
        else
        {
            edtBarcode.Text = "Error";
        }
    }

此方法的文件以这种方式创建

    async void StartCapture()
    {
        var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);

        MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
        settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview;
        settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;
        settings.AudioDeviceId = string.Empty;
        settings.VideoDeviceId = cameraID.Id;

        captureManager = new MediaCapture();    //Define MediaCapture object  
        await captureManager.InitializeAsync(settings);   //Initialize MediaCapture and 
        var focusSettings = new FocusSettings();
        focusSettings.AutoFocusRange = AutoFocusRange.Macro;
        focusSettings.Mode = FocusMode.Auto;
        focusSettings.WaitForFocus = true;
        focusSettings.DisableDriverFallback = false;

        captureManager.VideoDeviceController.FocusControl.Configure(focusSettings);
        captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
        capturePreview.Source = captureManager;   //Start preiving on CaptureElement  



        await captureManager.StartPreviewAsync();  //Start camera capturing   
    }
    async private void Capture_Photo_Click(object sender, RoutedEventArgs e)
    {
        //Create JPEG image Encoding format for storing image in JPEG type  
        ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
        // create storage file in local app storage  
        if (ImageIndex > 0)
        {
            StorageFile delFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Photo" + Convert.ToString(ImageIndex - 1) + ".jpg");
            await delFile.DeleteAsync();
        }
        file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Photo" + Convert.ToString(ImageIndex++) + ".jpg", CreationCollisionOption.ReplaceExisting);
        // take photo and store it on file location.  
        await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
        //// create storage file in Picture Library  
        //StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync("Photo.jpg",CreationCollisionOption.GenerateUniqueName);  
        // Get photo as a BitmapImage using storage file path.  

        bmpImage = new BitmapImage();
        bmpImage.CreateOptions = BitmapCreateOptions.None;
        bmpImage.UriSource = new Uri(file.Path);

        ImageTaken = true;
        Frame.Navigate(typeof(MainPage));
        Frame.Navigate(typeof(MainPage));
    }

运行此代码时出现错误。但是ZXing的结果总是空的。 ScanBarcodeAsync-Function在拍照后直接运行。应用程序拍摄的图像并不清晰。这会导致问题吗?

我很高兴得到任何建议来解决我的问题。

0 个答案:

没有答案