silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了教你如何用Silverlight调用摄像头和麦克风,拍照,保存照片大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

教你如何用Silverlight调用摄像头和麦克风,拍照,保存照片 在这个新的Silverlight一节中我会告诉你如何使用新的摄像头API来创建一个非常酷的摄像头应用程序,允许你运行你的摄像头,采取有趣的物体,如帽子快照图像,放在你的脸上,采取快照,然后保存图像到硬盘驱动器。 1. PNG格式操作类,图像保存为PHG格式.PngEncoder.cs /// <sumMary>     /// P

教你如何用Silverlight调用摄像头和麦克风,拍照,保存照片

在这个新的Silverlight一节中我会告诉你如何使用新的摄像头API来创建一个非常酷的摄像头应用程序,允许你运行你的摄像头,采取有趣的物体,如帽子快照图像,放在你的脸上,采取快照,然后保存图像到硬盘驱动器。

1. PNG格式操作类,图像保存为PHG格式.PngEncoder.cs

/// <sumMary>
    /// PNG格式操作类
    /// </sumMary>
    public class PngEncoder
    {
        private const int _ADLER32_BASE = 65521;
        private const int _MAXBLOCK = 0xFFFF;
        private static byte[] _HEADER = { 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A };
        private static byte[] _IHDR = { (bytE)'I',(bytE)'H',(bytE)'D',(bytE)'R' };
        private static byte[] _GAMA = { (bytE)'g',(bytE)'A',(bytE)'M',(bytE)'A' };
        private static byte[] _IDAT = { (bytE)'I',(bytE)'T' };
        private static byte[] _IEND = { (bytE)'I',(bytE)'E',(bytE)'N',(bytE)'D' };
        private static byte[] _4BYTEDATA = { 0,0 };
        private static byte[] _ARGB = { 0,8,6,0 };

        /// <sumMary>
        /// 编码
        /// </sumMary>
        /// <param name="data"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Stream Encode(byte[] data,int width,int height)
        {
            MemoryStream ms = new MemoryStream();
            byte[] size;

            // Write PNG header
            ms.Write(_HEADER,_HEADER.Length);

            // Write IHDR
            //  Width:              4 bytes
            //  Height:             4 bytes
            //  Bit depth:          1 byte
            //  Color type:         1 byte
            //  Compression method: 1 byte
            //  Filter method:      1 byte
            //  Interlace method:   1 byte

            size = BitConverter.GetBytes(width);
            _ARGB[0] = size[3]; _ARGB[1] = size[2]; _ARGB[2] = size[1]; _ARGB[3] = size[0];

            size = BitConverter.GetBytes(height);
            _ARGB[4] = size[3]; _ARGB[5] = size[2]; _ARGB[6] = size[1]; _ARGB[7] = size[0];

            // Write IHDR chunk
            WriteChunk(ms,_IHDR,_ARGB);

            // Set gAMMa = 1
            size = BitConverter.GetBytes(1 * 100000);
            _4BYTEDATA[0] = size[3]; _4BYTEDATA[1] = size[2]; _4BYTEDATA[2] = size[1]; _4BYTEDATA[3] = size[0];

            // Write gAMA chunk
            WriteChunk(ms,_GAMA,_4BYTEDATA);

            // Write IDAT chunk
            uint widthLength = (uint)(width * 4) + 1;
            uint dcSize = widthLength * (uint)height;

            // First part of ZLIB header is 78 1101 1010 (DA) 0000 00001 (01)
            // ZLIB info
            //
            // CMF Byte: 78
            //  CINFO = 7 (32K window sizE)
            //  CM = 8 = (deflate compression)
            // FLG Byte: DA
            //  FLEVEL = 3 (bits 6 and 7 - ignored but signifies max compression)
            //  FDicT = 0 (bit 5,0 - no preset Dictionary)
            //  FCHCK = 26 (bits 0-4 - ensure CMF*256+FLG / 31 has no remainder)
            // Compressed data
            //  FLAGS: 0 or 1
            //    00000 00 (no compression) X (X=1 for last block,0=not the last block)
            //    LEN = length in bytes (equal to ((width*4)+1)*height
            //    nLEN = one's compliment of LEN
            //    Example: 1111 1011 1111 1111 (FB),0000 0100 0000 0000 (40)
            //    Data for each line: 0 [RGBA] [RGBA] [RGBA] ...
            //    ADLER32

            uint adler = ComputeAdler32(data);
            MemoryStream comp = new MemoryStream();

            // 64K的块数计算
            uint rowsPerBlock = _MAXBLOCK / widthLength;
            uint blockSize = rowsPerBlock * widthLength;
            uint blockCount;
            ushort length;
            uint remainder = dcSize;

            if ((dcSize % blockSizE) == 0)
            {
                blockCount = dcSize / blockSize;
            }
            else
            {
                blockCount = (dcSize / blockSizE) + 1;
            }

            // 头部
            comp.WriteByte(0x78);
            comp.WriteByte(0xDA);

            for (uint blocks = 0; blocks < blockCount; blocks++)
            {
                // 长度
                length = (ushort)((remainder < blockSizE) ? remainder : blockSizE);

                if (length == remainder)
                {
                    comp.WriteByte(0x01);
                }
                else
                {
                    comp.WriteByte(0x00);
                }

                comp.Write(BitConverter.GetBytes(length),2);

                comp.Write(BitConverter.GetBytes((ushort)~length),2);

                // Write 块
                comp.Write(data,(int)(blocks * blockSizE),length);

                //下一块
                remainder -= blockSize;
            }

            WriteReversedBuffer(comp,BitConverter.GetBytes(adler));
            comp.Seek(0,SeekOrigin.begin);

            byte[] dat = new byte[comp.Length];
            comp.Read(dat,(int)comp.Length);

            WriteChunk(ms,_IDAT,dat);

            // Write IEND chunk
            WriteChunk(ms,_IEND,new byte[0]);

            // Reset stream
            ms.Seek(0,SeekOrigin.begin);

            return ms;
        }

        private static void WriteReversedBuffer(Stream stream,byte[] data)
        {
            int size = data.Length;
            byte[] reorder = new byte[size];

            for (int idx = 0; idx < size; idx++)
            {
                reorder[idx] = data[size - idx - 1];
            }
            stream.Write(reorder,sizE);
        }

        private static void WriteChunk(Stream stream,byte[] type,byte[] data)
        {
            int idx;
            int size = type.Length;
            byte[] buffer = new byte[type.Length + data.Length];

            // 初始化缓冲
            for (idx = 0; idx < type.Length; idx++)
            {
                buffer[idx] = type[idx];
            }

            for (idx = 0; idx < data.Length; idx++)
            {
                buffer[idx + size] = data[idx];
            }

            WriteReversedBuffer(stream,BitConverter.GetBytes(data.Length));

            // Write 类型和数据
            stream.Write(buffer,buffer.Length);   // Should always be 4 bytes

            // 计算和书写的CRC

            WriteReversedBuffer(stream,BitConverter.GetBytes(GetCRC(buffer)));
        }

        private static uint[] _crcTable = new uint[256];
        private static bool _crcTableComputed = false;

        private static void MakeCRCTable()
        {
            uint c;

            for (int n = 0; n < 256; n++)
            {
                c = (uint)n;
                for (int k = 0; k < 8; k++)
                {
                    if ((c & (0x00000001)) > 0)
                        c = 0xEDB88320 ^ (c >> 1);
                    else
                        c = c >> 1;
                }
                _crcTable[n] = c;
            }

            _crcTableComputed = true;
        }

        private static uint updateCRC(uint crc,byte[] buf,int len)
        {
            uint c = crc;

            if (!_crcTableComputed)
            {
                MakeCRCTable();
            }

            for (int n = 0; n < len; n++)
            {
                c = _crcTable[(c ^ buf[n]) & 0xFF] ^ (c >> 8);
            }

            return c;
        }

        //返回的字节的CRC缓冲区
        private static uint GetCRC(byte[] buf)
        {
            return updateCRC(0xFFFFFFFF,buf,buf.Length) ^ 0xFFFFFFFF;
        }

        private static uint ComputeAdler32(byte[] buf)
        {
            uint s1 = 1;
            uint s2 = 0;
            int length = buf.Length;

            for (int idx = 0; idx < length; idx++)
            {
                s1 = (s1 + (uint)buf[idx]) % _ADLER32_BASE;
                s2 = (s2 + s1) % _ADLER32_BASE;
            }

            return (s2 << 16) + s1;
        }
    }

2.图片处理.EditableImage.cs

  /// <sumMary>
    /// 编辑图片
    /// </sumMary>
    public class EditableImage
    {
        privat@R_944_9473@ _width = 0;
        privat@R_944_9473@ _height = 0;
        private bool _init = false;
        private byte[] _buffer;
        privat@R_944_9473@ _rowLength;

        /// <sumMary>
        /// 当图片错误时引发
        /// </sumMary>
        public event EventHandler<EditableImageErrorEventArgs> ImageError;

        /// <sumMary>
        /// 实例化
        /// </sumMary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public EditableImage(int width,int height)
        {
            this.Width = width;
            this.Height = height;
        }

        public int Width
        {
            get
            {
                return _width;
            }
            set
            {
                if (_init)
                {
                    OnImageError("错误: 图片初始化后不可以改变宽度");
                }
                else if ((value <= 0) || (value > 2047))
                {
                    OnImageError("错误: 宽度必须在 0 到 2047");
                }
                else
                {
                    _width = value;
                }
            }
        }

        public int Height
        {
            get
            {
                return _height;
            }
            set
            {
                if (_init)
                {
                    OnImageError("错误: 图片初始化后不可以改变高度");
                }
                else if ((value <= 0) || (value > 2047))
                {
                    OnImageError("错误: 高度必须在 0 到 2047");
                }
                else
                {
                    _height = value;
                }
            }
        }

        public void SetPixel(int col,int row,Color color)
        {
            SetPixel(col,row,color.R,color.G,color.b,color.A);
        }

        public void SetPixel(int col,byte red,byte green,byte blue,byte alpha)
        {
            if (!_init)
            {
                _rowLength = _width * 4 + 1;
                _buffer = new byte[_rowLength * _height];

                // Initialize
                for (int idx = 0; idx < _height; idx++)
                {
                    _buffer[idx * _rowLength] = 0;      // Filter bit
                }

                _init = true;
            }

            if ((col > _width) || (col < 0))
            {
                OnImageError("Error: column must be greater than 0 and less than the Width");
            }
            else if ((row > _height) || (row < 0))
            {
                OnImageError("Error: Row must be greater than 0 and less than the Height");
            }

            // Set the pixel
            int start = _rowLength * row + col * 4 + 1;
            _buffer[start] = red;
            _buffer[start + 1] = green;
            _buffer[start + 2] = blue;
            _buffer[start + 3] = alpha;
        }

        public Col@R_874_6356@Pixel(int col,int row)
        {
            if ((col > _width) || (col < 0))
            {
                OnImageError("Error: column must be greater than 0 and less than the Width");
            }
            else if ((row > _height) || (row < 0))
            {
                OnImageError("Error: Row must be greater than 0 and less than the Height");
            }

            Color color = new Color();
            int _base = _rowLength * row + col + 1;

            color.R = _buffer[_base];
            color.G = _buffer[_base + 1];
            color.b = _buffer[_base + 2];
            color.A = _buffer[_base + 3];

            return color;
        }

        public Stream GetStream()
        {
            Stream stream;

            if (!_init)
            {
                OnImageError("Error: Image has not been initialized");
                stream = null;
            }
            else
            {
                stream = PngEncoder.Encode(_buffer,_width,_height);
            }

            return stream;
        }

        private void OnImageError(String msg)
        {
            if (null != ImageError)
            {
                EditableImageErrorEventArgs args = new EditableImageErrorEventArgs();
                args.Errormessage = msg;
                ImageError(this,args);
            }
        }

        public class EditableImageErrorEventArgs : EventArgs
        {
            private String _errormessage = String.Empty;

            public String Errormessage
            {
                get { return _errormessage; }
                set { _errormessage = value; }
            }
        }

    }

3.应用

前台

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="WeccamDemo.MainPage"
Width="640" Height="480" mc:Ignorable="d">

<Grid x:Name="LayoutRoot">
  <Grid.BACkground>
   <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FF760085" Offset="0"/>
    <GradientStop Color="White" Offset="1"/>
   </LinearGradientBrush>
  </Grid.BACkground>
  <Canvas x:Name="SnapContainer" BACkground="#FFA3B4FF" Margin="120,21,140,206">
   <Rectangle x:Name="CameraCanvas" Fill="White" Height="160" Canvas.Left="96" stroke="Black" Canvas.Top="86" Width="200" d:LayoutOverrides="Width,Height"/>
   <Path x:Name="hat" Data="M555,375 C532.33331,375 509.66666,375 487,375 C463.2756,375 443.98489,380.12689 421,383 C420.00772,383.12402 419,383 418,383 C417,383 415.99747,383.07126 415,383 C412.3223,382.80875 405.94312,391.07962 416,377 C418.44412,373.57822 426.32227,373.47314 432,373 C444.02383,371.99802 454.69058,376.15695 462,363 C469.42038,349.64331 469.92422,333.37888 473,318 C489.43094,318 503.58987,321 520,321 C520,333.02835 511.57379,348.32788 525,356 C539.55701,364.3183 533.65735,369 555,369 C555,371.50333 554,373.49667 554,376 C554.33331,375.66666 554.66669,375.33334 555,375 z" Fill="#FF310808" Height="68.241" Canvas.Left="96" Stretch="Fill" stroke="Black" Canvas.Top="297" UseLayoutRounding="false" Width="145.469">
    <i:Interaction.behaviors>
     <il:MouseDragElementBehavior/>
    </i:Interaction.behaviors>
   </Path>
  </Canvas>
  <Button x:Name="StartCamBtn" Content="开启视频" HorizontalAlignment="Left" Height="29" Margin="8,0" VerticalAlignment="Top" Width="108"/>
  <Button x:Name="TakeSnapBtn" Content="拍照" HorizontalAlignment="Left" Height="29" Margin="8,54,0" VerticalAlignment="Top" Width="108"/>
  <Button x:Name="SaveBtn" Content="保存照片" HorizontalAlignment="Left" Height="29" Margin="8,87,0" VerticalAlignment="Top" Width="108"/>
  <Canvas x:Name="ImageHolder" Margin="82,24,-49" Height="255" VerticalAlignment="Bottom"/>
</Grid>
</UserControl>

后台

public partial class MainPage : UserControl
{
        private Capturesource CapSrc = new Capturesource();
        private VideoBrush MyVideoBrush = new VideoBrush();
        private WriteableBitmap _bitmap;
        private bool _VideoIsPlaying = false;

  public MainPage()
  {
   InitializeComponent();
            loaded += new RoutedEventHandler(MainPage_Loaded);
  }

        void MainPage_Loaded(object sender,RoutedEventArgs E)
        {
            StartCamBtn.Click += new RoutedEventHandler(StartCamBtn_Click);
            TakeSnapBtn.Click += new RoutedEventHandler(TakeSnapBtn_Click);
            SaveBtn.Click += new RoutedEventHandler(SaveBtn_Click);
        }

        void SaveBtn_Click(object sender,RoutedEventArgs E)
        {
            SaveTheImg();
        }

        /// <sumMary>
        /// 保存图片
        /// </sumMary>
        private void SaveTheImg()
        {
            _bitmap = new WriteableBitmap(ImageHolder,null);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PNG Files (*.png)|*.png|All Files (*.*)|*.*";
            sfd.DefaultExt = ".png";
            sfd.FilterIndex = 1;

            if ((bool)sfd.ShowDialog())
            {
                using (Stream fs = sfd.openFile())
                {
                    int width = _bitmap.pixelWidth;
                    int height = _bitmap.pixelHeight;

                    EditableImage ei = new EditableImage(width,height);

                    for (int i = 0; i < height; i++)
                    {
                        for (int j = 0; j < width; j++)
                        {
                            int pixel = _bitmap.pixels[(i * width) + j];
                            ei.SetPixel(j,i,
                                        (bytE)((pixel >> 16) & 0xFF),
                                        (bytE)((pixel >> 8) & 0xFF),
                                        (bytE)(pixel & 0xFF),
                                        (bytE)((pixel >> 24) & 0xFF)
                            );
                        }
                    }
                    //获取
                    Stream png = ei.GetStream();
                    int len = (int)png.Length;
                    byte[] bytes = new byte[len];
                    png.Read(bytes,len);
                    fs.Write(bytes,len);
                }
            }
        }

        void TakeSnapBtn_Click(object sender,RoutedEventArgs E)
        {
            WriteableBitmap snapshot = new WriteableBitmap(SnapContainer,null);
            Image image = new Image();
            image.Width = 400;
            image.source = snapshot;
            if (ImageHolder.Children.Count == 1)
            {
                ImageHolder.Children.RemoveAt(0);
            }
            ImageHolder.Children.Add(imagE);
        }

        void StartCamBtn_Click(object sender,RoutedEventArgs E)
        {
            if (!_VideoIsPlaying)
            {
                _VideoIsPlaying = true;
                //调用
                if (CaptureDeviceConfiguration.AllowedDeviceAccess == true || CaptureDeviceConfiguration.requestDeviceAccess())
                {
                    MyVideoBrush.Setsource(CapSrc);
                    CameraCanvas.Fill = MyVideoBrush;
                    CapSrc.Start();
                }

            }
            else
            {
                _VideoIsPlaying = false;
                CapSrc.Stop();
            }
        }
}

 

http://www.cnblogs.com/salam/archive/2010/09/02/1815943.html

大佬总结

以上是大佬教程为你收集整理的教你如何用Silverlight调用摄像头和麦克风,拍照,保存照片全部内容,希望文章能够帮你解决教你如何用Silverlight调用摄像头和麦克风,拍照,保存照片所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。