Inheritance: DialogFragment
 public MyDialogOnClickListener(ErrorDialog e)
 {
     er = e;
 }
Exemplo n.º 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.Front)))
                    {
                        continue;
                    }

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

                    // For still image captures, we use the largest available size.
                    //Size largest = (Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)),
                    //    new CompareSizesByArea());

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

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

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

                    Point displaySize = new Point();
                    activity.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.
                    //_previewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))),
                    //    rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                    //    maxPreviewHeight, largest);
                    _previewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))),
                                                     rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                                                     maxPreviewHeight, new Size(maxPreviewWidth, maxPreviewHeight));

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

                    _cameraId = 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);
            }
        }
		// Opens a CameraDevice. The result is listened to by 'mStateListener'.
		private void OpenCamera()
		{
			Activity activity = Activity;
			if (activity == null || activity.IsFinishing || mOpeningCamera) {
				return;
			}
			mOpeningCamera = true;
			CameraManager manager = (CameraManager)activity.GetSystemService (Context.CameraService);
			try 
			{
				string cameraId = manager.GetCameraIdList()[0];

				// To get a list of available sizes of camera preview, we retrieve an instance of
				// StreamConfigurationMap from CameraCharacteristics
				CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId);
				StreamConfigurationMap map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);
				mPreviewSize = map.GetOutputSizes(Java.Lang.Class.FromType(typeof(SurfaceTexture)))[0];
				Android.Content.Res.Orientation orientation = Resources.Configuration.Orientation;
				if (orientation == Android.Content.Res.Orientation.Landscape)
				{
					mTextureView.SetAspectRatio(mPreviewSize.Width, mPreviewSize.Height);
				}
				else 
				{
					mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width);
				}

				// We are opening the camera with a listener. When it is ready, OnOpened of mStateListener is called.
				manager.OpenCamera(cameraId, mStateListener, null);
			}
			catch (CameraAccessException ex) {
				Toast.MakeText (activity, "Cannot access the camera.", ToastLength.Short).Show ();
				Activity.Finish ();
			} catch (NullPointerException) {
				var dialog = new ErrorDialog ();
				dialog.Show (FragmentManager, "dialog");
			}
		}
			public MyDialogOnClickListener(ErrorDialog e)
			{
				er = e;
			}
Exemplo n.º 5
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.Front)))
                    {
                        continue;
                    }

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

                    // Find out if we need to swap dimension to get the preview size relative to sensor
                    // coordinate.
                    var displayRotation = activity.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;
                    }

                    Point displaySize = new Point();
                    activity.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 = new Size(960, 540);

                    // 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);
            }
        }