Пример #1
0
        public async Task CS_W_ZX_DetectCode(string filename, BarcodeFormat barcodeFormat, string barcodeValue)
        {
            // Load a bitmap in Bgra8 colorspace
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Input/" + filename));

            var    source      = new StorageFileImageSource(file);
            Bitmap bitmapBgra8 = await source.GetBitmapAsync(null, OutputOption.PreserveAspectRatio);

            Assert.AreEqual(ColorMode.Bgra8888, bitmapBgra8.ColorMode);

            // Convert the bitmap to Nv12 colorspace (colorspace used when decoding barcode from cameras)
            var bitmapYuv = new Bitmap(bitmapBgra8.Dimensions, ColorMode.Yuv420Sp);

            bitmapYuv.ConvertFrom(bitmapBgra8);

            // Decode the barcode
            var reader = new BarcodeReader
            {
                Options = new DecodingOptions
                {
                    PossibleFormats = new BarcodeFormat[] { barcodeFormat },
                    TryHarder       = true
                }
            };
            Result resultBgra8 = reader.Decode(
                bitmapBgra8.Buffers[0].Buffer.ToArray(),
                (int)bitmapBgra8.Dimensions.Width,
                (int)bitmapBgra8.Dimensions.Height,
                BitmapFormat.BGRA32
                );
            Result resultYuv = reader.Decode(
                bitmapYuv.Buffers[0].Buffer.ToArray(),
                (int)bitmapYuv.Buffers[0].Pitch, // Should be width here but I haven't found a way to pass both width and stride to ZXing yet
                (int)bitmapYuv.Dimensions.Height,
                BitmapFormat.Gray8
                );

            Assert.IsNotNull(resultBgra8, "Decoding barcode in Bgra8 colorspace failed");
            Assert.AreEqual(barcodeValue, resultBgra8.Text);
            Assert.IsNotNull(resultYuv, "Decoding barcode in Nv12 colorspace failed");
            Assert.AreEqual(barcodeValue, resultYuv.Text);
        }
Пример #2
0
        public async Task CS_W_ZX_DetectCode(string filename, BarcodeFormat barcodeFormat, string barcodeValue)
        {
            // Load a bitmap in Bgra8 colorspace
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Input/" + filename));
            var source = new StorageFileImageSource(file);
            Bitmap bitmapBgra8 = await source.GetBitmapAsync(null, OutputOption.PreserveAspectRatio);
            Assert.AreEqual(ColorMode.Bgra8888, bitmapBgra8.ColorMode);

            // Convert the bitmap to Nv12 colorspace (colorspace used when decoding barcode from cameras)
            var bitmapYuv = new Bitmap(bitmapBgra8.Dimensions, ColorMode.Yuv420Sp);
            bitmapYuv.ConvertFrom(bitmapBgra8);

            // Decode the barcode
            var reader = new BarcodeReader
            {
                Options = new DecodingOptions
                {
                    PossibleFormats = new BarcodeFormat[] { barcodeFormat },
                    TryHarder = true
                }
            };
            Result resultBgra8 = reader.Decode(
                bitmapBgra8.Buffers[0].Buffer.ToArray(),
                (int)bitmapBgra8.Dimensions.Width,
                (int)bitmapBgra8.Dimensions.Height,
                BitmapFormat.BGRA32
                );
            Result resultYuv = reader.Decode(
                bitmapYuv.Buffers[0].Buffer.ToArray(),
                (int)bitmapYuv.Buffers[0].Pitch, // Should be width here but I haven't found a way to pass both width and stride to ZXing yet
                (int)bitmapYuv.Dimensions.Height,
                BitmapFormat.Gray8
                );

            Assert.IsNotNull(resultBgra8, "Decoding barcode in Bgra8 colorspace failed");
            Assert.AreEqual(barcodeValue, resultBgra8.Text);
            Assert.IsNotNull(resultYuv, "Decoding barcode in Nv12 colorspace failed");
            Assert.AreEqual(barcodeValue, resultYuv.Text);
        }