Exemplo n.º 1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            SetTheme(Resource.Style.AppThemeClearStatusBar);
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_camera);

            //CrashLytics
            Fabric.Fabric.With(this, new Crashlytics.Crashlytics());
            Crashlytics.Crashlytics.HandleManagedExceptions();

            RequestPermissions(permissions, 0);

            if (Resources.Configuration.Orientation == Android.Content.Res.Orientation.Landscape)
            {
                RequestedOrientation = ScreenOrientation.Portrait;
            }

            toggleFlashButton       = FindViewById <ImageButton>(Resource.Id.toggleFlashButton);
            takePictureButton       = FindViewById <Button>(Resource.Id.takePictureButton);
            openGalleryButton       = FindViewById <ImageButton>(Resource.Id.openGalleryButton);
            openDefaultCameraButton = FindViewById <ImageButton>(Resource.Id.openDefaultCamera);

            cameraPreview = FindViewById <FrameLayout>(Resource.Id.cameraView);

            imageManager = new ImageManager();
            imageManager.AddOnImageResultListener(delegate(Bitmap bitmap, string path, Exception ex)
            {
                if (path != null)
                {
                    StopCamera();
                    Intent intent = new Intent(this, typeof(MainViewActivity));
                    intent.PutExtra("image", path);
                    StartActivity(intent);
                }
                else if (ex != null)
                {
                    Toast.MakeText(Application.Context, ex.Message, ToastLength.Short).Show();
                    StartCamera();
                }
                else
                {
                    StartCamera();
                }
            });

            //Выбор режима activity на весь экран
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {
                Window w = Window;
                w.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
            }

            takePictureButton.Click += delegate
            {
                TakePicture();
            };

            toggleFlashButton.Click += delegate
            {
                ToggleFlash();
            };

            openGalleryButton.Click += delegate
            {
                imageManager.PickPhoto();
            };

            openDefaultCameraButton.Click += delegate
            {
                StopCamera();
                imageManager.TakePhoto();
            };

            //Отслеживание смены угла экрана
            orientationListener = new OrientationListener(this, delegate(int angle)
            {
                if (lastAngle != angle)
                {
                    if (lastAngle == -90 && angle == 180)
                    {
                        angle = -180;
                    }
                    toggleFlashButton.Animate().Rotation(angle).Start();
                    openGalleryButton.Animate().Rotation(angle).Start();
                    openDefaultCameraButton.Animate().Rotation(angle).Start();
                    if (angle == -180)
                    {
                        lastAngle = 180;
                    }
                    else
                    {
                        lastAngle = angle;
                    }
                }
            });
        }