示例#1
0
        private static Size ChooseOptimalSize(Size[] choices, int textureViewWidth,
                                              int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio)
        {
            // Collect the supported resolutions that are at least as big as the preview Surface
            var bigEnough = new List <Size>();
            // Collect the supported resolutions that are smaller than the preview Surface
            var notBigEnough = new List <Size>();

            int w = aspectRatio.Width;
            int h = aspectRatio.Height;

            for (var i = 0; i < choices.Length; i++)
            {
                Size option = choices[i];
                if (option.Height == option.Width * h / w)
                {
                    if (option.Width >= textureViewWidth &&
                        option.Height >= textureViewHeight)
                    {
                        bigEnough.Add(option);
                    }
                    else
                    {
                        notBigEnough.Add(option);
                    }
                }
            }

            // Pick the smallest of those big enough. If there is no one big enough, pick the
            // largest of those not big enough.
            if (bigEnough.Count > 0)
            {
                return((Size)Collections.Min(bigEnough, new CompareSizesByArea()));
            }
            else if (notBigEnough.Count > 0)
            {
                return((Size)Collections.Max(notBigEnough, new CompareSizesByArea()));
            }
            else
            {
                Log.Error(TAG, "Couldn't find any suitable preview size");
                return(choices[0]);
            }
        }
示例#2
0
        // Sets up member variables related to camera.
        private void SetUpCameraOutputs(int width, int height)
        {
            var activity = Activity;
            var manager  = (CameraManager)activity.GetSystemService(Context.CameraService);

            try
            {
                for (var i = 0; i < manager.GetCameraIdList().Length; i++)
                {
                    var cameraId = manager.GetCameraIdList()[i];
                    CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId);

                    // We don't use a front facing camera in this sample.
                    var facing = (Integer)characteristics.Get(CameraCharacteristics.LensFacing);
                    if (facing != null && facing == (Integer.ValueOf((int)LensFacing.Back)))
                    {
                        continue;
                    }

                    var map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);
                    if (map == null)
                    {
                        continue;
                    }

                    Size largest = (Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), new CompareSizesByArea());
                    mImageReader = ImageReader.NewInstance(largest.Width, largest.Height, ImageFormatType.Jpeg, /*maxImages*/ 2);
                    mImageReader.SetOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);

                    Point displaySize = new Point();
                    activity.WindowManager.DefaultDisplay.GetSize(displaySize);
                    var rotatedPreviewWidth  = height;
                    var rotatedPreviewHeight = width;
                    var maxPreviewWidth      = displaySize.Y;
                    var maxPreviewHeight     = displaySize.X;


                    // Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
                    // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
                    // garbage capture data.
                    mPreviewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))),
                                                     rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                                                     maxPreviewHeight, largest);

                    // We fit the aspect ratio of TextureView to the size of preview we picked.
                    mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width);

                    // Check if the flash is supported.
                    var available = (Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable);
                    if (available == null)
                    {
                        mFlashSupported = false;
                    }
                    else
                    {
                        mFlashSupported = (bool)available;
                    }

                    mCameraId = cameraId;
                    return;
                }
            }
            catch (CameraAccessException e)
            {
                e.PrintStackTrace();
            }
            catch (NullPointerException e)
            {
                // Currently an NPE is thrown when the Camera2API is used but not supported on the
                // device this code runs.
                //ErrorDialog.NewInstance(GetString(Resource.String.camera_error)).Show(ChildFragmentManager, FRAGMENT_DIALOG);
            }
        }
示例#3
0
        /************ STUFF THAT WAS PART OF FRAGMENT **************/
        //public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        //{
        //    return inflater.Inflate(Resource.Layout.fragment_camera2_basic, container, false);
        //}

        //public override void OnViewCreated(View view, Bundle savedInstanceState)
        //{
        //    mTextureView = (AutoFitTextureView)view.FindViewById(Resource.Id.texture);
        //    view.FindViewById(Resource.Id.picture).SetOnClickListener(this);
        //    view.FindViewById(Resource.Id.info).SetOnClickListener(this);
        //}

        //public override void OnActivityCreated(Bundle savedInstanceState)
        //{
        //    base.OnActivityCreated(savedInstanceState);
        //    mFile = new File(Activity.GetExternalFilesDir(null), "pic.jpg");
        //}


        //this was used in fragment, requires Android.Support.V13
        //private void RequestCameraPermission()
        //{
        //    if (FragmentCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.Camera))
        //    {
        //        new ConfirmationDialog().Show(ChildFragmentManager, FRAGMENT_DIALOG);
        //    }
        //    else
        //    {
        //        FragmentCompat.RequestPermissions(this, new string[] { Manifest.Permission.Camera }, REQUEST_CAMERA_PERMISSION);
        //    }
        //}

        //public void OnRequestPermissionsResult(int requestCode, string[] permissions, int[] grantResults)
        //{
        //    if (requestCode != REQUEST_CAMERA_PERMISSION)
        //    {
        //        return;
        //    }

        //    if (grantResults.Length != 1 || grantResults[0] != (int)Permission.Granted)
        //    {
        //        ErrorDialog.NewInstance(GetString(Resource.String.request_permission)).Show(ChildFragmentManager, FRAGMENT_DIALOG);
        //    }
        //}


        // Sets up member variables related to camera.

        private void SetUpCameraOutputs(int width, int height)
        {
            //this line was with Fragment.Activity
            //var activity = Activity;

            var manager = (CameraManager)GetSystemService(Context.CameraService);

            try
            {
                for (var i = 0; i < manager.GetCameraIdList().Length; i++)
                {
                    var cameraId = manager.GetCameraIdList()[i];
                    CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId);

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

                    var map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);
                    if (map == null)
                    {
                        continue;
                    }


                    // For still image captures, we use the largest available size.
                    Android.Util.Size largest = (Android.Util.Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)),
                                                                                   new CompareSizesByArea());
                    mImageReader = ImageReader.NewInstance(largest.Width / 2, largest.Height / 2, ImageFormatType.Jpeg, /*maxImages*/ 2);
                    //mImageReader = ImageReader.NewInstance(1280, 720, ImageFormatType.Yuv420888, 2);
                    //mImageReader = ImageReader.NewInstance(960, 540, ImageFormatType.Jpeg, 2);
                    mImageReader.SetOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);

                    // Find out if we need to swap dimension to get the preview size relative to sensor
                    // coordinate.
                    var displayRotation = WindowManager.DefaultDisplay.Rotation;
                    //noinspection ConstantConditions
                    mSensorOrientation = (int)characteristics.Get(CameraCharacteristics.SensorOrientation);
                    bool swappedDimensions = false;
                    switch (displayRotation)
                    {
                    case SurfaceOrientation.Rotation0:
                    case SurfaceOrientation.Rotation180:
                        if (mSensorOrientation == 90 || mSensorOrientation == 270)
                        {
                            swappedDimensions = true;
                        }
                        break;

                    case SurfaceOrientation.Rotation90:
                    case SurfaceOrientation.Rotation270:
                        if (mSensorOrientation == 0 || mSensorOrientation == 180)
                        {
                            swappedDimensions = true;
                        }
                        break;

                    default:
                        Log.Error(TAG, "Display rotation is invalid: " + displayRotation);
                        break;
                    }

                    var displaySize = new Android.Graphics.Point();
                    WindowManager.DefaultDisplay.GetSize(displaySize);
                    var rotatedPreviewWidth  = width;
                    var rotatedPreviewHeight = height;
                    var maxPreviewWidth      = displaySize.X;
                    var maxPreviewHeight     = displaySize.Y;

                    if (swappedDimensions)
                    {
                        rotatedPreviewWidth  = height;
                        rotatedPreviewHeight = width;
                        maxPreviewWidth      = displaySize.Y;
                        maxPreviewHeight     = displaySize.X;
                    }

                    if (maxPreviewWidth > MAX_PREVIEW_WIDTH)
                    {
                        maxPreviewWidth = MAX_PREVIEW_WIDTH;
                    }

                    if (maxPreviewHeight > MAX_PREVIEW_HEIGHT)
                    {
                        maxPreviewHeight = MAX_PREVIEW_HEIGHT;
                    }

                    // Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
                    // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
                    // garbage capture data.
                    mPreviewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))),
                                                     rotatedPreviewWidth / 2, rotatedPreviewHeight / 2, maxPreviewWidth / 2, maxPreviewHeight / 2, largest);
                    //mPreviewSize = new Android.Util.Size(1280, 720);
                    //mPreviewSize = new Android.Util.Size(960, 540);

                    // We fit the aspect ratio of TextureView to the size of preview we picked.
                    var orientation = Resources.Configuration.Orientation;
                    if (orientation == Orientation.Landscape)
                    {
                        mTextureView.SetAspectRatio(mPreviewSize.Width, mPreviewSize.Height);
                    }
                    else
                    {
                        mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width);
                    }

                    // Check if the flash is supported.
                    var available = (Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable);
                    if (available == null)
                    {
                        mFlashSupported = false;
                    }
                    else
                    {
                        mFlashSupported = (bool)available;
                    }

                    mCameraId = cameraId;
                    return;
                }
            }
            catch (CameraAccessException e)
            {
                e.PrintStackTrace();
            }
            catch (NullPointerException e)
            {
                // Currently an NPE is thrown when the Camera2API is used but not supported on the
                // device this code runs.
                //ErrorDialog.NewInstance(GetString(Resource.String.camera_error)).Show(ChildFragmentManager, FRAGMENT_DIALOG);
            }
        }