示例:DownloadPreviewImage

DownloadPreviewImage 函数通过调用预览组件的 IWiaPreview::GetNewPreview 方法从扫描程序下载图像数据。 如果应用程序用户想要调用分段筛选器,它将调用 DetectSubregions 函数,该筛选器会在 pWiaItem2 下为其检测到的每个区域创建一个子项。 有关本示例中使用的 DetectSubregions 的信息,请参阅 IWiaSegmentationFilter::D etectRegions 方法。

在此示例中,应用程序用户通过单击检查框来设置m_bUseSegmentationFilter参数。 如果应用程序支持此功能,它应首先通过调用 IWiaItem2::CheckExtension 检查驱动程序具有分段筛选器。 有关此示例中使用的 CheckImgFilter 的信息,请参阅Microsoft Windows SDK文档中的 IWiaPreview::GetNewPreview 方法。

HRESULT
DownloadPreviewImage(
  IN IWiaItem2 *pWiaFlatbedItem2)
{
  HRESULT              hr = S_OK;
  BOOL                 bHasImgFilter  = FALSE;
  IWiaTransferCallback *pAppWiaTransferCallback = NULL;

  hr = CheckImgFilter(pWiaFlatbedItem2, &bHasImgFilter)

  if (SUCCEEDED(hr))
  {
     if (bHasImgFilter)
     {
        IWiaPreview *pWiaPreview = NULL;

        // In this example, the AppWiaTransferCallback class 
        // implements the IWiaTransferCallback interface.
         // The constructor of AppWiaTransferCallback sets the 
         // reference count to 1.
         pAppWiaTransferCallback = new AppWiaTransferCallback();

         hr = pAppWiaTransferCallback ? S_OK : E_OUTOFMEMORY;

         if (SUCCEEDED(hr))
         {
            // Acquire image from scanner
            hr = m_pWiaPreview->GetNewPreview(pWiaFlatbedItem2,
                                              0,
                                              pAppWiaTransferCallback);    
         }

         // m_FlatbedPreviewStream is the stream that
         // AppWiaTransferCallback::GetNextStream returned for the
         // flatbed item.
         // This stream is where the image data is stored after
         // the successful return of GetNewPreview.
         // The stream is passed into the segmentation filter
         // for region detection.
         if (SUCCEEDED(hr) && m_bUseSegmentationFilter)
         {
            DetectSubregions(m_FlatbedPreviewStream, pWiaFlatbedItem2);
         }

         if (pAppWiaTransferCallback)
         {
            // If the call to GetNewPreview was successful, the
            // preview component calls AddRef on the callback so
            // this call doesn't delete the object.

            pAppWiaTransferCallback->Release();
         }

      }
      else
      {
         // Do not create an instance of preview component if the 
         // driver does not come with an image-processing filter.
         // You can use a segmentation filter, however, if the driver
         // comes with one (omitted here).
      }
   }

   return hr;
}