示例#1
0
        public void SetStillPicture()
        {
            int width  = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
            int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);

            int[] PreviewBuffer = new int[width * height];
            _photoCamera.GetPreviewBufferArgb32(PreviewBuffer);

            WriteableBitmap wb = new WriteableBitmap(width, height);

            PreviewBuffer.CopyTo(wb.Pixels, 0);

            MemoryStream ms = new MemoryStream();

            wb.SaveJpeg(ms, wb.PixelWidth, wb.PixelHeight, 0, 80);
            ms.Seek(0, SeekOrigin.Begin);

            BitmapImage bi = new BitmapImage();

            bi.SetSource(ms);
            ImageBrush still = new ImageBrush();

            still.ImageSource       = bi;
            frame.Fill              = still;
            still.RelativeTransform = new CompositeTransform()
            {
                CenterX = 0.5, CenterY = 0.5, Rotation = _photoCamera.Orientation
            };
        }
        /// <summary>
        /// Called when device camera initialized.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="CameraOperationCompletedEventArgs"/> instance containing the event data.</param>
        private void CameraInitialized(object sender, CameraOperationCompletedEventArgs e)
        {
            if (e.Succeeded)
            {
                // Start scan process in separate thread
                Deployment.Current.Dispatcher.BeginInvoke(
                    () =>
                {
                    while (result == null)
                    {
                        var cameraBuffer = new WriteableBitmap(
                            (int)camera.PreviewResolution.Width,
                            (int)camera.PreviewResolution.Height);

                        camera.GetPreviewBufferArgb32(cameraBuffer.Pixels);
                        cameraBuffer.Invalidate();

                        reader.Decode(cameraBuffer);
                    }
                });
            }
            else
            {
                this.result = new BarcodeScannerTask.ScanResult(TaskResult.None);
                NavigationService.GoBack();
            }
        }
示例#3
0
        /*
         * Read camera frames in separate thread
         * */
        private void ReadFramesBackgroundWorker()
        {
            var bufferLayout = _camera.YCbCrPixelLayout;

            int[] currentFrame = new int[bufferLayout.RequiredBufferSize];
            while (!_threadPleaseExit)
            {
                // Get the current preview buffer from the camera
                _camera.GetPreviewBufferArgb32(currentFrame);

                // Does previous and current frame differ?
                if (FrameDiffer(currentFrame))
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        if (AlertEvent != null)
                        {
                            AlertEvent();
                        }
                    });
                }

                // Sleep
                Thread.Sleep(new TimeSpan(0, 0, 0, 0, 300));
            }
        }
示例#4
0
        private void ScanForBarcode()
        {
            _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
            _previewBuffer.Invalidate();

            _barcodeReader.Decode(_previewBuffer);
        }
示例#5
0
        private void ScanForBarcode()
        {
            //grab a camera snapshot
            _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
            _previewBuffer.Invalidate();

            //scan the captured snapshot for barcodes
            //if a barcode is found, the ResultFound event will fire
            _barcodeReader.Decode(_previewBuffer);
        }
示例#6
0
        private void ScanForBarcode()
        {
            var cameraBuffer = new WriteableBitmap(
                (int)camera.PreviewResolution.Width,
                (int)camera.PreviewResolution.Height);

            camera.GetPreviewBufferArgb32(cameraBuffer.Pixels);
            cameraBuffer.Invalidate();

            reader.Decode(cameraBuffer);
        }
 public WriteableBitmap GetPreview()
 {
     if (cameraInitialized)
     {
         WriteableBitmap output = new WriteableBitmap((int)camera.PreviewResolution.Width, (int)camera.PreviewResolution.Height);
         camera.GetPreviewBufferArgb32(output.Pixels);
         return(output);
     }
     else
     {
         return(null);
     }
 }
示例#8
0
        // ARGB frame pump
        void PumpARGBFrames()
        {
            // Create capture buffer.
            int[] ARGBPx = new int[(int)cam.PreviewResolution.Width * (int)cam.PreviewResolution.Height];

            try
            {
                PhotoCamera phCam = (PhotoCamera)cam;

                while (pumpARGBFrames)
                {
                    pauseFramesEvent.WaitOne();

                    // Copies the current viewfinder frame into a buffer for further manipulation.
                    phCam.GetPreviewBufferArgb32(ARGBPx);

                    // Conversion to grayscale.
                    for (int i = 0; i < ARGBPx.Length; i++)
                    {
                        ARGBPx[i] = ColorToGray(ARGBPx[i]);
                    }

                    pauseFramesEvent.Reset();
                    Deployment.Current.Dispatcher.BeginInvoke(delegate()
                    {
                        // Copy to WriteableBitmap.
                        ARGBPx.CopyTo(wb.Pixels, 0);
                        wb.Invalidate();

                        pauseFramesEvent.Set();
                    });
                }
            }
            catch (Exception e)
            {
                this.Dispatcher.BeginInvoke(delegate()
                {
                    // Display error message.
                    txtDebug.Text = e.Message;
                });
            }
        }
示例#9
0
        private void ScanningForQrCode(object sender, EventArgs eventArgs)
        {
            try
            {
                // get camera preview
                int[] previewBuffer = new int[(int)_cam.PreviewResolution.Width * (int)_cam.PreviewResolution.Height];
                _cam.GetPreviewBufferArgb32(previewBuffer);
                WriteableBitmap wb = new WriteableBitmap((int)_cam.PreviewResolution.Width, (int)_cam.PreviewResolution.Height);
                previewBuffer.CopyTo(wb.Pixels, 0);

                // process image
                string text = DecodeQrCode(wb);
                if (text != null)
                {
                    QrCodeDetected?.Invoke(text);
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }
示例#10
0
        private void callback(object sender, EventArgs e)
        {
            cam.FocusAtPoint(r, r);
            r = 1 - r;
            cam.Focus();
            textBlock1 = Globals.textBlock2;
            //if (Globals.tickCount % 50 == 0)
            //{
            //    System.Diagnostics.Debug.WriteLine(st.ElapsedMilliseconds/50);
            //    st.Reset();
            //    st.Start();
            //}

            PhotoCamera phCam = (PhotoCamera)cam;

            for (int i = Globals.n1 - 1; i > 0; i--)
            {
                Globals.x1[i] = Globals.x1[i - 1];
            }

            //pauseFramesEvent.WaitOne();
            phCam.GetPreviewBufferArgb32(ARGBPx);
            int tempVal = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    tempVal += (ARGBPx[j * (int)rect1.Width + i] >> 16) & 0xFF;
                }
            }
            //  tempVal+= (ARGBPx[(int)rect1.Width*(((int)rect1.Height+1)/2 - 50+i)]>>16)&0xFF;
            tempVal        /= 25;
            Globals.x1[0]   = tempVal;
            textBlock1.Text = Globals.x1[0] + "";
            Globals.lpf();
        }
示例#11
0
 /// <summary>
 /// Copies the current viewfinder ARGB frame into a buffer for further manipulation.
 /// </summary>
 /// <param name="pixelData">The ARGB pixel data.</param>
 /// <remarks>
 /// This method is already protected and only called when the service is currently running.
 /// </remarks>
 protected override void GetPreviewBufferArgb32FromCamera(int[] pixelData)
 {
     _photoCamera.GetPreviewBufferArgb32(pixelData);
 }
        private async void ScanPreviewBuffer()
        {
            if (cameraAvailable_)
            {
                int[]     ARGBPx = new int[(int)cameraRes_.Width * (int)cameraRes_.Height];
                MailModel mail   = new MailModel();

                // Get the current frame
                camera_.Focus();
                camera_.GetPreviewBufferArgb32(ARGBPx);

                if (firstImageToCompare_ == null)
                {
                    firstImageToCompare_ =
                        new Image(ARGBPx, (int)cameraRes_.Width, (int)cameraRes_.Height);
                }
                // Do the comparison and detect the motion
                else
                {
                    Image newImage =
                        new Image(ARGBPx, (int)cameraRes_.Width, (int)cameraRes_.Height);
                    int[] r1 = firstImageToCompare_.GetR();
                    int[] r2 = newImage.GetR();

                    // Update the screen if any motion has been detected
                    if (DetectMotion(firstImageToCompare_, newImage))
                    {
                        textBlock.Visibility = Visibility.Visible;

                        // Encode the preview buffer as string
                        using (MemoryStream ms = new MemoryStream())
                        {
                            WriteableBitmap wbmp = new WriteableBitmap(
                                (int)cameraRes_.Width, (int)cameraRes_.Height);
                            ARGBPx.CopyTo(wbmp.Pixels, 0);
                            wbmp.SaveJpeg(ms,
                                          (int)cameraRes_.Width, (int)cameraRes_.Height, 0, 50);

                            mail.Image   = Convert.ToBase64String(ms.ToArray());
                            mail.Subject = "MSS";
                            mail.Body    = "Intruder detected!";
                            mail.To      = App.RoamingSettings.Values["Email"].ToString();
                        }

                        // Send e-mail
                        if (App.RoamingSettings.Values["Emailactivate"].Equals(true) && mailAvailable_)
                        {
                            mailAvailable_ = false;

                            if (!await Controllers.MailController.SendMail(mail))
                            {
                                MessageBox.Show("Email could not be sent!");
                            }
                        }
                    }
                    else
                    {
                        textBlock.Visibility = Visibility.Collapsed;
                    }

                    // Update the image
                    firstImageToCompare_ = newImage;
                }
            }
        }
        private void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
        {
            if (!e.Succeeded)
            {
                cam = null;
                return;
            }

            try
            {
                cam = (PhotoCamera)sender;
                cam.Resolution = cam.AvailableResolutions.First();
            }
            catch (Exception)
            {
                cam = null;
                return;
            }

            this.Dispatcher.BeginInvoke(delegate()
            {
                if (cam == null)
                    return;

                WriteableBitmap bitmap = new WriteableBitmap((int)cam.PreviewResolution.Width,
                                                             (int)cam.PreviewResolution.Height);
                frameStart = DateTime.Now;
                cam.GetPreviewBufferArgb32(bitmap.Pixels);
                detectFaces(bitmap);
            });
        }
示例#14
0
        public void GetImageTexture(int[] returnImage, byte[] imagePtr)
        {
            if (cameraInitialized && cameraReady)
            {
                if (returnImage != null)
                {
                    if (UseLuminance)
                    {
                        camera.GetPreviewBufferY(luminance);

                        for (int i = 0; i < returnImage.Length; ++i)
                        {
                            returnImage[i] = (int)(luminance[i] << 16 | luminance[i] << 8 | luminance[i]);
                        }
                    }
                    else
                    {
                        camera.GetPreviewBufferArgb32(returnImage);

                        byte r, g, b;
                        for (int i = 0; i < returnImage.Length; ++i)
                        {
                            r = (byte)(returnImage[i]);
                            g = (byte)(returnImage[i] >> 8);
                            b = (byte)(returnImage[i] >> 16);
                            returnImage[i] = (int)(r << 16 | g << 8 | b);
                        }
                    }
                }

                if (imagePtr != null)
                {
                    switch (format)
                    {
                    case ImageFormat.B8G8R8A8_32:
                        if (returnImage != null)
                        {
                            Buffer.BlockCopy(returnImage, 0, imagePtr, 0, imagePtr.Length);
                        }
                        else
                        {
                            try
                            {
                                if (UseLuminance)
                                {
                                    camera.GetPreviewBufferY(luminance);

                                    // Here, we assume the resizer is HalfResizer and instead of using
                                    // the HalfResizer to resize the pixel data, it's much faster to
                                    // do it here directly
                                    if (resizer != null)
                                    {
                                        int srcIndex  = 0;
                                        int destIndex = 0;

                                        for (int j = 0; j < cameraHeight; j += 2)
                                        {
                                            for (int i = 0; i < cameraWidth; i += 2, destIndex += 4)
                                            {
                                                imagePtr[destIndex]         = imagePtr[destIndex + 1] =
                                                    imagePtr[destIndex + 2] = luminance[srcIndex];
                                                srcIndex += 2;
                                            }

                                            srcIndex += cameraWidth;
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0, j = 0; i < imagePtr.Length; i += 4, ++j)
                                        {
                                            imagePtr[i] = imagePtr[i + 1] = imagePtr[i + 2] = luminance[j];
                                        }
                                    }
                                }
                                else
                                {
                                    if (color == null)
                                    {
                                        color = new int[imagePtr.Length / 4];
                                    }

                                    camera.GetPreviewBufferArgb32(color);

                                    for (int i = 0; i < imagePtr.Length; i += 4)
                                    {
                                        imagePtr[i]     = (byte)(color[i]);
                                        imagePtr[i + 1] = (byte)(color[i] >> 8);
                                        imagePtr[i + 2] = (byte)(color[i] >> 16);
                                    }
                                }
                            }
                            catch (Exception exp) { }
                        }
                        break;

                    default:
                        throw new GoblinException("Currently " + format + " is not supported");
                    }
                }
            }
        }
示例#15
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                while (cam == null || !isCamInitialized)
                {
                    System.Threading.Thread.Sleep(100);
                }
                lock (cam)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        int[] pixel     = null;
                        int PixelHeight = 0;
                        int PixelWidth  = 0;

                        bool rotateFlip = false;

                        if (imgExample.Visibility == Visibility.Visible)
                        {
                            WriteableBitmap bitmap = imgExample.Source as WriteableBitmap;
                            //using (MemoryStream stream = new MemoryStream(bmp.))
                            //{
                            //    WriteableBitmap bitmap = PictureDecoder.DecodeJpeg(stream);
                            pixel       = (int[])bitmap.Pixels.Clone();
                            PixelHeight = bitmap.PixelHeight;
                            PixelWidth  = bitmap.PixelWidth;
                            //}
                            rotateFlip = false;
                        }
                        else
                        {
                            pixel = new int[(int)(cam.PreviewResolution.Width * cam.PreviewResolution.Height)];
                            cam.GetPreviewBufferArgb32(pixel);
                            PixelHeight = (int)cam.PreviewResolution.Height;
                            PixelWidth  = (int)cam.PreviewResolution.Width;
                            rotateFlip  = true;
                        }

                        byte[] data = new byte[(PixelHeight * PixelWidth * 3)];
                        int h       = PixelHeight;
                        int w       = PixelWidth;
                        int l       = data.Length - 3;
                        for (int x = 0; x < w; x++)
                        {
                            for (int y = 0; y < h; y++)
                            {
                                int p       = ((y * w) + x);
                                int dataPos = 0;
                                if (rotateFlip)
                                {
                                    dataPos = (y * (w * 3)) + ((w - x - 1) * 3);

                                    data[l - dataPos + 0] = (byte)((pixel[p] & 0x00FF0000) >> 16);
                                    data[l - dataPos + 1] = (byte)((pixel[p] & 0x0000FF00) >> 8);
                                    data[l - dataPos + 2] = (byte)((pixel[p] & 0x000000FF));
                                }
                                else
                                {
                                    dataPos = (y * (w * 3)) + (x * 3);

                                    data[dataPos + 0] = (byte)((pixel[p] & 0x00FF0000) >> 16);
                                    data[dataPos + 1] = (byte)((pixel[p] & 0x0000FF00) >> 8);
                                    data[dataPos + 2] = (byte)((pixel[p] & 0x000000FF));
                                }
                            }
                        }

//                           BitmapEncoder encoder = BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

                        WebClient wc = new WebClient();
                        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                        wc.UploadStringCompleted += new UploadStringCompletedEventHandler(ImageServiceRequestCompleted);

                        //wc.UploadStringAsync(new Uri("http://172.16.1.201:8080/actions/upload_picture"), "POST", "width=" + PixelWidth + "&picture=" + HttpUtility.UrlEncode(Convert.ToBase64String(data)));
                        wc.UploadStringAsync(new Uri("http://192.168.137.170:8080/actions/upload_picture"), "POST", "name=" + name + "&width=" + PixelWidth + "&picture=" + HttpUtility.UrlEncode(Convert.ToBase64String(data)));
                    });
                }
                System.Threading.Thread.Sleep(1000);
            }
        }