/// <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(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); } 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.WriteLineTime(_tag + "\n" + "OpenCamera() Failed to open camera \n " + "ErrorMessage: \n" + error.Message + "\n" + "Stacktrace: \n " + error.StackTrace); Available?.Invoke(this, false); } catch (System.Exception error) { _log.WriteLineTime(_tag + "\n" + "OpenCamera() Failed to open camera \n " + "ErrorMessage: \n" + error.Message + "\n" + "Stacktrace: \n " + error.StackTrace); Available?.Invoke(this, false); } }
private void LockRotation(Android.Content.Res.Orientation orientation) { switch (orientation) { case Android.Content.Res.Orientation.Portrait: RequestedOrientation = ScreenOrientation.Portrait; break; case Android.Content.Res.Orientation.Landscape: RequestedOrientation = ScreenOrientation.Landscape; break; } }
private void OrientationBasedUi(Orientation orientation) { var windowManager = this.WindowManager; var metrics = new DisplayMetrics(); windowManager.DefaultDisplay.GetMetrics(metrics); if (_adapter != null) { var size = orientation == Orientation.Portrait ? metrics.WidthPixels / 2 : metrics.WidthPixels / 4; _adapter.SetLayoutParams(size); } _gridView.SetNumColumns(orientation == Orientation.Portrait ? 2 : 4); }
private void SetBackground(Android.Content.Res.Orientation toOrientation = Android.Content.Res.Orientation.Undefined) { if (toOrientation == Android.Content.Res.Orientation.Undefined) { toOrientation = Activity.Resources.Configuration.Orientation; } try { string imgPath = ""; if (toOrientation == Android.Content.Res.Orientation.Portrait) { imgPath = "bg-portrait.png"; } else { imgPath = "bg-landscape.png"; } if (imgPath == "") { return; } System.IO.Stream stream = Resources.Assets.Open(imgPath); //View.Background = Drawable.CreateFromStream(stream, null); if (_backImage == null) { _backImage = new ImageView(Activity); _backImage.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent); _backImage.SetScaleType(ImageView.ScaleType.CenterCrop); var view = View.Parent as FrameLayout; view.AddView(_backImage, 0); View.SetBackgroundColor(Color.Transparent); } _backImage.SetImageDrawable(Drawable.CreateFromStream(stream, null)); } catch (Exception ex) { Log.Error("EdicolaFragment - SetBackground", ex.Message); } }
private void FixElementsPosition(Orientation orientation) { var captureButton = (Button)FindViewById(Resource.Id.button_capture); FrameLayout.LayoutParams layout = (FrameLayout.LayoutParams)captureButton.LayoutParameters; switch (orientation) { case Orientation.Landscape: layout.Gravity = GravityFlags.Right | GravityFlags.Center; break; case Orientation.Portrait: layout.Gravity = GravityFlags.Bottom | GravityFlags.Center; break; } }
// 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"); } }
private void SetUpForOrientation(Orientation orientation) { ViewGroup.LayoutParams param; switch (orientation) { case Orientation.Landscape: param = RootView.LayoutParameters; param.Height = ViewGroup.LayoutParams.WrapContent; RootView.LayoutParameters = param; break; case Orientation.Portrait: case Orientation.Square: case Orientation.Undefined: param = RootView.LayoutParameters; param.Height = ViewGroup.LayoutParams.MatchParent; RootView.LayoutParameters = param; break; default: throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null); } }
// 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 { bool front = Arguments.GetBoolean(EXTRA_USE_FRONT_FACING_CAMERA); if (front) { Log.Debug("VoiceCamera", "Front specified"); } else { Log.Debug("VoiceCamera", "Back Specified"); } Log.Debug("VoiceCamera", "Front ID: " + (int)LensFacing.Front + " Back ID: " + (int)LensFacing.Back); foreach (var cameraId in manager.GetCameraIdList()) { // To get a list of available sizes of camera preview, we retrieve an instance of // StreamConfigurationMap from CameraCharacteristics var characteristics = manager.GetCameraCharacteristics(cameraId); var lens = characteristics.Get(CameraCharacteristics.LensFacing); Log.Debug("VoiceCamera", "LensFacing: " + lens.ToString()); if (!IsCameraSpecified) { Log.Debug("VoiceCamera", "camera not specified"); if ((int)lens == (int)LensFacing.Front) { Log.Debug("VoiceCamera", "Skip front facing camera"); //we don't use a front facing camera in this sample. continue; } } else if (!ShouldUseCamera((int)lens)) { Log.Debug("VoiceCamera", "Skipping over camera, should not use"); //TODO: Handle case where there is no camera match continue; } Log.Debug("VoiceCamera", "Using this camera"); 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); return; } } 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"); } }
protected override void OnConfigurationChanged(ARes.Configuration newConfig) { base.OnConfigurationChanged(newConfig); orientationChanged = true; viewOrientation = newConfig.Orientation; }
/// <summary> /// 方向感知屏幕高度 /// </summary> /// <param name="orientation"></param> /// <returns></returns> public int GetOrientationAwareScreenHeight(Android.Content.Res.Orientation orientation) { return(orientation == Android.Content.Res.Orientation.Landscape ? DeviceScreenWidth : DeviceScreenHeight); }