public async Task <byte[]> CaptureAsync()
        {
            Activity = CrossCurrentActivity.Current.Activity;
            await Task.Delay(50);

            if (Activity == null)
            {
                throw new Exception("You have to set ScreenshotManager.Activity in your Android project");
            }

            LoadShutterSound();

            var view = Activity.Window.DecorView;

            view.DrawingCacheEnabled = true;

            Bitmap bitmap = view.GetDrawingCache(true);

            byte[] bitmapData;

            using (var stream = new MemoryStream())
            {
                await bitmap.CompressAsync(Bitmap.CompressFormat.Jpeg, 30, stream);

                bitmapData = stream.ToArray();
            }

            view.DrawingCacheEnabled = false;
            _mediaSound.Play(MediaActionSoundType.ShutterClick);
            return(bitmapData);
        }
        public void TakePhoto()
        {
            if (_context == null || CameraDevice == null)
            {
                return;
            }

            if (_captureBuilder == null)
            {
                _captureBuilder = CameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);
            }
            _captureBuilder.AddTarget(_imageReader.Surface);
            _captureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);
            SetAutoFlash(_captureBuilder);

            var windowManager = _context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>();
            int rotation      = (int)windowManager.DefaultDisplay.Rotation;
            int orientation   = GetOrientation(rotation);

            _captureBuilder.Set(CaptureRequest.JpegOrientation, orientation);

            _previewSession.StopRepeating();
            _previewSession.AbortCaptures();
            _previewSession.Capture(_captureBuilder.Build(),
                                    new CameraCaptureStillPictureSessionCallback
            {
                OnCaptureCompletedAction = session =>
                {
                    UnlockFocus();
                }
            }, null);
            // Play shutter sound to alert user that image was captured
            var am = (AudioManager)_context.GetSystemService(Context.AudioService);

            if (am != null && am.RingerMode == RingerMode.Normal)
            {
                var cameraSound = new MediaActionSound();
                cameraSound.Load(MediaActionSoundType.ShutterClick);
                cameraSound.Play(MediaActionSoundType.ShutterClick);
            }
        }
Beispiel #3
0
        public void CaptureStillPicture()
        {
            try
            {
                if (null == cameraDevice)
                {
                    return;
                }

                // This is the CaptureRequest.Builder that we use to take a picture.
                var stillCaptureBuilder = cameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);

                stillCaptureBuilder.AddTarget(imageReader.Surface);

                // Use the same AE and AF modes as the preview.
                stillCaptureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);
                SetAutoFlash(stillCaptureBuilder);

                // Orientation
                int rotation    = (int)WindowManager.DefaultDisplay.Rotation;
                int orientation = GetOrientation(rotation);
                stillCaptureBuilder.Set(CaptureRequest.JpegOrientation, orientation);

                captureSession.StopRepeating();
                captureSession.AbortCaptures();
                captureSession.Capture(stillCaptureBuilder.Build(), cameraCaptureCallback, null);

                // Play shutter sound to alert user that image was captured
                var am = (AudioManager)GetSystemService(AudioService);
                if (am != null && am.RingerMode == RingerMode.Normal)
                {
                    var cameraSound = new MediaActionSound();
                    cameraSound.Load(MediaActionSoundType.ShutterClick);
                    cameraSound.Play(MediaActionSoundType.ShutterClick);
                }
            }
            catch (CameraAccessException e)
            {
                e.PrintStackTrace();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Takes the photo.
        /// </summary>
        public void TakePhoto()
        {
            if (_context != null && _cameraDevice != null)
            {
                try
                {
                    Busy?.Invoke(this, true);

                    if (_mediaSoundLoaded)
                    {
                        _mediaSound.Play(MediaActionSoundType.ShutterClick);
                    }

                    // Pick the best JPEG size that can be captures with this CameraDevice
                    var    characteristics = _manager.GetCameraCharacteristics(_cameraDevice.Id);
                    Size[] jpegSizes       = null;
                    if (characteristics != null)
                    {
                        jpegSizes = ((StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap)).GetOutputSizes((int)ImageFormatType.RawSensor);
                    }
                    int width  = 640;
                    int height = 480;

                    if (jpegSizes != null && jpegSizes.Length > 0)
                    {
                        width  = jpegSizes[0].Width;
                        height = jpegSizes[0].Height;
                    }

                    // We use an ImageReader to get a JPEG from CameraDevice
                    // Here, we create a new ImageReader and prepare its Surface as an output from the camera
                    var reader         = ImageReader.NewInstance(width, height, ImageFormatType.Jpeg, 1);
                    var outputSurfaces = new List <Surface>(2);
                    outputSurfaces.Add(reader.Surface);
                    outputSurfaces.Add(new Surface(_viewSurface));

                    CaptureRequest.Builder captureBuilder = _cameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);
                    captureBuilder.AddTarget(reader.Surface);
                    captureBuilder.Set(CaptureRequest.ControlMode, new Integer((int)ControlMode.Auto));

                    // Orientation
                    var windowManager           = _context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>();
                    SurfaceOrientation rotation = windowManager.DefaultDisplay.Rotation;

                    captureBuilder.Set(CaptureRequest.JpegOrientation, new Integer(ORIENTATIONS.Get((int)rotation)));

                    // This listener is called when an image is ready in ImageReader
                    ImageAvailableListener readerListener = new ImageAvailableListener();

                    readerListener.ImageProcessingCompleted += (sender, e) =>
                    {
                        Photo?.Invoke(this, e);
                    };

                    // We create a Handler since we want to handle the resulting JPEG in a background thread
                    HandlerThread thread = new HandlerThread("CameraPicture");
                    thread.Start();
                    Handler backgroundHandler = new Handler(thread.Looper);
                    reader.SetOnImageAvailableListener(readerListener, backgroundHandler);

                    var captureListener = new CameraCaptureListener();

                    captureListener.PhotoComplete += (sender, e) =>
                    {
                        Busy?.Invoke(this, false);
                        StartPreview();
                    };

                    _cameraDevice.CreateCaptureSession(outputSurfaces, new CameraCaptureStateListener()
                    {
                        OnConfiguredAction = (CameraCaptureSession session) =>
                        {
                            try
                            {
                                _previewSession = session;
                                session.Capture(captureBuilder.Build(), captureListener, backgroundHandler);
                            }
                            catch (CameraAccessException ex)
                            {
                                Log.WriteLine(LogPriority.Info, "Capture Session error: ", ex.ToString());
                            }
                        }
                    }, backgroundHandler);
                }
                catch (CameraAccessException error)
                {
                    Log.WriteLine(LogPriority.Error, error.Source, error.Message);
                }
                catch (Java.Lang.Exception error)
                {
                    Log.WriteLine(LogPriority.Error, error.Source, error.Message);
                }
            }
        }
        public override void OnCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result)
        {
            MediaActionSound shutterSound = new MediaActionSound();

            shutterSound.Play(MediaActionSoundType.ShutterClick);
        }