VFWImage.h.cpp

Go to the documentation of this file.
00001 // ************************************************************************
00002 //      class CbmpUtils
00003 //      Can capture bitmap from camera, display and save it.
00004 //      Camera functionality provided by separate CVFWCapture class.
00005 //  Author:     Audrey J. W. Mbogho
00006 //      Email:  walegwa AT yahoo DOT com
00007 //      Last Modified: January 2005
00008 //      Previously, I tried to give credit here to all the
00009 //      other people from whom I "borrowed" code. But that list
00010 //      has grown to be too long for that. Apologies to all!
00011 //      Environment: Visual C++, Windows 98, Logitech Webcam 4000 Pro.
00012 // ************************************************************************
00013 
00014 #include <fstream.h>
00015 #include "VFWImage.h"
00016 #include "VFWCapture.h"
00017 
00018 // Constructor
00019 // Initializes object data
00020 CbmpUtils::CbmpUtils()
00021 {
00022         Width = 0;
00023         Height = 0;             
00024         bmpData = '\0';         
00025         pbmi = NULL;    
00026         BitmapSize = 0;
00027 }
00028 
00029 CbmpUtils::~CbmpUtils()
00030 {
00031 }
00032 
00033 // LoadBMP();
00034 // Loads a DIB captured from video camera into a CbmpUtils object
00035 int CbmpUtils::LoadBMP()
00036 {
00037         BITMAPINFOHEADER bmih;  // Contained in BITMAPINFO structure
00038 
00039         CVFWCapture cap;
00040         cap.Initialize();       // Initialize first found VFW device
00041 
00042         pbmi = NULL;    // CaptureDIB will automatically allocate this if set to NULL
00043 
00044         // Capture an image from the capture device.
00045         if (cap.CaptureDIB(&pbmi, 0, &BitmapSize))
00046         {
00047                 // Obtain args for SetDIBitsToDevice
00048                 bmih = pbmi->bmiHeader;
00049                 Width = bmih.biWidth;
00050                 Height = bmih.biHeight;
00051                 Height = (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight; // absolute value
00052                 bmpData = (char *)pbmi;
00053 
00054                 bmpData += cap.CalcBitmapInfoSize(bmih);
00055         }
00056         cap.Destroy();  // Done using VFW object
00057         return 0;
00058 }
00059 
00060 
00061 // CbmpUtils::GDIPaint (hdc,x,y);
00062 // Paints bitmap to a Windows DC
00063 int CbmpUtils::GDIPaint (HDC hdc,int x=0,int y=0)
00064 {
00065         int ret = 0;
00066         // Paint the image to the device.
00067         ret = SetDIBitsToDevice (hdc,x,y,Width,Height,0,0,
00068                 0,Height,(LPVOID)bmpData,pbmi,0);
00069         return ret;
00070 }
00071 
00072 
00073 // SaveBMP()
00074 // Saves current bitmap to file
00075 int CbmpUtils::SaveBMP(LPCSTR fileName)
00076 {
00077         BITMAPFILEHEADER bfh;
00078 
00079         if (!pbmi)
00080                 return 1;                       // bitmap is empty
00081 
00082         ofstream bmpFile(fileName, ios::out | ios::binary);
00083         if (bmpFile.is_open())
00084         {
00085                 bfh.bfType = 0x4d42;    // 0x42 = "B" 0x4d = "M"
00086                 bfh.bfSize = (DWORD) BitmapSize + sizeof(BITMAPFILEHEADER);
00087                 bfh.bfOffBits = (DWORD)   sizeof(BITMAPFILEHEADER) +
00088                                    sizeof(BITMAPINFOHEADER) +
00089                                     pbmi->bmiHeader.biClrUsed * 
00090                 sizeof (RGBQUAD);
00091                 bfh.bfReserved1 = 0;
00092                 bfh.bfReserved2 = 0;
00093 
00094                 bmpFile.write(reinterpret_cast<const char *>(&bfh),sizeof(bfh));
00095                 bmpFile.write(reinterpret_cast<const char *>(pbmi),BitmapSize);
00096                 bmpFile.close();
00097         }
00098         return 0;
00099 }

Generated on Wed Nov 29 01:27:42 2006 by  doxygen 1.4.6