public void SetupCamera()
        {
            if (Camera != null)
            {
                return;
            }

            var perf = PerformanceCounter.Start();

            OpenCamera();
            PerformanceCounter.Stop(perf, "Setup Camera took {0}ms");

            if (Camera == null)
            {
                return;
            }

            perf = PerformanceCounter.Start();
            ApplyCameraSettings();

            try
            {
                Camera.SetPreviewDisplay(holder);


                var previewParameters = Camera.GetParameters();
                var previewSize       = previewParameters.PreviewSize;
                var bitsPerPixel      = ImageFormat.GetBitsPerPixel(previewParameters.PreviewFormat);


                var       bufferSize          = (previewSize.Width * previewSize.Height * bitsPerPixel) / 8;
                const int NUM_PREVIEW_BUFFERS = 5;
                for (uint i = 0; i < NUM_PREVIEW_BUFFERS; ++i)
                {
                    using (var buffer = new FastJavaByteArray(bufferSize))
                        Camera.AddCallbackBuffer(buffer);
                }

                Camera.StartPreview();

                Camera.SetNonMarshalingPreviewCallback(cameraEventListener);

                // Docs suggest if Auto or Macro modes, we should invoke AutoFocus at least once
                var currentFocusMode = Camera.GetParameters().FocusMode;
                if (currentFocusMode == Camera.Parameters.FocusModeAuto ||
                    currentFocusMode == Camera.Parameters.FocusModeMacro)
                {
                    AutoFocus();
                }
            }
            catch (Exception ex)
            {
                Android.Util.Log.Debug(MobileBarcodeScanner.TAG, ex.ToString());
                return;
            }
            finally
            {
                PerformanceCounter.Stop(perf, "Setup Camera Parameters took {0}ms");
            }
        }
Esempio n. 2
0
        public void SetupCamera()
        {
            if (camera != null)
            {
                return;
            }

            PermissionsHandler.CheckCameraPermissions(context);

            OpenCamera();

            if (camera == null)
            {
                return;
            }

            ApplyCameraSettings();

            try
            {
                camera.SetPreviewDisplay(holder);

                var previewParameters = camera.GetParameters();
                var previewSize       = previewParameters.PreviewSize;
                var bitsPerPixel      = ImageFormat.GetBitsPerPixel(previewParameters.PreviewFormat);

                int       bufferSize          = (previewSize.Width * previewSize.Height * bitsPerPixel) / 8;
                const int NUM_PREVIEW_BUFFERS = 5;
                for (uint i = 0; i < NUM_PREVIEW_BUFFERS; ++i)
                {
                    using (var buffer = new FastJavaByteArray(bufferSize))
                        camera.AddCallbackBuffer(buffer);
                }

                camera.StartPreview();

                camera.SetNonMarshalingPreviewCallback(cameraEventListener);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return;
            }
            finally
            {
            }
        }
      public void OnPreviewFrame(byte[] data, Camera camera)
      {
         if (!_busy && ImagePreview != null)
            try
            {
               _busy = true;
               Camera.Size cSize = camera.GetParameters().PreviewSize;
               _imageSize = new Size(cSize.Width, cSize.Height);
               Size size = _imageSize;
               Image<Bgr, Byte> image = _bgrBuffers.GetBuffer(size, 0);

               GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
               using (Image<Gray, Byte> yuv420sp = new Image<Gray, byte>(size.Width, (size.Height >> 1) * 3, size.Width, handle.AddrOfPinnedObject()))
               {
                  ImagePreview(this, new ImagePreviewEventArgs(yuv420sp, image));
               }
               handle.Free();

               Invalidate();
            }
            finally
            {
               _busy = false;
            }

         if (_cameraPreviewCallbackWithBuffer)
            camera.AddCallbackBuffer(data);
      }