public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) { mOrientation = newConfig.Orientation; SetTextureAspectRatio(mOrientation); ConfigureTransform(mPreviewSize.Width, mPreviewSize.Height); base.OnConfigurationChanged(newConfig); }
public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig) { orientation = newConfig.Orientation; this.Finish (); Intent intent = new Intent(this, typeof(MainActivity)); StartActivity(intent); base.OnConfigurationChanged (newConfig); }
/// <summary> /// 方向感知屏幕高度 /// </summary> /// <param name="orientation"></param> /// <returns></returns> public int GetOrientationAwareScreenHeight(Android.Content.Res.Orientation orientation) { if (orientation == Android.Content.Res.Orientation.Landscape) { return(DeviceScreenWidth); } else { return(DeviceScreenHeight); } }
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) { orientation = newConfig.Orientation; this.Finish(); Intent intent = new Intent(this, typeof(MainActivity)); StartActivity(intent); base.OnConfigurationChanged(newConfig); }
private int GetLayoutManagerOrientation(Android.Content.Res.Orientation activityOrientation) { if (activityOrientation == Android.Content.Res.Orientation.Portrait) { return(LinearLayoutManager.Vertical); } else { return(LinearLayoutManager.Horizontal); } }
private void SetTextureAspectRatio(Orientation orientation) { if (orientation == Orientation.Landscape) { mTextureView.SetAspectRatio(mPreviewSize.Width, mPreviewSize.Height); } else { mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width); } }
public void OpenCamera() { if (_context == null || OpeningCamera) { return; } OpeningCamera = true; _manager = (CameraManager)_context.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); _previewSize = map.GetOutputSizes(Java.Lang.Class.FromType(typeof(SurfaceTexture)))[0]; Android.Content.Res.Orientation orientation = Resources.Configuration.Orientation; if (orientation == Android.Content.Res.Orientation.Landscape) { _cameraTexture.SetAspectRatio(_previewSize.Width, _previewSize.Height); } else { _cameraTexture.SetAspectRatio(_previewSize.Height, _previewSize.Width); } // We are opening the camera with a listener. When it is ready, OnOpened of mStateListener is called. _manager.OpenCamera(cameraId, _mStateListener, null); System.Diagnostics.Debug.WriteLine(" [camera debug] camera is opened"); } catch (Java.Lang.Exception error) { System.Diagnostics.Debug.WriteLine($"{error.Message} {error.StackTrace}"); // _log.WriteLineTime(_tag + "\n" + // "OpenCamera() Failed to open camera \n " + // "ErrorMessage: \n" + // error.Message + "\n" + // "Stacktrace: \n " + // error.StackTrace); Available?.Invoke(this, false); } }
public Orientation ToCustomOrientation(Android.Content.Res.Orientation orientation) { switch (orientation) { case Android.Content.Res.Orientation.Portrait: return(Orientation.Portrait); case Android.Content.Res.Orientation.Landscape: return(Orientation.Landscape); default: return(Orientation.Portrait); } }
/// <summary> /// Opens the camera. /// </summary> public void OpenCamera() { if (_context == null || OpeningCamera) { return; } OpeningCamera = true; _manager = (CameraManager)_context.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); _previewSize = map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture)))[0]; Android.Content.Res.Orientation orientation = Resources.Configuration.Orientation; if (orientation == Android.Content.Res.Orientation.Landscape) { _cameraTexture.SetAspectRatio(_previewSize.Width, _previewSize.Height); } else { _cameraTexture.SetAspectRatio(_previewSize.Height, _previewSize.Width); } HandlerThread thread = new HandlerThread("CameraPreview"); thread.Start(); Handler backgroundHandler = new Handler(thread.Looper); // We are opening the camera with a listener. When it is ready, OnOpened of mStateListener is called. _manager.OpenCamera(cameraId, _stateListener, null); } catch (Java.Lang.Exception error) { Log.WriteLine(LogPriority.Error, error.Source, error.Message); Available?.Invoke(this, false); } catch (System.Exception error) { Log.WriteLine(LogPriority.Error, error.Source, error.Message); Available?.Invoke(this, false); } }
public void OpenCamera(bool IsSwitchCam) { cameraManager = (CameraManager)GetSystemService(Context.CameraService); try { SyncCameraPosition(IsSwitchCam); string cameraId = cameraManager.GetCameraIdList()[CurrentCameraIndex]; CameraCharacteristics characteristics = cameraManager.GetCameraCharacteristics(cameraId); StreamConfigurationMap map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); previewSize = map.GetOutputSizes(Java.Lang.Class.FromType(typeof(SurfaceTexture)))[0]; Android.Content.Res.Orientation orientation = Resources.Configuration.Orientation; if (orientation == Android.Content.Res.Orientation.Landscape) { autoFitTextureView.SetAspectRatio(previewSize.Width, previewSize.Height); } else { autoFitTextureView.SetAspectRatio(previewSize.Height, previewSize.Width); } HandlerThread thread = new HandlerThread("CameraPreview"); thread.Start(); Handler backgroundHandler = new Handler(thread.Looper); cameraManager.OpenCamera(cameraId, cameraStateListener, null); var available = (bool)characteristics.Get(CameraCharacteristics.FlashInfoAvailable); if (available) { FlashSupported = true; } else { FlashSupported = false; } } catch (Java.Lang.Exception error) { ShowToastMessage("Failed to open camera"); DebugMessage("ErrorMessage: \n" + error.Message + "\n" + "Stacktrace: \n " + error.StackTrace); } catch (System.Exception error) { ShowToastMessage("Failed to open camera"); DebugMessage("ErrorMessage: \n" + error.Message + "\n" + "Stacktrace: \n " + error.StackTrace); } }
private DesktopContentAdapter GetAdapter(Orientation orientation) { var adapter = _landScape; switch (orientation) { case Orientation.Landscape: adapter = _landScape; break; case Orientation.Portrait: adapter = _portrait; break; } return(adapter); }
private void SetAdapter(Orientation orientation, DesktopContentAdapter adapter) { switch (orientation) { case Orientation.Landscape: _landScape = adapter; break; case Orientation.Portrait: _portrait = adapter; break; default: _landScape = adapter; break; } }
public void SetVideoQuality(CamcorderQuality quality, Android.Content.Res.Orientation orientation) { CamcorderProfile profile = CamcorderProfile.Get(quality); if (profile == null) { profile = CamcorderProfile.Get(CamcorderQuality.High); } if (orientation == Android.Content.Res.Orientation.Landscape) { SetVideoSize(profile.VideoFrameWidth, profile.VideoFrameHeight); } else { SetVideoSize(profile.VideoFrameHeight, profile.VideoFrameWidth); } SetVideoCodec(profile.VideoCodec); SetBitRate(profile.VideoBitRate); SetFrameRate(profile.VideoFrameRate); }
/// <summary> /// Open the camera. Camera API used depends on the device's sdk version num /// </summary> /// <param name="cameraIndex"></param> private void OpenCamera(int cameraIndex = 0) { Activity activity = Activity; if (activity == null || activity.IsFinishing || mOpeningCamera) { return; } mOpeningCamera = true; try { // Camera2 API is only supported after Lollipop, but newer devices may not support Camera1 if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) { CameraManager manager = (CameraManager)activity.GetSystemService(Android.Content.Context.CameraService); string[] idList = manager.GetCameraIdList(); string cameraId = idList[cameraIndex]; switchCameraButton.Enabled = idList.Length > 1; switchCameraButton.Visibility = idList.Length > 1 ? ViewStates.Visible : ViewStates.Gone; // The onion skin has to be repositioned for the front facing camera if (cameraIndex == 1) { previousPhoto.SetScaleType(ImageView.ScaleType.FitStart); } else { previousPhoto.SetScaleType(ImageView.ScaleType.CenterCrop); } 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; textureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width); manager.OpenCamera(cameraId, mStateListener, null); } else { camera = Android.Hardware.Camera.Open(cameraIndex); camEnabled = true; textureView1.LayoutParameters = new FrameLayout.LayoutParams(width, height, GravityFlags.Center); if (cameraType == CameraFacing.Back) { previousPhoto.LayoutParameters = new FrameLayout.LayoutParams(width, height, GravityFlags.Fill); } else { previousPhoto.LayoutParameters = new FrameLayout.LayoutParams(width, height, GravityFlags.Top); } surfaceTexture = surface; camera.SetPreviewTexture(surface); takePhotoButton.Enabled = true; switchCameraButton.Enabled = true; switchCameraButton.Visibility = ViewStates.Visible; PrepareAndStartCamera1(); } } catch (CameraAccessException ex) { Toast.MakeText(activity, AppResources.Camera_noAccess, ToastLength.Short).Show(); Activity.Finish(); } }
// Opens a CameraDevice. The result is listened to by 'cameraStateListener'. private void OpenCamera() { var activity = this.Activity; if (activity == null || activity.IsFinishing) { return; } this.tracer.Debug("OpenCamera"); this.tracer.Debug("OpenCamera: this.facing={0}", this.facing.ToString()); var cameraManager = (CameraManager)activity.GetSystemService(Context.CameraService); try { if (!this.mCameraOpenCloseLock.TryAcquire(2500, TimeUnit.Milliseconds)) { const string ErrorMessage = "Time out waiting to lock camera opening."; this.tracer.Error(ErrorMessage); throw new RuntimeException(ErrorMessage); } string idForOpen = null; string[] camerasIds = cameraManager.GetCameraIdList(); foreach (string id in camerasIds) { CameraCharacteristics cameraCharacteristics = cameraManager.GetCameraCharacteristics(id); var cameraLensFacing = (Integer)cameraCharacteristics.Get(CameraCharacteristics.LensFacing); CameraFacingDirection cameraFacingDirection = ToCameraFacingDirection(cameraLensFacing); CameraFacingDirection configuredFacingDirection = ToCameraFacingDirection(this.facing); this.tracer.Debug("OpenCamera: cameraFacingDirection={0}, configuredFacingDirection={1}", cameraFacingDirection, configuredFacingDirection); if (cameraFacingDirection == configuredFacingDirection) { idForOpen = id; break; } } var cameraId = idForOpen ?? camerasIds[0]; this.tracer.Debug("OpenCamera: idForOpen={0}", idForOpen); this.tracer.Debug("OpenCamera: idForOpen={0}", idForOpen); this.tracer.Debug("OpenCamera: cameraId={0}", cameraId); // To get a list of available sizes of camera preview, we retrieve an instance of // StreamConfigurationMap from CameraCharacteristics CameraCharacteristics characteristics = cameraManager.GetCameraCharacteristics(cameraId); StreamConfigurationMap map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); this.previewSize = map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture)))[0]; // We assume that the top-most element is the one with the best resolution Orientation orientation = this.Resources.Configuration.Orientation; if (orientation == Orientation.Landscape) { this.autoFitTextureView.SetAspectRatio(this.previewSize.Width, this.previewSize.Height); } else { this.autoFitTextureView.SetAspectRatio(this.previewSize.Height, this.previewSize.Width); } ////this.ConfigureTransform(this.autoFitTextureView.Width, this.autoFitTextureView.Width); // We are opening the camera with a listener. When it is ready, OnOpened of cameraStateListener is called. cameraManager.OpenCamera(cameraId, this.cameraStateListener, null); } catch (CameraAccessException caex) { this.tracer.Exception(caex, "Cannot access the camera."); Toast.MakeText(activity, "Cannot access the camera.", ToastLength.Short).Show(); this.Activity.Finish(); } catch (NullPointerException npex) { this.tracer.Exception(npex, "This device doesn't support Camera2 API."); var dialog = new ErrorDialog(); dialog.Show(this.FragmentManager, "dialog"); } catch (InterruptedException e) { const string ErrorMessage = "Interrupted while trying to lock camera opening."; this.tracer.Exception(e, ErrorMessage); throw new RuntimeException(ErrorMessage); } catch (Exception ex) { this.tracer.Exception(ex); throw; } }
/// <summary> /// Obtain Orientation /// </summary> public static bool IsLandscape(Context context) { Android.Content.Res.Orientation orientation = context.Resources.Configuration.Orientation; return(orientation == Android.Content.Res.Orientation.Landscape); }
private void SetLayoutForOrientation(Android.Content.Res.Orientation orientation) { switch (orientation) { case Android.Content.Res.Orientation.Portrait: _txtTimer.LayoutParameters = new RelativeLayout.LayoutParams(300, RelativeLayout.LayoutParams.WrapContent); ((RelativeLayout.LayoutParams)(_txtTimer.LayoutParameters)).AddRule(LayoutRules.AlignParentRight); // _btnNew.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent); // ((RelativeLayout.LayoutParams)(_btnNew.LayoutParameters)).AddRule(LayoutRules.AlignParentRight); _grdXWord.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent); ((RelativeLayout.LayoutParams)(_grdXWord.LayoutParameters)).AddRule(LayoutRules.Below, idTitleText); //_txtWordSoFar.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent); //((RelativeLayout.LayoutParams)(_txtWordSoFar.LayoutParameters)).TopMargin = 30; //((RelativeLayout.LayoutParams)(_txtWordSoFar.LayoutParameters)).AddRule(LayoutRules.Below, idGrdXWord); //((RelativeLayout.LayoutParams)(_txtWordSoFar.LayoutParameters)).AddRule(LayoutRules.CenterHorizontal); // _txtScore.LayoutParameters = new GridLayout.LayoutParams(200, RelativeLayout.LayoutParams.WrapContent); ((RelativeLayout.LayoutParams)(_viewBorder.LayoutParameters)).AddRule(LayoutRules.Below, idGrdXWord); _gridLayoutLetterWheel.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent); ((RelativeLayout.LayoutParams)(_gridLayoutLetterWheel.LayoutParameters)).AddRule(LayoutRules.Below, idBorder); _ctrlWordList.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent); ((RelativeLayout.LayoutParams)(_ctrlWordList.LayoutParameters)).AddRule(LayoutRules.Below, idLtrWheelView); break; case Android.Content.Res.Orientation.Landscape: if (WindowManager.DefaultDisplay.Rotation == SurfaceOrientation.Rotation270) // tilted from portrait to right { _txtTimer.LayoutParameters = new RelativeLayout.LayoutParams(300, RelativeLayout.LayoutParams.WrapContent); ((RelativeLayout.LayoutParams)(_txtTimer.LayoutParameters)).AddRule(LayoutRules.AlignParentRight); _grdXWord.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent); ((RelativeLayout.LayoutParams)(_grdXWord.LayoutParameters)).AddRule(LayoutRules.Below, idTitleText); _ctrlWordList.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent); ((RelativeLayout.LayoutParams)(_ctrlWordList.LayoutParameters)).AddRule(LayoutRules.RightOf, idGrdXWord); _txtWordSoFar.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent); ((RelativeLayout.LayoutParams)(_txtWordSoFar.LayoutParameters)).TopMargin = 30; ((RelativeLayout.LayoutParams)(_txtWordSoFar.LayoutParameters)).AddRule(LayoutRules.RightOf, idWordList); ((RelativeLayout.LayoutParams)(_txtWordSoFar.LayoutParameters)).AddRule(LayoutRules.CenterHorizontal); _gridLayoutLetterWheel.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent); //((RelativeLayout.LayoutParams)(_gridLayoutLetterWheel.LayoutParameters)).AddRule(LayoutRules.RightOf, idWordList); //((RelativeLayout.LayoutParams)(_gridLayoutLetterWheel.LayoutParameters)).AddRule(LayoutRules.Below, idtxtWordSofar); } else { } break; } }
// 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. var sizes = Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)); Size largest = (Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), new CompareSizesByArea()); mImageReader = ImageReader.NewInstance(CameraConstants.CustomSize.Width, CameraConstants.CustomSize.Height, ImageFormatType.Jpeg, /*maxImages*/ 2); mImageReader.SetOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler); // 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(CameraConstants.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 > CameraConstants.MAX_PREVIEW_WIDTH) { maxPreviewWidth = CameraConstants.MAX_PREVIEW_WIDTH; } if (maxPreviewHeight > CameraConstants.MAX_PREVIEW_HEIGHT) { maxPreviewHeight = CameraConstants.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 = CameraHelper.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. mOrientation = Resources.Configuration.Orientation; SetTextureAspectRatio(mOrientation); // Check if the flash is supported. var available = (Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable); mFlashSupported = available != null ? (bool)available : false; mCameraId = cameraId; return; } } catch (CameraAccessException e) { e.PrintStackTrace(); } catch (NullPointerException) { // Currently an NPE is thrown when the Camera2API is used but not supported on the // device this code runs. ErrorDialog.NewInstance("Resource.String.camera_error").Show(ChildFragmentManager, CameraConstants.FRAGMENT_DIALOG); } }