示例#1
0
        /// <summary>
        /// Presents the user with multiple peices of WIA UI so that they
        /// can select a capture device, then perform the image acquisition and
        /// will finally save the image to the HD (and transcode to JPG is required).
        /// </summary>
        /// <param name="outputDirectory">The directory the image should be placed into</param>
        public static WiaResult SelectAndCapture(string outputDirectory, out string outputtedFile)
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug(WiaConstants.LoggingConstants.MethodCalled);
            }

            outputtedFile = string.Empty;

            #region Input validation

            if (string.IsNullOrEmpty(outputDirectory) ||
                Directory.Exists(outputDirectory) == false)
            {
                return(new WiaResult("The specified output directory does not exist"));
            }

            #endregion

            WiaResult result;

            //1. Get the capture device
            Device device = null;
            result = GetDevice(out device);

            if (!result.Succeeded)
            {
                return(result);
            }

            //2. Get the capture item that we can use to transfer the image from.
            Item captureItem = null;
            result = GetCaptureItem(device, out captureItem);

            if (!result.Succeeded)
            {
                return(result);
            }

            //3. Obtain the format information we need to transfer the image.
            WiaFormat format = null;
            result = GetFormat(captureItem, out format);
            if (!result.Succeeded)
            {
                return(result);
            }

            //4. Transfer the image
            string fileName = null;
            result = TransferImage(captureItem, format, outputDirectory, out fileName);
            if (!result.Succeeded)
            {
                return(result);
            }

            //If we've gotten here then everything went ok
            outputtedFile = fileName;
            return(WiaResult.Success);
        }
示例#2
0
        /// <summary>
        /// Gets a WiaFormat object that represents an image format that the device
        /// and our app both understand.
        /// </summary>
        private static WiaResult GetFormat(Item item, out WiaFormat format)
        {
            format = null;
            try
            {
                //Devices can have supported formats and preferred formats.
                string deviceFormatGuid, devicePreferredFormatGuid;
                deviceFormatGuid = devicePreferredFormatGuid = null;

                //Get the format information from the device
                foreach (Property prop in item.Properties)
                {
                    System.Diagnostics.Debug.WriteLine(prop.PropertyID + " - " + prop.Name + " - " + prop.get_Value());
                    switch (prop.PropertyID)
                    {
                    case WiaConstants.WiaProperties.FORMAT_ID:
                        deviceFormatGuid = (prop.get_Value() == null ? null : prop.get_Value().ToString());
                        break;

                    case WiaConstants.WiaProperties.PREFERRED_FORMAT_ID:
                        devicePreferredFormatGuid = (prop.get_Value() == null ? null : prop.get_Value().ToString());
                        break;
                    }
                }

                //We'll try and match on the preferred format first.

                if (string.IsNullOrEmpty(devicePreferredFormatGuid) == false)
                //Check to see if we support the preferred format.
                {
                    format = WiaFormats.GetWiaFormat(devicePreferredFormatGuid);

                    if (format != null)
                    {
                        return(WiaResult.Success);
                    }
                }

                if (string.IsNullOrEmpty(deviceFormatGuid) == false)
                //Check to see if we support the format
                {
                    format = WiaFormats.GetWiaFormat(deviceFormatGuid);

                    if (format != null)
                    {
                        return(WiaResult.Success);
                    }
                }

                //If we've gotten here then the device does not support a format we can
                //work with
                return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                   "get the image format from the device",
                                                   "No supported file format can be found on the device")));
            }
            catch (COMException ce)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(WiaConstants.LoggingConstants.ExceptionOccurred, ce);
                }
                return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                   "get the image format from the device",
                                                   WiaError.GetErrorMessage(ce))));
            }
        }
示例#3
0
        /// <summary>
        /// Transfers an image from the capture device
        /// </summary>
        private static WiaResult TransferImage(Item item, WiaFormat format, string outputPath, out string fileName)
        {
            fileName = string.Empty;
            try
            {
                //Initiate transfer
                ImageFile image = item.Transfer(format.ComGuid) as ImageFile;

                if (image != null)
                //Ensure something was obtained.
                {
                    string tempFileName = string.Format("{0}.{1}", Guid.NewGuid().ToString(), format.FileExtension);

                    if (!outputPath.EndsWith("\\"))
                    {
                        outputPath += "\\";
                    }

                    tempFileName = outputPath + tempFileName;

                    image.SaveFile(tempFileName);

                    //An exception will be thrown if the image cannot be saved, by getting here
                    //we know that the save succeeded.
                    fileName = tempFileName;

                    //If the image was not saved in Jpg format then transcode it here
                    if (!WiaFormats.IsJpg(format.ComGuid))
                    {
                        string    transcodedFileName;
                        WiaResult transcodeResult = TranscodeImageToJpg(fileName, out transcodedFileName);

                        if (!transcodeResult.Succeeded)
                        {
                            return(transcodeResult);
                        }
                        else
                        {
                            fileName = transcodedFileName;
                        }
                    }
                    return(WiaResult.Success);
                }
                else
                {
                    return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                       "transfer the image from the device",
                                                       "The image returned could not be cast to an ImageFile interface")));
                }
            }
            catch (COMException ce)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(WiaConstants.LoggingConstants.ExceptionOccurred, ce);
                }
                return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                   "transfer the image from the device",
                                                   WiaError.GetErrorMessage(ce))));
            }
        }
示例#4
0
        /// <summary>
        /// Transfers an image from the capture device
        /// </summary>
        private static WiaResult TransferImage(Item item, WiaFormat format, string outputPath, out string fileName)
        {
            fileName = string.Empty;
            try
            {
                //Initiate transfer
                ImageFile image = item.Transfer(format.ComGuid) as ImageFile;

                if (image != null)
                    //Ensure something was obtained.
                {
                    string tempFileName = string.Format("{0}.{1}", Guid.NewGuid().ToString(), format.FileExtension);

                    if (!outputPath.EndsWith("\\"))
                    {
                        outputPath += "\\";
                    }

                    tempFileName = outputPath + tempFileName;

                    image.SaveFile(tempFileName);

                    //An exception will be thrown if the image cannot be saved, by getting here
                    //we know that the save succeeded.
                    fileName = tempFileName;

                    //If the image was not saved in Jpg format then transcode it here
                    if (!WiaFormats.IsJpg(format.ComGuid))
                    {
                        string transcodedFileName;
                        WiaResult transcodeResult = TranscodeImageToJpg(fileName, out transcodedFileName);

                        if (!transcodeResult.Succeeded)
                        {
                            return transcodeResult;
                        }
                        else
                        {
                            fileName = transcodedFileName;
                        }
                    }
                    return WiaResult.Success;
                }
                else
                {
                    return new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                        "transfer the image from the device",
                        "The image returned could not be cast to an ImageFile interface"));
                }
            }
            catch (COMException ce)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(WiaConstants.LoggingConstants.ExceptionOccurred, ce);
                }
                return new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                    "transfer the image from the device",
                    WiaError.GetErrorMessage(ce)));
            }
        }
示例#5
0
        /// <summary>
        /// Gets a WiaFormat object that represents an image format that the device
        /// and our app both understand.
        /// </summary>
        private static WiaResult GetFormat(Item item, out WiaFormat format)
        {
            format = null;
            try
            {
                //Devices can have supported formats and preferred formats.
                string deviceFormatGuid, devicePreferredFormatGuid;
                deviceFormatGuid = devicePreferredFormatGuid = null;

                //Get the format information from the device
                foreach (Property prop in item.Properties)
                {
                    System.Diagnostics.Debug.WriteLine(prop.PropertyID + " - " + prop.Name + " - " + prop.get_Value());
                    switch (prop.PropertyID)
                    {
                        case WiaConstants.WiaProperties.FORMAT_ID:
                            deviceFormatGuid = (prop.get_Value() == null ? null : prop.get_Value().ToString());
                            break;
                        case WiaConstants.WiaProperties.PREFERRED_FORMAT_ID:
                            devicePreferredFormatGuid = (prop.get_Value() == null ? null : prop.get_Value().ToString());
                            break;
                    }
                }

                //We'll try and match on the preferred format first.

                if (string.IsNullOrEmpty(devicePreferredFormatGuid) == false)
                //Check to see if we support the preferred format.
                {
                    format = WiaFormats.GetWiaFormat(devicePreferredFormatGuid);

                    if (format != null)
                    {
                        return WiaResult.Success;
                    }

                }

                if (string.IsNullOrEmpty(deviceFormatGuid) == false )
                //Check to see if we support the format
                {
                    format = WiaFormats.GetWiaFormat(deviceFormatGuid);

                    if (format != null)
                    {
                        return WiaResult.Success;
                    }
                }

                //If we've gotten here then the device does not support a format we can
                //work with
                return new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                    "get the image format from the device",
                    "No supported file format can be found on the device"));

            }
            catch (COMException ce)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(WiaConstants.LoggingConstants.ExceptionOccurred, ce);
                }
                return new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                    "get the image format from the device",
                    WiaError.GetErrorMessage(ce)));
            }
        }
示例#6
0
        /// <summary>
        /// Checks whether or not the supplied formatGuid is one we can work with
        /// </summary>
        public static bool IsFormatSupported(string formatGuid)
        {
            WiaFormat format = GetWiaFormat(formatGuid);

            return(format != null);
        }