共用方式為


在 WIA 1.0 中傳輸影像資料

注意

本教學課程是關於在將執行 Windows XP 或更早版本的應用程式中傳輸映射資料。 如需在 Windows Vista 或更新版本上執行之應用程式中傳輸影像資料的相關資訊,請參閱 在 WIA 2.0 中傳輸影像資料。

 

使用 IWiaDataTransfer 介面的方法,將資料從 Windows Image Acquisition (WIA) 1.0 裝置傳輸到應用程式。 此介面支援共用記憶體視窗,以將資料從裝置物件傳輸到應用程式,並在封送處理期間消除不必要的資料複本。

應用程式必須查詢影像專案,以取得其 IWiaDataTransfer 介面的指標,如下列程式碼範例所示:

    // Get the IWiaDataTransfer interface
    //
    IWiaDataTransfer *pWiaDataTransfer = NULL;
    hr = pWiaItem->QueryInterface( IID_IWiaDataTransfer, (void**)&pWiaDataTransfer );

在先前的程式碼中,假設 pWiaItemIWiaItem 介面的有效指標。 IUnknown::QueryInterface呼叫會填入pWiaDataTransfer,其中含有pWiaItem所參考專案的IWiaDataTransfer介面指標。

然後,應用程式會設定 STGMEDIUM 結構成員。 如需詳細資訊,請參閱 STGMEDIUM 和 TYMED

    // Set storage medium
    //
    STGMEDIUM StgMedium = {0};
    StgMedium.tymed = TYMED_FILE;

如果您提供檔案名,它應該包含適當的副檔名;WIA 1.0 不提供副檔名。 如果StgMediumlpszFileName成員為Null,WIA 1.0 會產生傳輸資料的隨機檔案名和路徑。 在呼叫 ReleaseStgMedium 之前移動或複製此檔案,因為此函式會刪除檔案。

應用程式接著會呼叫 IWiaDataTransfer::idtGetData 方法來執行資料傳輸:

    // Perform the transfer
    //
    hr = pWiaDataTransfer->idtGetData( &stgMedium, pWiaDataCallback );

IWiaDataTransfer::idtGetData的呼叫中,第二個參數會指定 IWiaDataCallback 介面的指標。 應用程式必須實作此介面,才能在資料傳輸期間接收回呼。 如需實作此介面的相關資訊,請參閱 IWiaDataCallback::BandedDataCallback

然後,應用程式會釋放 IWiaDataTransfer 介面的指標,並釋放 STGMEDIUM 結構中配置的任何資料。

應用程式也可以使用記憶體內部資料傳輸來傳輸映射,而不是檔案傳輸。 在此情況下,應用程式會使用 idtGetBandedData 方法取代 idtGetData 方法。

以下是完整的資料傳輸範例:

HRESULT TransferWiaItem( IWiaItem *pWiaItem )
{
    //
    // Validate arguments
    //
    if (NULL == pWiaItem)
    {
        return E_INVALIDARG;
    }

    //
    // Get the IWiaPropertyStorage interface so you can set required properties.
    //
    IWiaPropertyStorage *pWiaPropertyStorage = NULL;
    HRESULT hr = pWiaItem->QueryInterface( IID_IWiaPropertyStorage, (void**)&pWiaPropertyStorage );
    if (SUCCEEDED(hr))
    {
        //
        // Prepare PROPSPECs and PROPVARIANTs for setting the
        // media type and format
        //
        PROPSPEC PropSpec[2] = {0};
        PROPVARIANT PropVariant[2] = {0};
        const ULONG c_nPropCount = sizeof(PropVariant)/sizeof(PropVariant[0]);

        //
        // Use BMP as the output format
        //
        GUID guidOutputFormat = WiaImgFmt_BMP;

        //
        // Initialize the PROPSPECs
        //
        PropSpec[0].ulKind = PRSPEC_PROPID;
        PropSpec[0].propid = WIA_IPA_FORMAT;
        PropSpec[1].ulKind = PRSPEC_PROPID;
        PropSpec[1].propid = WIA_IPA_TYMED;

        //
        // Initialize the PROPVARIANTs
        //
        PropVariant[0].vt = VT_CLSID;
        PropVariant[0].puuid = &guidOutputFormat;
        PropVariant[1].vt = VT_I4;
        PropVariant[1].lVal = TYMED_FILE;

        //
        // Set the properties
        //
        hr = pWiaPropertyStorage->WriteMultiple( c_nPropCount, PropSpec, PropVariant, WIA_IPA_FIRST );
        if (SUCCEEDED(hr))
        {
            //
            // Get the IWiaDataTransfer interface
            //
            IWiaDataTransfer *pWiaDataTransfer = NULL;
            hr = pWiaItem->QueryInterface( IID_IWiaDataTransfer, (void**)&pWiaDataTransfer );
            if (SUCCEEDED(hr))
            {
                //
                // Create our callback class
                //
                CWiaDataCallback *pCallback = new CWiaDataCallback;
                if (pCallback)
                {
                    //
                    // Get the IWiaDataCallback interface from our callback class.
                    //
                    IWiaDataCallback *pWiaDataCallback = NULL;
                    hr = pCallback->QueryInterface( IID_IWiaDataCallback, (void**)&pWiaDataCallback );
                    if (SUCCEEDED(hr))
                    {
                        //
                        // Perform the transfer using default settings
                        //
                        STGMEDIUM stgMedium = {0};
                        StgMedium.tymed = TYMED_FILE;
                        hr = pWiaDataTransfer->idtGetData( &stgMedium, pWiaDataCallback );
                        if (S_OK == hr)
                        {
                            //
                            // Print the filename (note that this filename is always
                            // a WCHAR string, not TCHAR).
                            //
                            _tprintf( TEXT("Transferred filename: %ws\n"), stgMedium.lpszFileName );

                            //
                            // Release any memory associated with the stgmedium
                            // This will delete the file stgMedium.lpszFileName.
                            //
                            ReleaseStgMedium( &stgMedium );
                        }

                        //
                        // Release the callback interface
                        //
                        pWiaDataCallback->Release();
                        pWiaDataCallback = NULL;
                    }

                    //
                    // Release our callback.  It should now delete itself.
                    //
                    pCallback->Release();
                    pCallback = NULL;
                }

                //
                // Release the IWiaDataTransfer
                //
                pWiaDataTransfer->Release();
                pWiaDataTransfer = NULL;
            }
        }

        //
        // Release the IWiaPropertyStorage
        //
        pWiaPropertyStorage->Release();
        pWiaPropertyStorage = NULL;
    }

    return hr;
}