Exemplo n.º 1
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))));
            }
        }
Exemplo n.º 2
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))));
            }
        }