コード例 #1
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // METHODS //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private string GenerateFriendlyName(ImageScannerFormat targetFormat)
        {
            switch (targetFormat)
            {
            case ImageScannerFormat.Jpeg:
                return("JPG");

            case ImageScannerFormat.Png:
                return("PNG");

            case ImageScannerFormat.DeviceIndependentBitmap:
                return("BMP");

            case ImageScannerFormat.Tiff:
                return("TIF");

            case ImageScannerFormat.Xps:
                return("XPS");

            case ImageScannerFormat.OpenXps:
                return("OpenXPS");

            case ImageScannerFormat.Pdf:
                return("PDF");

            default:
                throw new ArgumentException("Unable to generate FriendlyName from format " + targetFormat + ".");
            }
        }
コード例 #2
0
ファイル: Utilities.cs プロジェクト: simon-knuth/scanner
    /// <summary>
    ///     Converts a <see cref="ImageScannerFormat"/> into the corresponding format string.
    /// </summary>
    /// <param name="format">An image format.</param>
    /// <returns>The corresponding string.</returns>
    public static string ConvertImageScannerFormatToString(ImageScannerFormat format)
    {
        switch (format)
        {
        case ImageScannerFormat.Jpeg:
            return(".jpg");

        case ImageScannerFormat.Png:
            return(".png");

        case ImageScannerFormat.DeviceIndependentBitmap:
            return(".bmp");

        case ImageScannerFormat.Tiff:
            return(".tif");

        case ImageScannerFormat.Xps:
            return(".xps");

        case ImageScannerFormat.OpenXps:
            return(".oxps");

        case ImageScannerFormat.Pdf:
            return(".pdf");

        default:
            throw new ApplicationException("ConvertImageScannerFormatToImageScannerFormat received invalid ImageScannerFormat '" + format + "' for conversion.");
        }
    }
コード例 #3
0
        /// <summary>
        ///     Converts the given <see cref="ImageScannerFormat"/> into a glyph string.
        /// </summary>
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            ImageScannerFormat format = (ImageScannerFormat)value;

            switch (format)
            {
            case ImageScannerFormat.Jpeg:
                return(glyphImage);

            case ImageScannerFormat.Png:
                return(glyphImage);

            case ImageScannerFormat.DeviceIndependentBitmap:
                return(glyphImage);

            case ImageScannerFormat.Tiff:
                return(glyphImage);

            case ImageScannerFormat.Xps:
                return(glyphImage);

            case ImageScannerFormat.OpenXps:
                return(glyphImage);

            case ImageScannerFormat.Pdf:
                return(glyphPdf);

            default:
                break;
            }

            return((int)value);
        }
コード例 #4
0
ファイル: Utilities.cs プロジェクト: simon-knuth/scanner
    /// <summary>
    ///     Checks whether the given format is an image format.
    /// </summary>
    public static bool IsImageFormat(ImageScannerFormat format)
    {
        switch (format)
        {
        case ImageScannerFormat.Jpeg:
        case ImageScannerFormat.Png:
        case ImageScannerFormat.DeviceIndependentBitmap:
        case ImageScannerFormat.Tiff:
            return(true);

        case ImageScannerFormat.Xps:
        case ImageScannerFormat.OpenXps:
        case ImageScannerFormat.Pdf:
        default:
            return(false);
        }
    }
コード例 #5
0
        public bool SupportsFileFormat(ScannerSource source, ImageScannerFormat format)
        {
            switch (source)
            {
            case ScannerSource.Auto:
                return(AutoFormats.Any((x) => x.TargetFormat == format));

            case ScannerSource.Flatbed:
                return(FlatbedFormats.Any((x) => x.TargetFormat == format));

            case ScannerSource.Feeder:
                return(FeederFormats.Any((x) => x.TargetFormat == format));

            case ScannerSource.None:
            default:
                return(false);
            }
        }
コード例 #6
0
        /// <summary>
        ///     Attempts to determine the <see cref="BitmapRotation"/> needed to fix the orientation of the given
        ///     <paramref name="imageFile"/>.
        /// </summary>
        public async Task <BitmapRotation> TryGetRecommendedRotationAsync(StorageFile imageFile, ImageScannerFormat format)
        {
            try
            {
                if (OcrEngine != null)
                {
                    // get separate stream
                    using (IRandomAccessStream sourceStream = await imageFile.OpenAsync(FileAccessMode.Read))
                    {
                        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);

                        Tuple <BitmapRotation, int> bestRotation;

                        // create rotated 0°
                        SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync();

                        OcrResult ocrResult = await OcrEngine.RecognizeAsync(bitmap);

                        bestRotation = new Tuple <BitmapRotation, int>(BitmapRotation.None, ocrResult.Text.Length);

                        using (InMemoryRandomAccessStream targetStream = new InMemoryRandomAccessStream())
                        {
                            // create rotated 90°
                            BitmapEncoder encoder = await HelperService.CreateOptimizedBitmapEncoderAsync(format, targetStream);

                            encoder.SetSoftwareBitmap(bitmap);
                            encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
                            await encoder.FlushAsync();

                            decoder = await BitmapDecoder.CreateAsync(targetStream);

                            bitmap = await decoder.GetSoftwareBitmapAsync();

                            ocrResult = await OcrEngine.RecognizeAsync(bitmap);

                            if (ocrResult.Text.Length > bestRotation.Item2)
                            {
                                bestRotation = new Tuple <BitmapRotation, int>(BitmapRotation.Clockwise90Degrees, ocrResult.Text.Length);
                            }
                        }

                        using (InMemoryRandomAccessStream targetStream = new InMemoryRandomAccessStream())
                        {
                            // create rotated 180°
                            BitmapEncoder encoder = await HelperService.CreateOptimizedBitmapEncoderAsync(format, targetStream);

                            encoder.SetSoftwareBitmap(bitmap);
                            encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
                            await encoder.FlushAsync();

                            decoder = await BitmapDecoder.CreateAsync(targetStream);

                            bitmap = await decoder.GetSoftwareBitmapAsync();

                            ocrResult = await OcrEngine.RecognizeAsync(bitmap);

                            if (ocrResult.Text.Length > bestRotation.Item2)
                            {
                                bestRotation = new Tuple <BitmapRotation, int>(BitmapRotation.Clockwise180Degrees, ocrResult.Text.Length);
                            }
                        }

                        using (InMemoryRandomAccessStream targetStream = new InMemoryRandomAccessStream())
                        {
                            // create rotated 270°
                            BitmapEncoder encoder = await HelperService.CreateOptimizedBitmapEncoderAsync(format, targetStream);

                            encoder.SetSoftwareBitmap(bitmap);
                            encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
                            await encoder.FlushAsync();

                            decoder = await BitmapDecoder.CreateAsync(targetStream);

                            bitmap = await decoder.GetSoftwareBitmapAsync();

                            ocrResult = await OcrEngine.RecognizeAsync(bitmap);

                            if (ocrResult.Text.Length > bestRotation.Item2)
                            {
                                bestRotation = new Tuple <BitmapRotation, int>(BitmapRotation.Clockwise270Degrees, ocrResult.Text.Length);
                            }
                        }

                        bitmap.Dispose();

                        if (bestRotation.Item2 < MinimumNumberOfWords)
                        {
                            // very low confidence, could just be random patterns
                            return(BitmapRotation.None);
                        }
                        else
                        {
                            return(bestRotation.Item1);
                        }
                    }
                }
                else
                {
                    return(BitmapRotation.None);
                }
            }
            catch (Exception exc)
            {
                LogService?.Log.Error(exc, "Determining the recommended rotation failed.");
                AppCenterService?.TrackError(exc);
                return(BitmapRotation.None);
            }
        }
コード例 #7
0
 public ScannerFileFormat(ImageScannerFormat targetFormat)
 {
     TargetFormat = targetFormat;
     FriendlyName = GenerateFriendlyName(TargetFormat);
 }