예제 #1
0
        /// <summary>
        /// Gets the device that will be used to acquire an image.
        /// </summary>
        private static WiaResult GetDevice(out Device device)
        {
            device = null;
            try
            {
                CommonDialogClass cdc = new CommonDialogClass();
                device = cdc.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, false, false);

                if (device == null)
                //Either the user cancelled OR we have no device we can use to get an image.
                {
                    return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                       "get the capture device",
                                                       "No device was selected")));
                }
                else
                {
                    return(WiaResult.Success);
                }
            }
            catch (COMException ce)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(WiaConstants.LoggingConstants.ExceptionOccurred, ce);
                }
                return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                   "get the capture device",
                                                   WiaError.GetErrorMessage(ce))));
            }
        }
예제 #2
0
        /// <summary>
        /// Queries a device for the Wia Item that we will use to do the capture and
        /// transfer.
        /// </summary>
        private static WiaResult GetCaptureItem(Device device, out Item item)
        {
            item = null;
            try
            {
                //Show UI.
                CommonDialogClass cdc   = new CommonDialogClass();
                Items             items = cdc.ShowSelectItems(
                    device,
                    WiaImageIntent.UnspecifiedIntent,
                    WiaImageBias.MaximizeQuality,
                    true,
                    true,
                    false);

                if (items == null || items.Count == 0)
                //Cannot obtain an item to acquire the image OR the user cancelled.
                {
                    return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                       "get the capture item",
                                                       "No items were selected to be captured")));
                }
                else
                {
                    //Wia arrays are 1-based-indexed.  1 here represents the first item.
                    item = items[1];

                    return(WiaResult.Success);
                }
            }
            catch (COMException ce)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(WiaConstants.LoggingConstants.ExceptionOccurred, ce);
                }
                return(new WiaResult(string.Format(ERROR_MESSAGE_FORMAT,
                                                   "get the capture item",
                                                   WiaError.GetErrorMessage(ce))));
            }
        }
예제 #3
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))));
            }
        }
예제 #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))));
            }
        }