home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Komunik / sambar / sambar51p.exe / samples / source / webcam.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-09  |  7.7 KB  |  307 lines

  1. /*
  2. ** WEBCAM
  3. **
  4. **      HTTP Wrapper for the Video for Windows Driver
  5. **
  6. **        Confidential Property of Tod Sambar
  7. **        (c) Copyright Tod Sambar 1999
  8. **        All rights reserved.
  9. **
  10. **
  11. ** Public Functions:
  12. **
  13. **        webcam_snapshot
  14. **
  15. **
  16. ** History:
  17. ** Chg#    Date    Description                                                Resp
  18. ** ----    -------    -------------------------------------------------------    ----
  19. **         26JUL99    Created                                                    sambar
  20. **
  21. ** Notes:
  22. ** Built using release 6b of the Independent JPEG Group's JPEG software
  23. */
  24.  
  25. #include    <windows.h>
  26. #include    <stdio.h>
  27. #include    <stdlib.h>
  28. #include    <webcam.h>
  29.  
  30. static const char * const cdjpeg_message_table[] = {
  31.     #include "cderror.h"
  32.     NULL
  33. };
  34.  
  35.  
  36. /*
  37. ** WebCam Parameters
  38. */
  39. static SA_RPCPARAM    webcamp [] =
  40. {
  41.     { "outfile",     1,    "WebCam output JPEG file." },
  42.     { "height",     0,    "Snapshot image height." },
  43.     { "width",         0,    "Snapshot image width." },
  44.     { "quality",    0,    "Quality of JPEG compression (0 - 100)." },
  45.     { "vfwdriver",     0,    "VFW driver to use (default is 0)." }
  46. };
  47.  
  48. /*
  49. ** WebCam Defines
  50. */
  51. #define WEBCAM_VFW_DRIVER        0
  52. #define WEBCAM_JPEG_QUALITY        100
  53. #define WEBCAM_BMP_FILE            "capframe.bmp"
  54.  
  55. /*
  56. **  WEBCAM_INIT
  57. **
  58. **    Initialize the WebCAM Interfaces for use by the 
  59. **    Sambar Server plugins.
  60. **
  61. **
  62. **  Parameters:
  63. **    sactx        Sambar Server context
  64. **
  65. **  Returns:
  66. **    SA_SUCCEED | SA_FAIL
  67. */
  68. SA_RETCODE SA_PUBLIC
  69. webcam_init(sactx)
  70. SA_CTX        *sactx;
  71. {
  72.     /* Register the WebCAM RPC with the server                            */
  73.     if (sa_cmd_init(sactx, "webcam_snapshot", webcamp, 
  74.         sizeof(webcamp) / sizeof(SA_RPCPARAM), SA_AUTHORIZATION_ADMIN,
  75.         "Sambar Server VFW WebCam snapshot utility.",
  76.         webcam_snapshot) != SA_SUCCEED)
  77.     {
  78.         sa_log2(sactx, SA_LOG_ERROR, 
  79.             "Unable to initialize WebCam Snapshot RPC");
  80.         return (SA_FAIL);
  81.     }
  82.  
  83.     sa_log2(sactx, SA_LOG_TRACE, "WebCam Snapshot Initialized");
  84.  
  85.     return (SA_SUCCEED);
  86. }
  87.  
  88. /*
  89. **  WEBCAM_SNAPSHOT
  90. **
  91. **    Take a snapshot from the Video for Windows compatible camera and
  92. **    save the JPEG image.  
  93. **
  94. **  Parameters:
  95. **    sactx        Sambar Server context
  96. **    saconn        Sambar Server connection
  97. **    saparams    RPC Parameters
  98. **    infop        Error parameters
  99. **
  100. **  Returns:
  101. **    SA_SUCCEED | SA_FAIL
  102. **
  103. **    Notes:
  104. **    Some of the libraries used by this RPC may result in a Server
  105. **    if an error is experienced.  The JPEG library, in particular,
  106. **    calls exit() rather than gracefully cleaning up errors.
  107. */
  108. SA_RETCODE SA_PUBLIC
  109. webcam_snapshot(sactx, saconn, saparams, infop)
  110. SA_CTX        *sactx;
  111. SA_CONN        *saconn;
  112. SA_PARAMS    *saparams;
  113. SA_INT        *infop;
  114. {
  115.     int                            width;
  116.     int                            height;
  117.     int                            quality;
  118.     int                            vfwdriver;
  119.     SA_INT                        datalen;
  120.     SA_CHAR                        *data;
  121.     HWND                        capwin;
  122.     CAPSTATUS                     capstatus;
  123.     CAPDRIVERCAPS                caps;
  124.       struct jpeg_compress_struct    cinfo;
  125.       struct jpeg_error_mgr         jerr;
  126.       cjpeg_source_ptr             src_mgr;
  127.       FILE                         *infile;
  128.       FILE                         *outfile;
  129.       JDIMENSION                     num_scanlines;
  130.     SA_CHAR                        buffer[256];
  131.  
  132.     /* Initialization                                                    */
  133.     width = 0;
  134.     height = 0;
  135.     vfwdriver = WEBCAM_VFW_DRIVER;
  136.     quality = WEBCAM_JPEG_QUALITY;
  137.     
  138.     if ((sa_param(sactx, saparams, "vfwdriver", &data, &datalen) == SA_SUCCEED)
  139.         && (datalen < 12))
  140.     {
  141.         vfwdriver = atoi(data);
  142.     }
  143.     
  144.     if ((sa_param(sactx, saparams, "quality", &data, &datalen) == SA_SUCCEED)
  145.         && (datalen < 12))
  146.     {
  147.         quality = atoi(data);
  148.     }
  149.  
  150.     if ((sa_param(sactx, saparams, "width", &data, &datalen) == SA_SUCCEED)
  151.         && (datalen < 12))
  152.     {
  153.         width = atoi(data);
  154.     }
  155.  
  156.     if ((sa_param(sactx, saparams, "height", &data, &datalen) == SA_SUCCEED)
  157.         && (datalen < 12))
  158.     {
  159.         height = atoi(data);
  160.     }
  161.     
  162.     if ((sa_param(sactx, saparams, "outfile", &data, &datalen) != SA_SUCCEED)
  163.         || (datalen > 255))
  164.     {
  165.         *infop = SA_E_INVALIDDATA;
  166.         sa_log2(sactx, SA_LOG_WARN,
  167.             "webcam_snapshot(): Expected parameter 'outfile'!");
  168.         return (SA_FAIL);
  169.     }
  170.     
  171.  
  172.     /*
  173.     ** Create the capture window
  174.     */
  175.     capwin = capCreateCaptureWindow("Video Capture Window", 
  176.                 WS_CHILD, 0,0, 0,0, GetDesktopWindow(), 0);
  177.     if (capwin == NULL)
  178.     {
  179.         sa_log2(sactx, SA_LOG_ERROR,
  180.             "webcam_snapshot(): Can't create capture window.");
  181.         return (SA_FAIL);
  182.     }
  183.  
  184.     /* Connect the capture window to the driver                            */
  185.     if (!capDriverConnect(capwin, vfwdriver))
  186.     {
  187.         sprintf(buffer, 
  188.             "webcam_snapshot(): Can't connect to capture driver %d", 
  189.             vfwdriver);
  190.         sa_log2(sactx, SA_LOG_ERROR, buffer);
  191.         return (SA_FAIL);
  192.     }
  193.  
  194.     /*
  195.     ** Display the catpure driver name                                     
  196.     **
  197.     ** capGetDriverDescription(index, buffer, sizeof(buffer), NULL, 0);
  198.     ** fprintf(stdout, "Using capture driver: %s\n", buffer);
  199.     */
  200.  
  201.     /* Get the capabilities of the capture driver                        */
  202.     capDriverGetCaps(capwin, &caps, sizeof(caps));
  203.  
  204.     /* Get the capture window attributes .. width and height             */
  205.     capGetStatus(capwin, &capstatus, sizeof(capstatus));
  206.  
  207.     /* Use the default width and height if none is provided.            */
  208.     if ((width == 0) || (height == 0))
  209.     {
  210.         width = capstatus.uiImageWidth;
  211.         height = capstatus.uiImageHeight;
  212.     }
  213.  
  214.     /* Resize the capture window to the capture sizes                     */
  215.     SetWindowPos(capwin, NULL, 0, 0, width, height, SWP_NOMOVE|SWP_NOZORDER);
  216.  
  217.     /* Get a video frame                                                 */
  218.     capGrabFrameNoStop(capwin);
  219.     capFileSaveDIB(capwin, (LPCTSTR)WEBCAM_BMP_FILE);
  220.     capDriverDisconnect(capwin);
  221.  
  222.       /* 
  223.     ** Initialize the JPEG compression object with default error 
  224.     ** handling. 
  225.     */
  226.       cinfo.err = jpeg_std_error(&jerr);
  227.       jpeg_create_compress(&cinfo);
  228.  
  229.     /* Add application-specific error messages                            */
  230.     jerr.addon_message_table = cdjpeg_message_table;
  231.     jerr.first_addon_message = JMSG_FIRSTADDONCODE;
  232.     jerr.last_addon_message = JMSG_LASTADDONCODE;
  233.  
  234.       /*
  235.     ** Initialize JPEG parameters.
  236.        ** Much of this may be overridden later.
  237.        ** In particular, we don't yet know the input file's color space,
  238.        ** but we need to provide some value for jpeg_set_defaults() to work.
  239.        */
  240.       cinfo.in_color_space = JCS_RGB;
  241.     jpeg_set_defaults(&cinfo);
  242.  
  243.     /* Open the input file.                                             */
  244.     if ((infile = fopen(WEBCAM_BMP_FILE, READ_BINARY)) == NULL) 
  245.     {
  246.         sprintf(buffer, "webcam_snapshot(): Can't open %s", WEBCAM_BMP_FILE);
  247.         sa_log2(sactx, SA_LOG_ERROR, buffer);
  248.         jpeg_destroy_compress(&cinfo);
  249.         return (SA_FAIL);
  250.     }
  251.  
  252.     /* FIX THIS Sambar - build path to jpeg file relative to homedir    */
  253.  
  254.     /* Open the output file.                                             */
  255.     if ((outfile = fopen(data, WRITE_BINARY)) == NULL) 
  256.     {
  257.         sprintf(buffer, "webcam_snapshot(): Can't open %s", data);
  258.         sa_log2(sactx, SA_LOG_ERROR, buffer);
  259.         jpeg_destroy_compress(&cinfo);
  260.         return (SA_FAIL);
  261.     }
  262.  
  263.     /* Figure out the input file format, and set up to read it.         */
  264.     src_mgr = jinit_read_bmp(&cinfo);
  265.     src_mgr->input_file = infile;
  266.  
  267.     /* Read the input file header to obtain file size & colorspace.     */
  268.       (*src_mgr->start_input) (&cinfo, src_mgr);
  269.  
  270.     /* 
  271.     ** Now that we know input colorspace, fix colorspace-dependent defaults
  272.     */
  273.     jpeg_default_colorspace(&cinfo);
  274.  
  275.     /* Adjust the compression density                                     */
  276.     if (quality < 0)
  277.         quality = 0;
  278.     else if (quality > 100)
  279.         quality = 100;
  280.  
  281.     jpeg_set_quality(&cinfo, quality, TRUE);
  282.  
  283.     /* Specify data destination for compression                         */
  284.     jpeg_stdio_dest(&cinfo, outfile);
  285.  
  286.     /* Start compressor                                                 */
  287.     jpeg_start_compress(&cinfo, TRUE);
  288.  
  289.     /* Process data                                                     */
  290.     while (cinfo.next_scanline < cinfo.image_height) 
  291.     {
  292.         num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
  293.         (void)jpeg_write_scanlines(&cinfo, src_mgr->buffer, 
  294.             num_scanlines);
  295.     }
  296.  
  297.     /* Finish compression and release memory                             */
  298.     (*src_mgr->finish_input) (&cinfo, src_mgr);
  299.     jpeg_finish_compress(&cinfo);
  300.     jpeg_destroy_compress(&cinfo);
  301.  
  302.     fclose(infile);
  303.     fclose(outfile);
  304.  
  305.     return (SA_SUCCEED);
  306. }
  307.