Exemple #1
0
        //this should ultimately be user driven like google photos can recycle code below later to find out hardware capabilites when giving users options
        private void SetVideoSize(CameraFacing camface) //determines cameras supported sizes and sets frame sizes for video as best as possible
        {
            CameraManager manager = (CameraManager)GetSystemService(Context.CameraService);
            var           cams    = manager.GetCameraIdList();

            LensFacing lensface = camface == CameraFacing.Back ? LensFacing.Back : LensFacing.Front;

            foreach (var camid in cams)
            {
                var camprops = manager.GetCameraCharacteristics(camid);

                if ((int)camprops.Get(CameraCharacteristics.LensFacing) == (int)lensface)
                {
                    var    map   = (StreamConfigurationMap)camprops.Get(CameraCharacteristics.ScalerStreamConfigurationMap);
                    Size[] sizes = map.GetOutputSizes((int)ImageFormatType.Jpeg);
                    if (sizes.ToList().Any(s => s.Width == 1280 && s.Height == 720)) //if this format is supported then always use it, this is 16:9 aspect HD
                    {
                        pFramewidth  = 1280;
                        pFrameHeight = 720;
                    }
                    else //if (sizes.ToList().Any(s => s.Width == 640 && s.Height == 480)) //if HD is not supported use this default
                    {//if HD is not supported use this default
                        pFramewidth  = 640;
                        pFrameHeight = 480;
                    }
                }
            }
        }
        void GetCameraIds()
        {
            try
            {
                CameraManager manager = (CameraManager)Context.GetSystemService(Context.CameraService);

                string[] cameraIds = manager.GetCameraIdList();

                foreach (string id in cameraIds)
                {
                    CameraCharacteristics characteristics = manager.GetCameraCharacteristics(id);

                    LensFacing lensFacing = (LensFacing)(int)characteristics.Get(CameraCharacteristics.LensFacing);

                    if (lensFacing == LensFacing.Back)
                    {
                        BackCameraAvailable = true;
                        backCameraId        = id;
                    }
                    else if (lensFacing == LensFacing.Front)
                    {
                        FrontCameraAvailable = true;
                        frontCameraId        = id;
                    }
                }
            }
            catch (CameraAccessException)
            {
                Toast.MakeText(Context, "Cannot access the camera.", ToastLength.Short).Show();
            }
            catch (NullPointerException)
            {
                Toast.MakeText(Context, "This device doesn't support Camera2 API.", ToastLength.Short).Show();
            }
        }
Exemple #3
0
        /// <summary>
        /// This method forces our view to re-create the camera session by changing 'currentLensFacing' and requesting the original value
        /// </summary>
        private void ForceResetLensFacing()
        {
            var targetLensFacing = currentLensFacing;

            currentLensFacing = currentLensFacing == LensFacing.Back ? LensFacing.Front : LensFacing.Back;
            SetLensFacing(targetLensFacing);
        }
Exemple #4
0
        //Initialize the instance of the object
        private void initInstance(Context context, LensFacing lensFacing, Handler handler)
        {
            //mContext = context;
            mHandler    = handler;
            _lensFacing = lensFacing;

            mCameraStateCallBack               = new CameraStateCallBack(this);
            mCaptureSessionStateCallBack       = new CaptureSessionStateCallBack(this, false);
            mSingleCaptureSessionStateCallBack = new CaptureSessionStateCallBack(this, true);

            //get the Camera Manager
            var manager = (CameraManager)context.GetSystemService(Context.CameraService);

            mCameraManager = manager;
            effects        = new List <ControlEffectMode>();
            effects.Add(ControlEffectMode.Off);
            effects.Add(ControlEffectMode.Mono);
            effects.Add(ControlEffectMode.Negative);
            effects.Add(ControlEffectMode.Sepia);
            effects.Add(ControlEffectMode.Posterize);
            effects.Add(ControlEffectMode.Whiteboard);
            effects.Add(ControlEffectMode.Blackboard);
            effects.Add(ControlEffectMode.Aqua);

            _selEffect = effects[0];
            //open the camera
            openCamera();
        }
Exemple #5
0
        private void SetUpCameraOutputs(int width, int height, LensFacing lensFacing)
        {
            _manager = (CameraManager)_context.GetSystemService(Context.CameraService);

            string[] cameraIds = _manager.GetCameraIdList();

            _cameraId = cameraIds[0];

            for (int i = 0; i < cameraIds.Length; i++)
            {
                CameraCharacteristics chararc = _manager.GetCameraCharacteristics(cameraIds[i]);

                var facing = (Integer)chararc.Get(CameraCharacteristics.LensFacing);
                if (facing != null && facing == (Integer.ValueOf((int)LensFacing.Front)))
                {
                    continue;
                }

                _cameraId = cameraIds[i];
            }

            var characteristics = _manager.GetCameraCharacteristics(_cameraId);
            var map             = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);

            if (_supportedJpegSizes == null && characteristics != null)
            {
                _supportedJpegSizes = ((StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap)).GetOutputSizes((int)ImageFormatType.Jpeg);
            }

            if (_supportedJpegSizes != null && _supportedJpegSizes.Length > 0)
            {
                _idealPhotoSize = GetOptimalSize(_supportedJpegSizes, 1050, 1400); //MAGIC NUMBER WHICH HAS PROVEN TO BE THE BEST
            }

            _imageReader = ImageReader.NewInstance(_idealPhotoSize.Width, _idealPhotoSize.Height, ImageFormatType.Jpeg, 1);

            var readerListener = new ImageAvailableListener();

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

            var available = (Java.Lang.Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable);

            if (available == null)
            {
                _flashSupported = false;
            }
            else
            {
                _flashSupported = (bool)available;
            }

            _imageReader.SetOnImageAvailableListener(readerListener, _backgroundHandler);

            _previewSize = GetOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))), width, height);
        }
        /// <summary>
        /// Gets the rear camera if possible, if not, then uses the front camera.
        /// </summary>
        /// <returns></returns>
        private string GetCameraIdForOrientation(LensFacing facingToMatch)
        {
            CameraCharacteristics characteristics = null;

            return(cameraManager.GetCameraIdList().FirstOrDefault(id => {
                characteristics = cameraManager.GetCameraCharacteristics(id);
                int lensFacing = (int)characteristics.Get(CameraCharacteristics.LensFacing);
                return lensFacing == (int)facingToMatch;
            }));
        }
Exemple #7
0
 private void CameraHandle()
 {
     FindViewById <Button>(Resource.Id.takepic).Click += (o, e) =>
     {
         CameraFacing = FindViewById <RadioButton>(FindViewById <RadioGroup>(Resource.Id.radioGroup)
                                                   .CheckedRadioButtonId).Text == "Front Camera" ? LensFacing.Front
                                                                 : LensFacing.Back;
         ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.Camera },
                                           REQUEST_CAMERA);
     };
 }
Exemple #8
0
        private void SetLensFacing(LensFacing lenseFacing)
        {
            bool shouldRestartCamera = currentLensFacing != lenseFacing;

            currentLensFacing = lenseFacing;
            string cameraId = string.Empty;

            characteristics = null;

            foreach (var id in manager.GetCameraIdList())
            {
                cameraId        = id;
                characteristics = manager.GetCameraCharacteristics(id);

                var face = (int)characteristics.Get(CameraCharacteristics.LensFacing);
                if (face == (int)currentLensFacing)
                {
                    break;
                }
            }

            if (characteristics == null)
            {
                return;
            }

            if (cameraDevice != null)
            {
                try
                {
                    if (!shouldRestartCamera)
                    {
                        return;
                    }
                    if (cameraDevice.Handle != IntPtr.Zero)
                    {
                        cameraDevice.Close();
                        cameraDevice.Dispose();
                        cameraDevice = null;
                    }
                }
                catch (Exception e)
                {
                    //Ignored
                    System.Diagnostics.Debug.WriteLine(e);
                }
            }

            SetUpCameraOutputs(cameraId);
            ConfigureTransform(surfaceTextureView.Width, surfaceTextureView.Height);
            manager.OpenCamera(cameraId, cameraStateCallback, null);
        }
Exemple #9
0
        private int getJpegOrientation(int sensorOrientation, int deviceOrientation, LensFacing lensFacing)
        {
            // Round device orientation to a multiple of 90
            deviceOrientation = (deviceOrientation + 45) / 90 * 90;

            // Reverse device orientation for front-facing cameras
            if (lensFacing == LensFacing.Front)
            {
                deviceOrientation = -deviceOrientation;
            }

            // Calculate desired JPEG orientation relative to camera orientation to make
            // the image upright relative to the device orientation
            int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360;

            return(jpegOrientation);
        }
Exemple #10
0
        //funtion to obtain the camera id with the lensFacing
        private static string getId(CameraManager manager, LensFacing lensFacing)
        {
            //Obtain all of the disponibles ids
            var ids = manager.GetCameraIdList();

            //verify all cameras characteristics
            foreach (var id in ids)
            {
                //get the characteristics
                var c = manager.GetCameraCharacteristics(id);

                //if the camera has the same lens facing return it
                if ((int)c.Get(CameraCharacteristics.LensFacing) == (int)lensFacing)
                {
                    return(id);
                }
            }
            //return this when can't find any camera
            return("NotSupported");
        }
 public void SetCameraOption(CameraOptions cameraOptions)
 {
     this.lensFacing = (cameraOptions == CameraOptions.Front) ? LensFacing.Front : LensFacing.Back;
 }
Exemple #12
0
 //Basic constructor
 public ACamera(Context context, LensFacing lensFacing, Handler handler)
 {
     initInstance(context, lensFacing, handler);
 }
Exemple #13
0
 //init instance with preview
 public ACamera(Context context, LensFacing lensFacing, Handler handler, TextureView preview)
 {
     initInstance(context, lensFacing, handler);
     TextureView = preview;
 }