コード例 #1
0
        public async Task <ActionResult> GetImage()
        {
            if (DateTime.Now.Hour > 18 || DateTime.Now.Hour < 7)
            {
                _logger.LogInformation("Tuning for night mode...");
                MMALCameraConfig.ExposureCompensation = (int)MMAL_PARAM_EXPOSUREMODE_T.MMAL_PARAM_EXPOSUREMODE_NIGHT;
            }
            else
            {
                _logger.LogInformation("Tuning for normal mode...");
                MMALCameraConfig.ExposureCompensation = (int)MMAL_PARAM_EXPOSUREMODE_T.MMAL_PARAM_EXPOSUREMODE_OFF;
            }
            byte[] imageData;
            using (var imgCaptureHandler = new MemoryStreamCaptureHandler())
            {
                _logger.LogInformation("Capturing the picture...");
                await _camera.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                imageData = new byte[imgCaptureHandler.CurrentStream.Length];
                imgCaptureHandler.CurrentStream.Seek(0, System.IO.SeekOrigin.Begin);
                imgCaptureHandler.CurrentStream.Read(imageData, 0, imageData.Length);
                _logger.LogInformation("Captured the picture...");
            }
            return(File(imageData, "image/jpeg"));
        }
コード例 #2
0
ファイル: ImageProcessor.cs プロジェクト: dvroegop/PiCam
        /// <summary>
        /// Takes a picture and saves that in the default location
        /// </summary>
        /// <returns>The filename of the picture,
        /// without path or extension</returns>
        public async Task <string> TakePicture()
        {
            // Instantiate the camera, if needed
            // In my demos I always have the cam upside down. Fix that (not needed
            // for analyzing, just for the user...)
            if (_cameraDevice == null)
            {
                MMALCameraConfig.Flips = MMAL_PARAM_MIRROR_T.MMAL_PARAM_MIRROR_BOTH;
                _cameraDevice          = MMALCamera.Instance;
            }

            // Create the handler that will receive the data
            using (var imgCaptureHandler = new ImageStreamCaptureHandler(
                       "/home/pi/images/",
                       "jpg"))
            {
                // Call the camera and collect bytes
                await _cameraDevice.TakePicture(
                    imgCaptureHandler,
                    MMALEncoding.JPEG,
                    MMALEncoding.I420);

                // Get the last generated filename
                var fileName = imgCaptureHandler.GetFilename();

                return(fileName);
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: FabianNitsche/Fermenter
        static void Main(string[] args)
        {
            MMALCamera cam = MMALCamera.Instance;

            // Create observable that will generate an incrementing number every second
            var observable = Observable.Generate(1, x => true, x => x + 1, x => x, x => TimeSpan.FromSeconds(1));

            var relay  = OutputPort.Create(17, OutputPort.InitialValue.Low).Result;
            var light1 = OutputPort.Create(27, OutputPort.InitialValue.Low).Result;
            var light2 = OutputPort.Create(22, OutputPort.InitialValue.Low).Result;
            var button = InputPort.Create(24, GpioEdge.Both).Result;

            // Write true whenever the number is even and odd when the number is odd
            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
                using (observable.Select(x => x % 2 == 0).Subscribe(relay))
                    using (observable.Select(x => x % 2 == 0).Subscribe(light1))
                        //using (observable.Select(x => x % 2 != 0).Subscribe(light2))
                        //using (button.Do(pressed => Console.WriteLine(pressed)).Subscribe())
                        using (button.Subscribe(light2))
                            using (var i2cBus = new I2CBusPI("/dev/i2c-1"))
                            {
                                var takePictureTask = cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                                var i2cDevice = new I2CDevicePI(i2cBus, Display.DefaultI2CAddress);

                                var sensor = new BME280Sensor(i2cBus, 1014);

                                var display = new SSD1306.Display(i2cDevice, 128, 64);
                                display.Init();

                                var dfont = new AdafruitSinglePageFont();

                                for (int i = 0; i < 100; i++)
                                {
                                    display.WriteLineBuff(dfont, $"Temperature: {sensor.ReadTemperature().Result} °C", $"Pressure: {sensor.ReadPressure().Result} Pa", $"Humidity: {sensor.ReadHumidity().Result} %", $"Altitude: {sensor.ReadAltitude().Result} m", "Line 5", "Line 6", "Line 7", "Line 8");
                                    display.DisplayUpdate();
                                }

                                //for (int i = 0; i < 100; i++)
                                //    display.DrawPixel(i, i);

                                takePictureTask.Wait();
                                display.ClearDisplay();
                            }
            // releasing relay
            relay.Write(true);
            // turning of light
            light1.Write(false);
            light2.Write(false);
            // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done
            // on the camera.
            cam.Cleanup();
        }
コード例 #4
0
ファイル: PiCameraService.cs プロジェクト: tobievii/PiMonitR
        private async Task <string> CapturePictureAndGetFileName()
        {
            string fileName = null;

            using (var imgCaptureHandler = new ImageStreamCaptureHandler(picStoragePath, picExtension))
            {
                await MMALCamera.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                fileName = imgCaptureHandler.GetFilename();
            }
            return(fileName);
        }
コード例 #5
0
        //   private string UploadUrl = "https://westeurope.api.cognitive.microsoft.com/customvision/v3.0/Prediction/aeeb0af8-9c49-45d3-8419-9c76485c1fdf/detect/iterations/Iteration1/image";

        public async Task <string> TakePicture()
        {
            if (_mmalCamera == null)
            {
                MMALCameraConfig.Flips = MMAL_PARAM_MIRROR_T.MMAL_PARAM_MIRROR_BOTH;
                _mmalCamera            = MMALCamera.Instance;
            }

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
            {
                await _mmalCamera.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                var fileName = imgCaptureHandler.GetFilename();

                return(fileName);
            }
        }
コード例 #6
0
        public async Task <ImageDetails> Post([FromBody] bool refresh)
        {
            String fileName = "NotAnImage.jpg";

            logger.LogInformation("Will refresh Image");

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/doorimages/", "jpg"))
            {
                await cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                fileName = imgCaptureHandler.GetFilename();
                logger.LogInformation(fileName);
            }
            ImageDetails imageDetails = new ImageDetails();

            imageDetails.Name = "/doorimages/" + Uri.EscapeDataString(fileName) + ".jpg";
            return(imageDetails);
        }
コード例 #7
0
        public static async Task TakePictureAsync()
        {
            MMALCameraConfig.Annotate = new AnnotateImage
            {
                ShowDateText = true,
                ShowTimeText = true
            };
            MMALCameraConfig.StatsPass = true;
            // Singleton initialized lazily. Reference once in your application.
            MMALCamera cam = MMALCamera.Instance;

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
            {
                await cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);
            }

            // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done
            // on the camera.
            cam.Cleanup();
        }
コード例 #8
0
        static async Task Main(string[] args)
        {
            System.Console.WriteLine("Starting");
            const int lightCount = 12;
            var       settings   = new SpiConnectionSettings(0, 0)
            {
                ClockFrequency = 2_400_000,
                Mode           = SpiMode.Mode0,
                DataBitLength  = 8
            };

            // Create a Neo Pixel x8 stick on spi 0.0
            var spi = SpiDevice.Create(settings);

            var neo = new Ws2812b(spi, lightCount);

            var img = neo.Image;

            img.Clear();
            for (var x = 0; x < lightCount; x++)
            {
                img.SetPixel(x, 0, Color.White);
            }
            neo.Update();

            MMALCamera cam = MMALCamera.Instance;

            for (var y = 0; y < 50; y++)
            {
                using (var imgCaptureHandler = new ImageStreamCaptureHandler($"_testing{y}.jpg")){
                    await cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);
                }
                Step();
            }

            img.Clear();
            neo.Update();
            cam.Cleanup();
        }
コード例 #9
0
        public async Task TakePictureAsync(string filePath)
        {
            try
            {
                // Singleton initialized lazily. Reference once in your application.
                MMALCamera cam = this.MMALSharpCameraInstance;

                using (var imgCaptureHandler = new ImageStreamCaptureHandler(filePath))
                {
                    await cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);
                }

                // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done
                // on the camera.
                Console.WriteLine($"Wrote picture to: {filePath}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{nameof(MMALSharpCamera)} {nameof(TakePictureAsync)} Failed");
                Console.WriteLine($"{nameof(MMALSharpCamera)} {nameof(TakePictureAsync)} {ex.ToString()}");
                Console.WriteLine($"{nameof(MMALSharpCamera)} {nameof(TakePictureAsync)} Failed");
            }
        }
コード例 #10
0
        public async Task <TakePhotoResponse> TakePhotoAsync(TakePhotoRequest takePhotoRequest)
        {
            var cameraWasUsed = false;


            if (IsTakingTimelapse())
            {
                return(new TakePhotoResponse()
                {
                    ErrorMessage = $"Timelapse {currentTimelapseId?.ToString()} is being taken",
                });
            }

            try
            {
                var path = EnsureLocalDirectoryExists();

                await cameraInUse.WaitAsync();

                cameraWasUsed = true;

                using (var imgCaptureHandler = new ImageStreamCaptureHandler(path, takePhotoRequest.ImageType))
                {
                    var stopwatch = Stopwatch.StartNew();

                    if (takePhotoRequest.QuickMode)
                    {
                        await camera.TakeRawPicture(imgCaptureHandler);
                    }
                    else
                    {
                        await camera.TakePicture(imgCaptureHandler, takePhotoRequest.GetImageEncoding(), takePhotoRequest.GetPixelFormatEncoding());
                    }
                    stopwatch.Stop();

                    var localFilePath = imgCaptureHandler.GetFilepath();
                    var blobName      = await UploadFileAsync("photos", localFilePath);

                    var fi = new FileInfo(localFilePath);
                    Logger.Log($"New photo: {fi.Length} bytes, in {stopwatch.ElapsedMilliseconds}ms");

                    if (takePhotoRequest.DeleteLocalFile)
                    {
                        File.Delete(localFilePath);
                    }

                    return(new TakePhotoResponse()
                    {
                        BlobName = blobName,
                        LocalFilePath = localFilePath,
                        DeleteLocalFile = takePhotoRequest.DeleteLocalFile,
                        PixelFormat = takePhotoRequest.PixelFormat,
                        ImageType = takePhotoRequest.ImageType,
                        QuickMode = takePhotoRequest.QuickMode,
                    });
                }
            }
            finally
            {
                if (cameraWasUsed)
                {
                    cameraInUse.Release();
                }
            }
        }