Search the Community
Showing results for tags 'xna'.
-
Boa dia, Estou a criar um jogo cujo objetivo é capturar imagens de uma webcam, depois aplicar uns filtros para detetar movimento, até agora consegui por a webcam a funcionar no programa, mas quando aplico os filtros dá-me um erro "Região de Bitmaps bloqueada", após conseguir resolver esse erro, deu-me outro de seguida "O formato de pixeis tem de ser igual no Overlay" ou algo assim parecido, mais uma vez consegui resolver o problema. Agora estou preso num outro erro "Memória Esgotada." System out of memory exception. O erro aparece na seguinte linha: image = eventArgs.Frame.Clone(Rec, PixelFormat.Format8bppIndexed); Mas quando eu uso os breakpoints diz-me que o erro vem de : for (int i = 0; i < imgData.Length; i++) { byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000); } Em baixo apresento os dois métoos completos. private void device_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { image = eventArgs.Frame.Clone(Rec, PixelFormat.Format8bppIndexed); MoveTowards moveTowardsFilter = new MoveTowards(); Difference differenceFilter = new Difference(); IFilter thresholdFilter = new Threshold(15); if (backgroundFrame == null) { backgroundFrame = processingFilter1.Apply(image); width = image.Width; height = image.Height; return; } processingFilter1.Add(new Difference(backgroundFrame)); processingFilter1.Add(new Threshold(15)); processingFilter1.Add(new Opening()); processingFilter1.Add(new Edges()); tmpImage = processingFilter1.Apply(image); if (++counter == 2) { counter = 0; moveTowardsFilter.OverlayImage = tmpImage; moveTowardsFilter.ApplyInPlace(backgroundFrame); } differenceFilter.OverlayImage = backgroundFrame; bitmapData = tmpImage.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); differenceFilter.ApplyInPlace(bitmapData); thresholdFilter.Apply(bitmapData); blobCounter.ProcessImage(bitmapData); System.Drawing.Rectangle[] rects = blobCounter.GetObjectsRectangles(); Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(System.Drawing.Color.Red, 1)) { foreach (System.Drawing.Rectangle rc in rects) { g.DrawRectangle(pen, rc); if ((rc.Width > 15) && (rc.Height > 15)) { //Para os retângulos maiores } } } g.Dispose(); } private Texture2D GetTexture(GraphicsDevice dev, System.Drawing.Bitmap bmp) { int[] imgData = new int[bmp.Width * bmp.Height]; Texture2D texture = new Texture2D(dev, bmp.Width, bmp.Height); unsafe { // lock bitmap System.Drawing.Imaging.BitmapData origdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); uint* byteData = (uint*)origdata.Scan0; // Switch bgra -> rgba for (int i = 0; i < imgData.Length; i++) { byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000); } // copy data System.Runtime.InteropServices.Marshal.Copy(origdata.Scan0, imgData, 0, bmp.Width * bmp.Height); byteData = null; // unlock bitmap bmp.UnlockBits(origdata); } texture.SetData(imgData); return texture; }
-
-
Boas tardes caros colegas, Eu sou novo no XNA e C#, consegui encontrar um pedaço de código que deteta a webcam e passa-a para um picturebox, só que agora eu queria que detete movimento, qualquer ajuda é agradecida. GAME1.CS using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using System.Windows.Forms; using DirectShowLib; using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imaging; namespace WebCamXNA { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; private System.Windows.Forms.Button button1; private System.Windows.Forms.PictureBox pictureBox1; private System.ComponentModel.Container components = null; private System.Windows.Forms.PictureBox pictureBox2; private Capture cam; IntPtr m_ip = IntPtr.Zero; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { // TODO: Add your initialization logic here this.button1 = new System.Windows.Forms.Button(); this.button1.Visible = false; this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox2 = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); // // button1 // //this.button1.Location = new System.Drawing.Point(368, 80); //this.button1.Name = "button1"; //this.button1.Size = new System.Drawing.Size(75, 40); //this.button1.TabIndex = 0; //this.button1.Text = "Click"; //this.button1.Click += new System.EventHandler(this.button1_Click); // // pictureBox1 // //this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; //this.pictureBox1.Location = new System.Drawing.Point(graphics.PreferredBackBufferWidth/2, graphics.PreferredBackBufferHeight/2); //this.pictureBox1.Name = "pictureBox1"; //this.pictureBox1.Size = new System.Drawing.Size(320, 240); //this.pictureBox1.TabIndex = 1; //this.pictureBox1.TabStop = false; // // pictureBox2 // this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pictureBox2.Location = new System.Drawing.Point(170,50); this.pictureBox2.Name = "pictureBox2"; this.pictureBox2.Size = new System.Drawing.Size(430, 400); this.pictureBox2.TabIndex = 2; this.pictureBox2.TabStop = false; Control.FromHandle(Window.Handle).Controls.Add(pictureBox1); Control.FromHandle(Window.Handle).Controls.Add(pictureBox2); Control.FromHandle(Window.Handle).Controls.Add(button1); const int VIDEODEVICE = 0; // zero based index of video capture device to use const int VIDEOWIDTH = 320; // Depends on video device caps const int VIDEOHEIGHT = 240; // Depends on video device caps const int VIDEOBITSPERPIXEL = 24; // BitsPerPixel values determined by device cam = new Capture(VIDEODEVICE, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, pictureBox2); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); } protected override void UnloadContent() { cam.Dispose(); if (m_ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(m_ip); m_ip = IntPtr.Zero; } } protected override void Update(GameTime gameTime) { if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape)) this.Exit(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue); base.Draw(gameTime); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); if (m_ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(m_ip); m_ip = IntPtr.Zero; } } //********TIRAR UM PRINTSCREEN DA FRAME ATUAL**************** //private void button1_Click(object sender, System.EventArgs e) //{ // Cursor.Current = Cursors.WaitCursor; // // Release any previous buffer // if (m_ip != IntPtr.Zero) // { // Marshal.FreeCoTaskMem(m_ip); // m_ip = IntPtr.Zero; // } // // capture image // m_ip = cam.Click(); // Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip); // // If the image is upsidedown // b.RotateFlip(RotateFlipType.RotateNoneFlipY); // pictureBox1.Image = b; // Cursor.Current = Cursors.Default; //} } } CAPTURE.CS using System; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.Runtime.InteropServices; using System.Threading; using System.Diagnostics; using System.Windows.Forms; using DirectShowLib; namespace WebCamXNA { internal class Capture : ISampleGrabberCB, IDisposable { #region Member variables /// <summary> graph builder interface. </summary> private IFilterGraph2 m_FilterGraph = null; // Used to snap picture on Still pin private IAMVideoControl m_VidControl = null; private IPin m_pinStill = null; /// <summary> so we can wait for the async job to finish </summary> private ManualResetEvent m_PictureReady = null; private bool m_WantOne = false; /// <summary> Dimensions of the image, calculated once in constructor for perf. </summary> private int m_videoWidth; private int m_videoHeight; private int m_stride; /// <summary> buffer for bitmap data. Always release by caller</summary> private IntPtr m_ipBuffer = IntPtr.Zero; #if DEBUG // Allow you to "Connect to remote graph" from GraphEdit DsROTEntry m_rot = null; #endif #endregion #region APIs [DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")] private static extern void CopyMemory(IntPtr Destination, IntPtr Source, [MarshalAs(UnmanagedType.U4)] int Length); #endregion // Zero based device index and device params and output window public Capture(int iDeviceNum, int iWidth, int iHeight, short iBPP, Control hControl) { DsDevice[] capDevices; // Get the collection of video devices capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); if (iDeviceNum + 1 > capDevices.Length) { throw new Exception("No video capture devices found at that index!"); } try { // Set up the capture graph SetupGraph(capDevices[iDeviceNum], iWidth, iHeight, iBPP, hControl); // tell the callback to ignore new images m_PictureReady = new ManualResetEvent(false); } catch { Dispose(); throw; } } /// <summary> release everything. </summary> public void Dispose() { #if DEBUG if (m_rot != null) { m_rot.Dispose(); } #endif CloseInterfaces(); if (m_PictureReady != null) { m_PictureReady.Close(); } } // Destructor ~Capture() { Dispose(); } /// <summary> /// Get the image from the Still pin. The returned image can turned into a bitmap with /// Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip); /// If the image is upside down, you can fix it with /// b.RotateFlip(RotateFlipType.RotateNoneFlipY); /// </summary> /// <returns>Returned pointer to be freed by caller with Marshal.FreeCoTaskMem</returns> public IntPtr Click() { int hr; // get ready to wait for new image m_PictureReady.Reset(); m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(m_stride) * m_videoHeight); try { m_WantOne = true; // If we are using a still pin, ask for a picture if (m_VidControl != null) { // Tell the camera to send an image hr = m_VidControl.SetMode(m_pinStill, VideoControlFlags.Trigger); DsError.ThrowExceptionForHR(hr); } // Start waiting if (!m_PictureReady.WaitOne(9000, false)) { throw new Exception("Timeout waiting to get picture"); } } catch { Marshal.FreeCoTaskMem(m_ipBuffer); m_ipBuffer = IntPtr.Zero; throw; } // Got one return m_ipBuffer; } public int Width { get { return m_videoWidth; } } public int Height { get { return m_videoHeight; } } public int Stride { get { return m_stride; } } /// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP, Control hControl) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin pCaptureOut = null; IPin pSampleIn = null; IPin pRenderIn = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; try { #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif // add the video input device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); // Find the still pin m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0); // Didn't find one. Is there a preview pin? if (m_pinStill == null) { m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0); } // Still haven't found one. Need to put a splitter in so we have // one stream to capture the bitmap from, and one to display. Ok, we // don't *have* to do it that way, but we are going to anyway. if (m_pinStill == null) { IPin pRaw = null; IPin pSmart = null; // There is no still pin m_VidControl = null; // Add a splitter IBaseFilter iSmartTee = (IBaseFilter)new SmartTee(); try { hr = m_FilterGraph.AddFilter(iSmartTee, "SmartTee"); DsError.ThrowExceptionForHR(hr); // Find the find the capture pin from the video device and the // input pin for the splitter, and connnect them pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0); hr = m_FilterGraph.Connect(pRaw, pSmart); DsError.ThrowExceptionForHR(hr); // Now set the capture and still pins (from the splitter) m_pinStill = DsFindPin.ByName(iSmartTee, "Preview"); pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture"); // If any of the default config items are set, perform the config // on the actual video device (rather than the splitter) if (iHeight + iWidth + iBPP > 0) { SetConfigParms(pRaw, iWidth, iHeight, iBPP); } } finally { if (pRaw != null) { Marshal.ReleaseComObject(pRaw); } if (pRaw != pSmart) { Marshal.ReleaseComObject(pSmart); } if (pRaw != iSmartTee) { Marshal.ReleaseComObject(iSmartTee); } } } else { // Get a control pointer (used in Click()) m_VidControl = capFilter as IAMVideoControl; pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); // If any of the default config items are set if (iHeight + iWidth + iBPP > 0) { SetConfigParms(m_pinStill, iWidth, iHeight, iBPP); } } // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Configure the sample grabber IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); // Get the default video renderer IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter; hr = m_FilterGraph.AddFilter(pRenderer, "Renderer"); DsError.ThrowExceptionForHR(hr); pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0); // Add the sample grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); if (m_VidControl == null) { // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); // Connect the capture pin to the renderer hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); } else { // Connect the capture pin to the renderer hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); } // Learn the video properties SaveSizeInfo(sampGrabber); ConfigVideoWindow(hControl); // Start the graph IMediaControl mediaCtrl = m_FilterGraph as IMediaControl; hr = mediaCtrl.Run(); DsError.ThrowExceptionForHR(hr); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (pCaptureOut != null) { Marshal.ReleaseComObject(pCaptureOut); pCaptureOut = null; } if (pRenderIn != null) { Marshal.ReleaseComObject(pRenderIn); pRenderIn = null; } if (pSampleIn != null) { Marshal.ReleaseComObject(pSampleIn); pSampleIn = null; } } } private void SaveSizeInfo(ISampleGrabber sampGrabber) { int hr; // Get the media type from the SampleGrabber AMMediaType media = new AMMediaType(); hr = sampGrabber.GetConnectedMediaType(media); DsError.ThrowExceptionForHR(hr); if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Grab the size info VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); m_videoWidth = videoInfoHeader.BmiHeader.Width; m_videoHeight = videoInfoHeader.BmiHeader.Height; m_stride = m_videoWidth * (videoInfoHeader.BmiHeader.BitCount / 8); DsUtils.FreeAMMediaType(media); media = null; } // Set the video window within the control specified by hControl private void ConfigVideoWindow(Control hControl) { int hr; IVideoWindow ivw = m_FilterGraph as IVideoWindow; // Set the parent hr = ivw.put_Owner(hControl.Handle); DsError.ThrowExceptionForHR(hr); // Turn off captions, etc hr = ivw.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings); DsError.ThrowExceptionForHR(hr); // Yes, make it visible hr = ivw.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); // Move to upper left corner Rectangle rc = hControl.ClientRectangle; hr = ivw.SetWindowPosition(0, 0, rc.Right, rc.Bottom); DsError.ThrowExceptionForHR(hr); } private void ConfigureSampleGrabber(ISampleGrabber sampGrabber) { int hr; AMMediaType media = new AMMediaType(); // Set the media type to Video/RBG24 media.majorType = MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; hr = sampGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media); media = null; // Configure the samplegrabber hr = sampGrabber.SetCallback(this, 1); DsError.ThrowExceptionForHR(hr); } // Set the Framerate, and video size private void SetConfigParms(IPin pStill, int iWidth, int iHeight, short iBPP) { int hr; AMMediaType media; VideoInfoHeader v; IAMStreamConfig videoStreamConfig = pStill as IAMStreamConfig; // Get the existing format block hr = videoStreamConfig.GetFormat(out media); DsError.ThrowExceptionForHR(hr); try { // copy out the videoinfoheader v = new VideoInfoHeader(); Marshal.PtrToStructure(media.formatPtr, v); // if overriding the width, set the width if (iWidth > 0) { v.BmiHeader.Width = iWidth; } // if overriding the Height, set the Height if (iHeight > 0) { v.BmiHeader.Height = iHeight; } // if overriding the bits per pixel if (iBPP > 0) { v.BmiHeader.BitCount = iBPP; } // Copy the media structure back Marshal.StructureToPtr(v, media.formatPtr, false); // Set the new format hr = videoStreamConfig.SetFormat(media); DsError.ThrowExceptionForHR(hr); } finally { DsUtils.FreeAMMediaType(media); media = null; } } /// <summary> Shut down capture </summary> private void CloseInterfaces() { int hr; try { if (m_FilterGraph != null) { IMediaControl mediaCtrl = m_FilterGraph as IMediaControl; // Stop the graph hr = mediaCtrl.Stop(); } } catch (Exception ex) { Debug.WriteLine(ex); } if (m_FilterGraph != null) { Marshal.ReleaseComObject(m_FilterGraph); m_FilterGraph = null; } if (m_VidControl != null) { Marshal.ReleaseComObject(m_VidControl); m_VidControl = null; } if (m_pinStill != null) { Marshal.ReleaseComObject(m_pinStill); m_pinStill = null; } } /// <summary> sample callback, NOT USED. </summary> int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample) { Marshal.ReleaseComObject(pSample); return 0; } /// <summary> buffer callback, COULD BE FROM FOREIGN THREAD. </summary> int ISampleGrabberCB.BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen) { // Note that we depend on only being called once per call to Click. Otherwise // a second call can overwrite the previous image. Debug.Assert(BufferLen == Math.Abs(m_stride) * m_videoHeight, "Incorrect buffer length"); if (m_WantOne) { m_WantOne = false; Debug.Assert(m_ipBuffer != IntPtr.Zero, "Unitialized buffer"); // Save the buffer CopyMemory(m_ipBuffer, pBuffer, BufferLen); // Picture is ready. m_PictureReady.Set(); } return 0; } } } Mais uma vez digo, ainda sou novato nisto, os meus unicos projetos foram jogo de ping pong (realizado sozinho) e um jogo de canhões (Ajuda com um tutorial),
-
Após o sucesso das últimas edições eis que chega a altura de mais um XNA Pizza Night, o concurso destinado a todos os estudantes, game developers e amantes de jogos. O que é? O XNA Pizza Night é um evento promovido pela Microsoft em parceria com o XNAPT que tem como objectivo incentivar o desenvolvimento de jogos em Portugal e dar foco aos seus criadores. Porque participar? Este evento é a tua oportunidade de mostrares os teus trabalhos e de passar um bom momento com outros developers. Na ultimas edições tivemos excelentes jogos, muitos sairam candidatos à Imagine Cup, outros estão a ter sucesso no Marketplace do Windows Phone! e ainda temos alguns exemplos comerciais multi-plataforma: Vizati Como participar? Criar um jogo ou demo com a tecnologia XNA/Silverilight e mostrarem o vosso talento. Podes participar individualmente ou em grupo num máximo de 4 pessoas por equipa. Todos os jogos são submetidos a votação e há prémios para os melhores. E como não podia deixar de ser, pizzas e bebidas por conta da Microsoft. O concurso é no dia 4 de Março por isso comecem já a trabalhar. Para te inscreveres, basta mandar email para pizzanight@xnapt.net com: Nome da equipa Nome do jogo Número de Elementos Nome e Email de um elemento Em que tecnologias posso criar o meu jogo? Podes fazer o teu jogo em XNA3.0/3.1/4.0, tanto para PC/Xbox como para Windows Phone 7. Além de XNA aceitamos também jogos em Silverlight, mais uma vez sejam eles Windowsou Windows Phone 7. Existem algumas restrições quanto ao tipo/tema do jogo? Não, podes fazer o teu jogo sem qualquer restrição. No entanto, se quiseres aproveitar a Pizza Night para pensar numa ideia para participar naImagine Cup 2011, que te poderá levar a Nova Iorque! Sabe mais em: http://imaginecup.com/competitions/game-design E as ferramentas para criar o meu jogo? Todo o Software de desenvolvimento é totalmente gratuito! Podes fazer download de todo o software aqui: http://create.msdn.com/en-us/resources/downloads Para qualquer dúvida podem ir até ao fórum do XNAPT onde podem responder às vossas questões. Site do evento: http://xnapt.net/2011/01/pizzanight_springedition/