HRESULT WIACamera::SetDeviceProperty(const unsigned int CamID, const PropertyType Type, const int iValue, bool bAuto)
{
	HRESULT hr = S_OK;
	if ( CamID >= m_iCount ) return S_FALSE;

	IWiaPropertyStorage *pWiaPropertyStorage;
	hr = m_vRootItem[CamID]->QueryInterface(IID_IWiaPropertyStorage,(void**)&pWiaPropertyStorage);
	if (hr != S_OK) {
		return E_NOINTERFACE;
	}

	PROPSPEC PropSpec;
	PROPVARIANT PropVar;
	PropSpec.ulKind = PRSPEC_PROPID;
	PropVar.vt = VT_I4;

	if ( Type==CAMERA_SHUTTERSPEED ) {
		// shutter speed変更の際には,撮影モード変更後,値を設定
		long lResult;
		PropSpec.propid = WIA_DPC_EXPOSURE_MODE;
		hr = ReadPropertyLong( pWiaPropertyStorage, &PropSpec, &lResult);

		if (bAuto) {
			// EXPOSUREMODE_MANUAL	-> EXPOSUREMODE_APERTURE_PRIORITY
			// EXPOSUREMODE_SHUTTER_PRIORITY	-> EXPOSUREMODE_AUTO
			// OTHER : NOTHING 
			if ( lResult==EXPOSUREMODE_MANUAL ) {
				PropVar.lVal = EXPOSUREMODE_APERTURE_PRIORITY;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			} else if ( lResult==EXPOSUREMODE_SHUTTER_PRIORITY ) {
				PropVar.lVal = EXPOSUREMODE_AUTO;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			}
		} else {
			// EXPOSUREMODE_MANUAL	<- EXPOSUREMODE_APERTURE_PRIORITY
			// EXPOSUREMODE_SHUTTER_PRIORITY	<- EXPOSUREMODE_AUTO
			// OTHER : NOTHING 
			if ( lResult==EXPOSUREMODE_APERTURE_PRIORITY ) {
				PropVar.lVal = EXPOSUREMODE_MANUAL;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			} else if ( lResult==EXPOSUREMODE_AUTO ) {
				PropVar.lVal = EXPOSUREMODE_SHUTTER_PRIORITY;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			}
			//	then Set Value.
			PropSpec.propid = WIA_DPC_EXPOSURE_TIME;
			PropVar.lVal = iValue;
			hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
		}
	} else if ( Type==CAMERA_APERTURE ) {
		// 絞り変更の際には,撮影モード変更後,値を設定
		long lResult;
		PropSpec.propid = WIA_DPC_EXPOSURE_MODE;
		hr = ReadPropertyLong( pWiaPropertyStorage, &PropSpec, &lResult);

		if (bAuto) {
			// EXPOSUREMODE_MANUAL	-> EXPOSUREMODE_SHUTTER_PRIORITY
			// EXPOSUREMODE_APERTURE_PRIORITY	-> EXPOSUREMODE_AUTO
			// OTHER : NOTHING 
			if ( lResult==EXPOSUREMODE_MANUAL ) {
				PropVar.lVal = EXPOSUREMODE_SHUTTER_PRIORITY;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			} else if ( lResult==EXPOSUREMODE_APERTURE_PRIORITY ) {
				PropVar.lVal = EXPOSUREMODE_AUTO;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			}
		} else {
			// EXPOSUREMODE_MANUAL	<- EXPOSUREMODE_SHUTTER_PRIORITY
			// EXPOSUREMODE_APERTURE_PRIORITY	<- EXPOSUREMODE_AUTO
			// OTHER : NOTHING 
			if ( lResult==EXPOSUREMODE_SHUTTER_PRIORITY ) {
				PropVar.lVal = EXPOSUREMODE_MANUAL;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			} else if ( lResult==EXPOSUREMODE_AUTO ) {
				PropVar.lVal = EXPOSUREMODE_APERTURE_PRIORITY;
				hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
			}
			//	then Set Value.
			PropSpec.propid = WIA_DPC_FNUMBER;
			PropVar.lVal = iValue;
			hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
		}
	} else if ( Type==CAMERA_GAIN ) {
		PropSpec.propid = WIA_DPC_EXPOSURE_INDEX;
		if (bAuto) {
			PropVar.lVal = 0xFFFF;
		} else {
			PropVar.lVal = iValue;
		}
		hr = pWiaPropertyStorage->WriteMultiple( 1, &PropSpec, &PropVar, 2 );
	} else {
		hr = E_NOTIMPL;
	}

	if (pWiaPropertyStorage) {
		pWiaPropertyStorage->Release();
		pWiaPropertyStorage = NULL;
	}

	return hr;
}
Example #2
0
HRESULT 
WiaGetImage(
    HWND                 hWndParent,
    LONG                 lDeviceType,
    LONG                 lFlags,
    LONG                 lIntent,
    IWiaDevMgr          *pSuppliedWiaDevMgr,
    IWiaItem            *pSuppliedItemRoot,
    PFNPROGRESSCALLBACK  pfnProgressCallback,
    PVOID                pProgressCallbackParam,
    GUID                *pguidFormat,
    LONG                *plCount,
    IStream             ***pppStream
)
{
    HRESULT hr;

    // Validate and initialize output parameters

    if (plCount == NULL)
    {
        return E_POINTER;
    }

    if (pppStream == NULL)
    {
        return E_POINTER;
    }

    *plCount = 0;
    *pppStream = NULL;

    // Initialize the local root item variable with the supplied value.
    // If no value is supplied, display the device selection common dialog.

    CComPtr<IWiaItem> pItemRoot = pSuppliedItemRoot;

    if (pItemRoot == NULL)
    {
        // Initialize the device manager pointer with the supplied value
        // If no value is supplied, connect to the local device manager

        CComPtr<IWiaDevMgr> pWiaDevMgr = pSuppliedWiaDevMgr;

        if (pWiaDevMgr == NULL)
        {
            hr = pWiaDevMgr.CoCreateInstance(CLSID_WiaDevMgr);

            if (FAILED(hr))
            {
                return hr;
            }
        }
    
        // Display the device selection common dialog

        hr = pWiaDevMgr->SelectDeviceDlg(
            hWndParent,
            lDeviceType,
            lFlags,
            0,
            &pItemRoot
        );

        if (FAILED(hr) || hr == S_FALSE)
        {
            return hr;
        }
    }

    // Display the image selection common dialog 

    CComPtrArray<IWiaItem> ppIWiaItem;

    hr = pItemRoot->DeviceDlg(
        hWndParent,
        lFlags,
        lIntent,
        &ppIWiaItem.Count(),
        &ppIWiaItem
    );

    if (FAILED(hr) || hr == S_FALSE)
    {
        return hr;
    }

    // For ADF scanners, the common dialog explicitly sets the page count to one.
    // So in order to transfer multiple images, set the page count to ALL_PAGES
    // if the WIA_DEVICE_DIALOG_SINGLE_IMAGE flag is not specified, 

    if (!(lFlags & WIA_DEVICE_DIALOG_SINGLE_IMAGE))
    {
        // Get the property storage interface pointer for the root item

        CComQIPtr<IWiaPropertyStorage> pWiaRootPropertyStorage(pItemRoot);

        if (pWiaRootPropertyStorage == NULL)
        {
            return E_NOINTERFACE;
        }

        // Determine if the selected device is a scanner or not

        PROPSPEC specDevType;

        specDevType.ulKind = PRSPEC_PROPID;
        specDevType.propid = WIA_DIP_DEV_TYPE;

        LONG nDevType;

        hr = ReadPropertyLong(
            pWiaRootPropertyStorage, 
            &specDevType, 
            &nDevType
        );

        if (SUCCEEDED(hr) && (GET_STIDEVICE_TYPE(nDevType) == StiDeviceTypeScanner))
        {
            // Determine if the document feeder is selected or not

            PROPSPEC specDocumentHandlingSelect;

            specDocumentHandlingSelect.ulKind = PRSPEC_PROPID;
            specDocumentHandlingSelect.propid = WIA_DPS_DOCUMENT_HANDLING_SELECT;

            LONG nDocumentHandlingSelect;

            hr = ReadPropertyLong(
                pWiaRootPropertyStorage, 
                &specDocumentHandlingSelect, 
                &nDocumentHandlingSelect
            );

            if (SUCCEEDED(hr) && (nDocumentHandlingSelect & FEEDER))
            {
                PROPSPEC specPages;

                specPages.ulKind = PRSPEC_PROPID;
                specPages.propid = WIA_DPS_PAGES;

                PROPVARIANT varPages;
                    
                varPages.vt = VT_I4;
                varPages.lVal = ALL_PAGES;

                pWiaRootPropertyStorage->WriteMultiple(
                    1,
                    &specPages,
                    &varPages,
                    WIA_DPS_FIRST
                );
                
                PropVariantClear(&varPages);
            }
        }
    }

    // If a status callback function is not supplied, use the default.
    // The default displays a simple dialog with a progress bar and cancel button.

    CComPtr<CProgressDlg> pProgressDlg;

    if (pfnProgressCallback == NULL)
    {
        pfnProgressCallback = DefaultProgressCallback;

        pProgressDlg = new CProgressDlg(hWndParent);

        pProgressCallbackParam = (CProgressDlg *) pProgressDlg;
    }

    // Create the data callback interface

    CComPtr<CDataCallback> pDataCallback = new CDataCallback(
        pfnProgressCallback,
        pProgressCallbackParam,
        plCount, 
        pppStream
    );

    if (pDataCallback == NULL)
    {
        return E_OUTOFMEMORY;
    }

    // Start the transfer of the selected items

    for (int i = 0; i < ppIWiaItem.Count(); ++i)
    {
        // Get the interface pointers

        CComQIPtr<IWiaPropertyStorage> pWiaPropertyStorage(ppIWiaItem[i]);

        if (pWiaPropertyStorage == NULL)
        {
            return E_NOINTERFACE;
        }

        CComQIPtr<IWiaDataTransfer> pIWiaDataTransfer(ppIWiaItem[i]);

        if (pIWiaDataTransfer == NULL)
        {
            return E_NOINTERFACE;
        }

        // Set the transfer type

        PROPSPEC specTymed;

        specTymed.ulKind = PRSPEC_PROPID;
        specTymed.propid = WIA_IPA_TYMED;

        PROPVARIANT varTymed;

        varTymed.vt = VT_I4;
        varTymed.lVal = TYMED_CALLBACK;

        hr = pWiaPropertyStorage->WriteMultiple(
            1,
            &specTymed,
            &varTymed,
            WIA_IPA_FIRST
        );

        PropVariantClear(&varTymed);

        if (FAILED(hr))
        {
            return hr;
        }

        // If there is no transfer format specified, use the device default

        GUID guidFormat = GUID_NULL;

        if (pguidFormat == NULL)
        {
            pguidFormat = &guidFormat;
        }

        if (*pguidFormat == GUID_NULL)
        {
            PROPSPEC specPreferredFormat;

            specPreferredFormat.ulKind = PRSPEC_PROPID;
            specPreferredFormat.propid = WIA_IPA_PREFERRED_FORMAT;

            hr = ReadPropertyGuid(
                pWiaPropertyStorage,
                &specPreferredFormat,
                pguidFormat
            );

            if (FAILED(hr))
            {
                return hr;
            }
        }

        // Set the transfer format

        PROPSPEC specFormat;

        specFormat.ulKind = PRSPEC_PROPID;
        specFormat.propid = WIA_IPA_FORMAT;

        PROPVARIANT varFormat;

        varFormat.vt = VT_CLSID;
        varFormat.puuid = (CLSID *) CoTaskMemAlloc(sizeof(CLSID));

        if (varFormat.puuid == NULL)
        {
            return E_OUTOFMEMORY;
        }

        *varFormat.puuid = *pguidFormat;

        hr = pWiaPropertyStorage->WriteMultiple(
            1,
            &specFormat,
            &varFormat,
            WIA_IPA_FIRST
        );

        PropVariantClear(&varFormat);

        if (FAILED(hr))
        {
            return hr;
        }

        // Read the transfer buffer size from the device, default to 64K

        PROPSPEC specBufferSize;

        specBufferSize.ulKind = PRSPEC_PROPID;
        specBufferSize.propid = WIA_IPA_BUFFER_SIZE;

        LONG nBufferSize;

        hr = ReadPropertyLong(
            pWiaPropertyStorage, 
            &specBufferSize, 
            &nBufferSize
        );

        if (FAILED(hr))
        {
            nBufferSize = 64 * 1024;
        }

        // Choose double buffered transfer for better performance

        WIA_DATA_TRANSFER_INFO WiaDataTransferInfo = { 0 };

        WiaDataTransferInfo.ulSize        = sizeof(WIA_DATA_TRANSFER_INFO);
        WiaDataTransferInfo.ulBufferSize  = 2 * nBufferSize;
        WiaDataTransferInfo.bDoubleBuffer = TRUE;

        // Start the transfer

        hr = pIWiaDataTransfer->idtGetBandedData(
            &WiaDataTransferInfo,
            pDataCallback
        );

        if (FAILED(hr) || hr == S_FALSE)
        {
            return hr;
        }
    }

    return S_OK;
}
HRESULT WIACamera::DigitalCameraCapture(IWiaItem* pIWiaRoot, IplImage** ppIplImage)
{
	HRESULT hr = S_OK;

	IWiaItem* pIWiaItem = NULL;
	hr = pIWiaRoot->DeviceCommand( 0, &WIA_CMD_TAKE_PICTURE, &pIWiaItem );

	if (pIWiaItem==NULL) {
		cvReleaseImage(ppIplImage);
		(*ppIplImage) = NULL;
		return hr;
	}
	IStream **ppStream = NULL;
	LONG	lCount = 0;
	// Create the data callback interface
	CDataCallback *pDataCallback = new CDataCallback( &lCount,&ppStream );
	if (pDataCallback == NULL) {
		return E_OUTOFMEMORY;
	}

	{
		// Get the interface pointers
		IWiaPropertyStorage *pWiaPropertyStorage;
		hr = pIWiaItem->QueryInterface(IID_IWiaPropertyStorage,(void**)&pWiaPropertyStorage);
		if (hr != S_OK) {
			return E_NOINTERFACE;
		}

		IWiaDataTransfer *pIWiaDataTransfer;
		hr = pIWiaItem->QueryInterface(IID_IWiaDataTransfer, (void**)&pIWiaDataTransfer);
		if (hr != S_OK) {
			return E_NOINTERFACE;
		}

		// Set the transfer type
		PROPSPEC specTymed;
		specTymed.ulKind = PRSPEC_PROPID;
		specTymed.propid = WIA_IPA_TYMED;

		PROPVARIANT varTymed;
		varTymed.vt = VT_I4;
		varTymed.lVal = TYMED_CALLBACK;

		hr = pWiaPropertyStorage->WriteMultiple(1, &specTymed, &varTymed, WIA_IPA_FIRST );
		PropVariantClear(&varTymed);
		if (FAILED(hr)) {
			return hr;
		}

		// If there is no transfer format specified, use the device default
		GUID guidFormat = GUID_NULL;
		GUID *pguidFormat = &guidFormat;
		PROPSPEC specPreferredFormat;

		specPreferredFormat.ulKind = PRSPEC_PROPID;
		specPreferredFormat.propid = WIA_IPA_PREFERRED_FORMAT;

		hr = ReadPropertyGuid( pWiaPropertyStorage, &specPreferredFormat, pguidFormat );
		if (FAILED(hr))	{
			return hr;
		}

		// Set the transfer format
		PROPSPEC specFormat;
		PROPVARIANT varFormat;
		specFormat.ulKind = PRSPEC_PROPID;
		specFormat.propid = WIA_IPA_FORMAT;
		varFormat.vt = VT_CLSID;
		varFormat.puuid = (CLSID *) CoTaskMemAlloc(sizeof(CLSID));
		if (varFormat.puuid == NULL) {
			return E_OUTOFMEMORY;
		}
		*varFormat.puuid = *pguidFormat;
		hr = pWiaPropertyStorage->WriteMultiple( 1, &specFormat, &varFormat, WIA_IPA_FIRST );
		PropVariantClear(&varFormat);
		if (FAILED(hr)) {
			return hr;
		}

		// Read the transfer buffer size from the device, default to 64K
		PROPSPEC specBufferSize;
		specBufferSize.ulKind = PRSPEC_PROPID;
		specBufferSize.propid = WIA_IPA_BUFFER_SIZE;
		LONG nBufferSize;
		hr = ReadPropertyLong( pWiaPropertyStorage, &specBufferSize, &nBufferSize );
		if (FAILED(hr)) {
			nBufferSize = 64 * 1024;
		}

		// Choose double buffered transfer for better performance
		WIA_DATA_TRANSFER_INFO WiaDataTransferInfo = { 0 };
		WiaDataTransferInfo.ulSize        = sizeof(WIA_DATA_TRANSFER_INFO);
		WiaDataTransferInfo.ulBufferSize  = 2 * nBufferSize;
		WiaDataTransferInfo.bDoubleBuffer = TRUE;

		// Start the transfer
		hr = pIWiaDataTransfer->idtGetBandedData( &WiaDataTransferInfo, pDataCallback );

		if (pWiaPropertyStorage) {
			pWiaPropertyStorage->Release();
			pWiaPropertyStorage = NULL;
		}
		if (pIWiaDataTransfer) {
			pIWiaDataTransfer->Release();
			pIWiaDataTransfer = NULL;
		}
	}
	if (pIWiaItem) {
		// Delete file from DigitalCamera storage
		pIWiaItem->DeleteItem(0);
		pIWiaItem->Release();
		pIWiaItem = NULL;
	}

	if (lCount!=1) throw "Error.\n"; 

	if ( SUCCEEDED(hr) ) { 
		Gdiplus::Bitmap myBmp(ppStream[0]);
		int Width = myBmp.GetWidth();
		int Height = myBmp.GetHeight();
		Gdiplus::Rect rect(0, 0, Width, Height);
		Gdiplus::BitmapData myBitmapData;
		Gdiplus::Status res = 
			myBmp.LockBits(
			&rect, Gdiplus::ImageLockModeRead,
			PixelFormat24bppRGB, &myBitmapData
		);
		if ( (*ppIplImage)==NULL ) {
			(*ppIplImage) = cvCreateImage(cvSize(Width,Height),IPL_DEPTH_8U,3);
		} else {
			CvSize oldSize = cvGetSize((*ppIplImage));
			if ( oldSize.width!=Width || oldSize.height!=Height ) {
				throw "Warning.\n";
				cvReleaseImage(&(*ppIplImage));
				(*ppIplImage) = cvCreateImage(cvSize(Width,Height),IPL_DEPTH_8U,3);
			}
		}

		unsigned char *pIplData = (unsigned char*)(*ppIplImage)->imageData;
		for ( int h=0; h < Height; h++) {
			unsigned char *pIplLine = &pIplData[(*ppIplImage)->widthStep*h];
			unsigned char *pBitmapLine = &((unsigned char*)myBitmapData.Scan0)[myBitmapData.Stride*h];
			memcpy( pIplLine, pBitmapLine, sizeof(unsigned char)*Width*3 );
			/*
			for ( int w=0; w < Width; w++) {
				pIplLine[w*3+0] = pBitmapLine[w*3+2];
				pIplLine[w*3+1] = pBitmapLine[w*3+1];
				pIplLine[w*3+2] = pBitmapLine[w*3+0];
			}
			*/
		}

	} else {
		delete (*ppIplImage);
		(*ppIplImage) = NULL;
	}
	ppStream[0]->Release();

	return hr;
}