Victor Image Processing Library How-to Tips

Capture an image using webcam




Capture an image using webcam, C# source code
Requires Windows 8 [Windows Store apps only]

This example demonstrates the capture of an image using the computer's webcam. The Windows Camera Capture User Interface does all the work and places the image in a byte array. We use the Victor Library functions to load the image from the byte array into the global image buffer for further processing and display.

Requires Victor Image Processing Library for 32-bit Windows, v 6 or higher and the Victor Library .Net namespace viclib_w8.

        // Invoke the camera interface and capture an image into a byte array.
        // Transfer the image from the byte array to an image buffer for further processing and display.
        static int picno;
        async void b_camera_clicked(object sender, RoutedEventArgs e)
        {
            int rcode;
            vicwin_ws.imgdes cimage = new vicwin_ws.imgdes();
            CameraCaptureUI cameraCap = new CameraCaptureUI();  // CameraCaptureUI does all the camera work
            StorageFile storageFile = await cameraCap.CaptureFileAsync(CameraCaptureUIMode.Photo);
            if (storageFile != null)
            {
                using (IRandomAccessStream stream = await storageFile.OpenReadAsync())
                {
                    using (DataReader dataReader = new DataReader(stream))
                    {
                        uint length = (uint)stream.Size;
                        byte[] imagedata = new byte[length];
                        await dataReader.LoadAsync(length);
                        dataReader.ReadBytes(imagedata);

                        // loadimagefromfilebuffer defined below
                        rcode = loadimagefromfilebuffer(ref cimage, imagedata);
                    }
                }
            {
                // Success, replace global simage, defined in Victor Brightenator Windows Store example
                replace_global_image(ref cimage);   // Success, copy temp to global image
                vicwin_ws.freeimage(ref cimage);    // Free temp cimage
                vfilename = "camerapic" + picno++;
                refresh_image_info();
            }
        }
                                                              
        unsafe public static int loadimagefromfilebuffer(ref vicwin_ws.imgdes timage, byte[] filebuffer)
        {
            int rcode;
            int length, width, vbitcount;
            fixed (byte* fb = &filebuffer[0])
            {
        // Try JPEG
                vicwin_ws.JpegDataEx jdata = new vicwin_ws.JpegDataEx();
                rcode = vicwin_ws.jpeginfofrombufferex(fb, ref jdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = (int)jdata.width;
                    length = (int)jdata.length;
                    vbitcount = jdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadjpgfrombuffer(fb, ref timage);
                    goto done;
                }
        // Try GIF
                vicwin_ws.GifData gdata = new vicwin_ws.GifData();
                rcode = vicwin_ws.gifinfofrombuffer(fb, ref gdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = (int)gdata.width;
                    length = (int)gdata.length;
                    vbitcount = gdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadgiffrombuffer(fb, ref timage);
                    goto done;
                }
        // Try TIFF
                vicwin_ws.TiffData tdata = new vicwin_ws.TiffData();
                rcode = vicwin_ws.tiffinfofrombuffer(fb, ref tdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    // Tiff files require different function to handle the byte array
                    width = (int)tdata.width;
                    length = (int)tdata.length;
                    vbitcount = tdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadtiffrombytearray(filebuffer, ref timage);
                    goto done;
                }
        // Try PNG
                vicwin_ws.PngData pdata = new vicwin_ws.PngData();
                rcode = vicwin_ws.pnginfofrombuffer(fb, ref pdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = (int)pdata.width;
                    length = (int)pdata.length;
                    vbitcount = pdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadpngfrombuffer(fb, ref timage);
                    goto done;
                }
        // Try BMP
                vicwin_ws.BITMAPINFOHEADER bdata = new vicwin_ws.BITMAPINFOHEADER();
                rcode = vicwin_ws.bmpinfofrombuffer(fb, ref bdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = bdata.biWidth;
                    length = bdata.biHeight;
                    vbitcount = bdata.biBitCount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadbmpfrombuffer(fb, ref timage);
                    goto done;
                }
            }
        done:
            return (rcode);
        }
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code




Copyright © 2013 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.