Esempio n. 1
0
 public bool OnSurfaceTextureDestroyed(SurfaceTexture surface)
 {
     if (camEnabled)
     {
         camera.StopPreview();
         camera.Release();
         cameraClosed = true;
     }
     return(true);
 }
Esempio n. 2
0
        private void SwitchCameraButton_Click(object sender, EventArgs e)
        {
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
            {
                string[] camPermissions = { Android.Manifest.Permission.Camera };

                // Make sure we can use the camera
                if ((int)Android.OS.Build.VERSION.SdkInt >= 23 && Activity.CheckSelfPermission(camPermissions[0]) != (Permission.Granted))
                {
                    Toast.MakeText(Activity, AppResources.Camera_permissionNeeded, ToastLength.Long).Show();
                    Activity.RequestPermissions(camPermissions, CamPermReqId);
                    return;
                }

                mCameraDevice.Close();

                int newCamIndex = backCam ? 1 : 0;
                backCam = !backCam;

                try
                {
                    OpenCamera(newCamIndex);
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(ex);
                }
            }
            else
            {
                if (cameraType == CameraFacing.Front)
                {
                    cameraType = CameraFacing.Back;

                    camera.StopPreview();
                    camera.Release();
                    camera = Android.Hardware.Camera.Open((int)cameraType);
                    camera.SetPreviewTexture(surfaceTexture);
                    PrepareAndStartCamera1();
                }
                else
                {
                    cameraType = CameraFacing.Front;

                    camera.StopPreview();
                    camera.Release();
                    camera = Android.Hardware.Camera.Open((int)cameraType);
                    camera.SetPreviewTexture(surfaceTexture);
                    PrepareAndStartCamera1();
                }
            }
        }
Esempio n. 3
0
 protected override void OnDestroy()
 {
     camera?.StopPreview();
     camera?.Release();
     camera = null;
     base.OnDestroy();
 }
Esempio n. 4
0
        public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
        {
            if (Holder.Surface == null)
            {
                return;
            }

            try
            {
                _camera.StopPreview();
            }
            catch (Exception)
            {
                // ignore: tried to stop a non-existent preview
            }

            try
            {
                // start preview with new settings
                _camera.SetPreviewDisplay(Holder);
                _camera.StartPreview();
            }
            catch (Exception e)
            {
                Log.Debug("", "Error starting camera preview: " + e.Message);
            }
        }
Esempio n. 5
0
        public bool OnSurfaceTextureDestroyed(SurfaceTexture surface)
        {
            _camera.StopPreview();
            _camera.Release();

            return(true);
        }
Esempio n. 6
0
        public void ShutdownCamera()
        {
            if (camera == null)
            {
                return;
            }

            try
            {
                try
                {
                    camera.StopPreview();
                    camera.SetNonMarshalingPreviewCallback(null);

                    camera.SetPreviewDisplay(null);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }

                camera.Release();
                camera = null;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
        }
Esempio n. 7
0
 public override void OnDestroy()
 {
     _camera.StopPreview();
     _camera.Release();
     _cameraReleased = true;
     base.OnDestroy();
 }
Esempio n. 8
0
        public void ShutdownCamera()
        {
            if (Camera == null)
            {
                return;
            }


            try
            {
                try
                {
                    Camera.StopPreview();


                    //Camera.SetPreviewDisplay(null);
                }
                catch (Exception ex)
                {
                    //Android.Util.Log.Error(MobileBarcodeScanner.TAG, ex.ToString());
                }
                Camera.Release();
                Camera = null;
            }
            catch (Exception e)
            {
                //Android.Util.Log.Error(MobileBarcodeScanner.TAG, e.ToString());
            }
        }
        public void ShutdownCamera()
        {
            if (Camera == null)
            {
                return;
            }

            // camera release logic takes about 0.005 sec so there is no need in async releasing
            var perf = PerformanceCounter.Start();

            try
            {
                try
                {
                    Camera.SetPreviewCallback(null);
                    Camera.SetPreviewDisplay(null);
                    Camera.StopPreview();
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Error(MobileBarcodeScanner.TAG, ex.ToString());
                }
                Camera.Release();
                Camera = null;
            }
            catch (Exception e)
            {
                Android.Util.Log.Error(MobileBarcodeScanner.TAG, e.ToString());
            }

            PerformanceCounter.Stop(perf, "Shutdown camera took {0}ms");
        }
Esempio n. 10
0
 // Stop preview if it is running
 private void stopPreview()
 {
     if (inPreview)
     {
         camera.StopPreview();
         inPreview = false;
     }
 }
Esempio n. 11
0
        public async Task <byte[]> TakePhoto()
        {
            camera.StopPreview();
            var ratio = ((decimal)Height) / Width;
            var image = Bitmap.CreateBitmap(liveView.Bitmap, 0, 0, liveView.Bitmap.Width, (int)(liveView.Bitmap.Width * ratio));

            byte[] imageBytes = null;
            using (var imageStream = new System.IO.MemoryStream())
            {
                await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 50, imageStream);

                image.Recycle();
                imageBytes = imageStream.ToArray();
            }
            camera.StartPreview();
            return(imageBytes);
        }
Esempio n. 12
0
        public bool OnSurfaceTextureDestroyed(SurfaceTexture surface)
        {
            camera.StopPreview();
            camera.SetPreviewCallback(null);
            camera.Release();

            return(true);
        }
Esempio n. 13
0
        void TakePhotoButtonTapped(object sender, EventArgs e)
        {
            //if camera isn't started
            if (!_isCameraStarted)
            {
                return;
            }

            camera.StopPreview();

            //Gets bitmap from the textureView
            var bitmap = textureView.Bitmap;

            //Converts bitmap to byte[]
            byte[] bitmapData;
            using (var stream = new MemoryStream())
            {
                bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
                bitmapData = stream.ToArray();
            }

            //Send a message to App.axml to open next page
            MessagingCenter.Send <App, byte[]>(App.Current as App, "OpenRestaurantResultPage", bitmapData);

            camera.StartPreview();
        }
 public void SurfaceDestroyed(ISurfaceHolder holder)
 {
     //stop the preview
     mCamera.StopPreview();
     //release the camera
     mCamera.Release();
     //unbind the camera from this object
     mCamera = null;
 }
Esempio n. 15
0
        public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
        {
            if (_holder.Surface == null)
            {
                return;
            }
            try
            {
                _camera.StopPreview();
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
            if (_previewSize != null)
            {
                try
                {
                    Android.Hardware.Camera.Parameters parameters = _camera.GetParameters();
                    parameters.SetPreviewSize(_previewSize.Width, _previewSize.Height);
                    parameters.SetPictureSize(_picSize.Width, _picSize.Height);
                    _camera.SetParameters(parameters);
                    _camera.SetPreviewDisplay(_holder);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }

            Android.App.Activity activity = _context as Android.App.Activity;
            if (activity != null)
            {
                int orientation = CameraHelper.GetCameraDisplayOrientation(activity, _cameraId, displayRotation);
                _camera.SetDisplayOrientation(orientation);
            }

            try
            {
                Parameters parameters = _camera.GetParameters();

                foreach (var previewSize in  _camera.GetParameters().SupportedPreviewSizes)
                {
                    // if the size is suitable for you, use it and exit the loop.
                    parameters.SetPreviewSize(previewSize.Width, previewSize.Height);
                    break;
                }

                _camera.SetParameters(parameters);
                _camera.SetPreviewDisplay(_holder);
                _camera.StartPreview();
            }
            catch (Exception ex)
            {
                Log.Debug(Tag.ToString(), "Error starting camera preview: " + ex.Message);
            }
        }
Esempio n. 16
0
        private void PrepareAndStartCamera()
        {
            camera.StopPreview();

            var display = WindowManager.DefaultDisplay;

            if (display.Rotation == SurfaceOrientation.Rotation0)
            {
                camera.SetDisplayOrientation(90);
            }

            if (display.Rotation == SurfaceOrientation.Rotation270)
            {
                camera.SetDisplayOrientation(180);
            }

            camera.StartPreview();
        }
Esempio n. 17
0
 public void SurfaceDestroyed(ISurfaceHolder holder)
 {
     if (_camera != null)
     {
         _camera.StopPreview();
         _camera.Release();
         _camera = null;
     }
 }
Esempio n. 18
0
 public void SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)
 {
     try
     {
         if (previewing)
         {
             camera.StopPreview();
             previewing = false;
         }
         if (camera != null)
         {
             camera.SetPreviewDisplay(surfaceHolder);
             camera.StartPreview();
             previewing = true;
         }
     }
     catch { }
 }
Esempio n. 19
0
        void SwitchCameraType()
        {
            if (Android.Hardware.Camera.NumberOfCameras != 2)
            {
                return;
            }
            camera.StopPreview();
            camera.Release();

            if (Element.CameraType == CameraType.Rear)
            {
                SetupLiveCameraStream(0);
            }
            else
            {
                SetupLiveCameraStream(1);
            }
        }
Esempio n. 20
0
        public void ReceiveDetections(Detections detections)
        {
            if (!Element.IsScannerActive)
            {
                return;
            }
            SparseArray qrcodes = detections.DetectedItems;

            if (qrcodes.Size() != 0)
            {
                Vibrator vibrator = (Vibrator)Context.GetSystemService(Context.VibratorService);
                vibrator.Vibrate(100);
                Element.Barcode = ((Barcode)qrcodes.ValueAt(0)).RawValue;
                cam.StopPreview();
                Element.IsScannerActive = false;
                Element.Barcode         = null;
            }
        }
Esempio n. 21
0
 private void StopCamera()
 {
     if (_isRunning)
     {
         _camera?.StopPreview();
         _camera?.Release();
         _camera    = null;
         _isRunning = false;
     }
 }
Esempio n. 22
0
        protected override void OnStop()
        {
            if (Camera != null)
            {
                Camera.StopPreview();
                Camera.SetPreviewCallback(null);
                Camera.SetPreviewTexture(null);
            }

            cameraOn = false;
        }
Esempio n. 23
0
        public void ShutdownCamera()
        {
            tokenSource.Cancel();

            if (camera != null)
            {
                camera.SetPreviewCallback(null);
                camera.StopPreview();
                camera.Release();
                camera = null;
            }
        }
Esempio n. 24
0
    public void onPause()
    {
        if (inPreview)
        {
            camera.StopPreview();
        }

        camera.Release();
        camera = null;
        inPreview = false;
        base.OnPause();
    }
Esempio n. 25
0
 public bool OnSurfaceTextureDestroyed(SurfaceTexture surface)
 {
     if (_camera == null)
     {
         return(true);
     }
     _orientationListener.Disable();
     _camera.CancelAutoFocus();
     _camera.StopPreview();
     _camera.Release();
     return(true);
 }
 public void TakePhoto2()
 {
     while (takePictures)
     {
         (Element as StocktakingCamera).Countme();
         camera.StopPreview();
         var    ratio      = ((decimal)Height) / Width;
         var    image      = Bitmap.CreateBitmap(liveView.Bitmap, 0, 0, liveView.Bitmap.Width, (int)(liveView.Bitmap.Width * ratio));
         byte[] imageBytes = null;
         using (var imageStream = new System.IO.MemoryStream())
         {
             image.CompressAsync(Bitmap.CompressFormat.Jpeg, 50, imageStream);
             image.Recycle();
             imageBytes = imageStream.ToArray();
         }
         camera.StartPreview();
         (Element as StocktakingCamera).SetPhotoResult(imageBytes, liveView.Bitmap.Width, liveView.Bitmap.Height);
         Task.Delay(TimeSpan.FromSeconds(2));
         //return imageBytes;
     }
 }
        public void SwitchCamera(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                _camera.StopPreview();
                _camera.Release();
                _camera = null;
            }

            var cameraToSwitch = _cameraId == 0 ? 1 : 0;

            EnableCamera(cameraToSwitch);
        }
 private void releaseCameraAndPreview()
 {
     if (mCamera != null)
     {
         mCamera.StopPreview();
         mCamera.Release();
         mCamera = null;
     }
     if (mPreview != null)
     {
         mPreview.DestroyDrawingCache();
         mPreview.mCamera = null;
     }
 }
Esempio n. 29
0
        public async Task <byte[]> TakePhoto()
        {
            camera.StopPreview();
            var ratio = ((decimal)Height) / Width;
            var image = Bitmap.CreateBitmap(liveView.Bitmap, 0, 0, liveView.Bitmap.Width, (int)(liveView.Bitmap.Width * ratio));

            byte[] imageBytes = null;
            using (var imageStream = new System.IO.MemoryStream())
            {
                await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, imageStream);

                image.Recycle();
                imageBytes = imageStream.ToArray();
            }
            Parameters parameters = camera.GetParameters();

            parameters.FocusMode = Parameters.FocusModeContinuousPicture;
            parameters.FlashMode = Parameters.FlashModeAuto;
            //parameters.ColorEffect = Parameters.EffectMono;
            camera.SetParameters(parameters);
            camera.StartPreview();
            return(imageBytes);
        }
Esempio n. 30
0
        public void SwitchCamera(object sender, EventArgs e)
        {
            _revertButton.Enabled = false;

            if (_camera != null)
            {
                _camera.StopPreview();
                _camera.Release();
                _camera = null;
            }

            var cameraToSwitch = _cameraId == 0 ? 1 : 0;

            EnableCamera(cameraToSwitch);

            _revertButton.Enabled = true;
        }
Esempio n. 31
0
		public void OnPreviewFrame (byte[] data, Camera camera)
		{
			var parameters = camera.GetParameters ();
			var size = parameters.PreviewSize;

			var barcode = new Image (size.Width, size.Height, "Y800");
			barcode.SetData (data);

			var result = scanner.ScanImage (barcode);

			if (result == 0)
				return;
			

			camera.SetPreviewCallback (null);
			camera.StopPreview ();

			var scannerResult = GetScannerResult ();

			ScanComplete?.Invoke (this, new ScanCompleteEventArgs (scannerResult));
		}
Esempio n. 32
0
        private void StopPreview(Camera camera)
        {
            if (camera == null) { return; }

            _previewStarted = false;

            try { camera.StopPreview(); }
            catch (Exception)
            {
                // ignore: tried to stop a non-existent preview
            }
        }