コード例 #1
0
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            camera = Android.Hardware.Camera.Open();
            var parameters = camera.GetParameters();



            parameters.FocusMode = Parameters.FocusModeContinuousPicture;
            parameters.FlashMode = Parameters.FlashModeAuto;
            var aspect = ((decimal)height) / ((decimal)width);


            var previewSize = parameters.SupportedPreviewSizes.First();



            System.Diagnostics.Debug.WriteLine($"Preview sizes: {parameters.SupportedPreviewSizes.Count}");
            var supportedPreviewSizes = parameters.SupportedPreviewSizes;


            parameters.SetPreviewSize(previewSize.Width, previewSize.Height);
            System.Diagnostics.Debug.WriteLine(previewSize.Width + " " + previewSize.Height);
            camera.SetParameters(parameters);

            camera.SetPreviewTexture(surface);
            StartCamera();
        }
コード例 #2
0
        public async void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            try
            {
                _camera = Android.Hardware.Camera.Open((int)CameraFacing.Back);
                determineDisplayOrientation();
                setupCamera();

                _textureView.LayoutParameters = new Android.Widget.FrameLayout.LayoutParams(width, height);
                _surfaceTexture = surface;
                _camera.SetPreviewTexture(_surfaceTexture);
                _camera.StartPreview();
            }
            catch (Exception)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(_activity);

                builder.SetMessage("Die Kamera kann nicht geöffnet werden.");
                builder.SetTitle("Fehler");

                AlertDialog dialog = builder.Create();

                dialog.Show();
                await Element.Navigation.PopModalAsync();
            }
        }
コード例 #3
0
 public void SetupLiveCameraStream()
 {
     lock (lockObj)
     {
         if (surface == null)
         {
             return;
         }
         try
         {
             camera = Android.Hardware.Camera.Open(1);
             camera.SetDisplayOrientation(90);
             var cameraParams = camera.GetParameters();
             foreach (Android.Hardware.Camera.Size size in cameraParams.SupportedPreviewSizes)
             {
                 if (size.Width <= 176)
                 {
                     cameraParams.SetPreviewSize(size.Width, size.Height);
                 }
             }
             camera.SetParameters(cameraParams);
             camera.SetPreviewTexture(surface);
             camera.SetPreviewCallback(this);
             camera.StartPreview();
         }
         catch (Java.IO.IOException ex)
         {
         }
     }
 }
コード例 #4
0
        protected sealed override void StartPreview()
        {
            _camera = Camera.Open((int)CurrentCamera);
            _camera.SetDisplayOrientation(90);

            var parameters = _camera.GetParameters();
            if (parameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture))
            {
                parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
            }

            var optimalSize = GetOptimalPreviewSize(_width, _height);
            if (optimalSize != null)
            {
                parameters.SetPreviewSize(optimalSize.Width, optimalSize.Height);
            }

            _camera.SetParameters(parameters);

            try
            {
                _camera.SetPreviewTexture(_surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
 {
     camera = Android.Hardware.Camera.Open((int)CameraFacing.Back);
     textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height);
     camera.SetPreviewTexture(surface);
     PrepareAndStartCamera();
 }
コード例 #6
0
        void ToggleFlashButtonTapped(object sender, EventArgs e)
        {
            _flashOn = !_flashOn;
            if (_flashOn)
            {
                if (_cameraType == CameraFacing.Back)
                {
                    _toggleFlashButton.SetBackgroundResource(Resource.Drawable.FlashButton);
                    _cameraType = CameraFacing.Back;

                    _camera.StopPreview();
                    _camera.Release();
                    _camera = Android.Hardware.Camera.Open((int)_cameraType);
                    var parameters = _camera.GetParameters();
                    parameters.FlashMode = global::Android.Hardware.Camera.Parameters.FlashModeTorch;
                    _camera.SetParameters(parameters);
                    _camera.SetPreviewTexture(_surfaceTexture);
                    PrepareAndStartCamera();
                }
            }
            else
            {
                _toggleFlashButton.SetBackgroundResource(Resource.Drawable.NoFlashButton);
                _camera.StopPreview();
                _camera.Release();

                _camera = global::Android.Hardware.Camera.Open((int)_cameraType);
                var parameters = _camera.GetParameters();
                parameters.FlashMode = global::Android.Hardware.Camera.Parameters.FlashModeOff;
                _camera.SetParameters(parameters);
                _camera.SetPreviewTexture(_surfaceTexture);
                PrepareAndStartCamera();
            }
        }
コード例 #7
0
        public void OpenCamera()
        {
            if (MyCamera != null)
            {
                MyCamera.StopPreview();
                MyCamera.Release();
                MyCamera = null;
            }

            if (_cameraId >= 0)
            {
                Camera.GetCameraInfo(_cameraId, _cameraInfo);

                MyCamera = Camera.Open(_cameraId);

                Camera.Parameters param = MyCamera.GetParameters();
                param.SetRotation(0);

                MyCamera.SetParameters(param);

                try {
                    if (_surfaceTexture != null)
                    {
                        MyCamera.SetPreviewTexture(_surfaceTexture);
                        MyCamera.StartPreview();
                    }
                } catch (Exception) {
                }
            }

            UpdateRotation();
        }
コード例 #8
0
        private void ToggleFlashButtonTapped(object sender, EventArgs e)
        {
            flashOn = !flashOn;
            if (flashOn)
            {
                if (cameraType == CameraFacing.Back)
                {
                    toggleFlashButton.SetBackgroundResource(Resource.Drawable.FlashButton);
                    cameraType = CameraFacing.Back;

                    camera.StopPreview();
                    camera.Release();
                    camera = Camera.Open((int)cameraType);
                    var parameters = camera.GetParameters();
                    parameters.FlashMode = Camera.Parameters.FlashModeTorch;
                    camera.SetParameters(parameters);
                    camera.SetPreviewTexture(surfaceTexture);
                    PrepareAndStartCamera();
                }
            }
            else
            {
                toggleFlashButton.SetBackgroundResource(Resource.Drawable.NoFlashButton);
                camera.StopPreview();
                camera.Release();

                camera = Camera.Open((int)cameraType);
                var parameters = camera.GetParameters();
                parameters.FlashMode = Camera.Parameters.FlashModeOff;
                camera.SetParameters(parameters);
                camera.SetPreviewTexture(surfaceTexture);
                PrepareAndStartCamera();
            }
        }
コード例 #9
0
ファイル: CameraRenderer.cs プロジェクト: meta012/MoodFull
        //switches between front and back cameras when button is clicked
        void SwitchCameraButtonTapped(object sender, EventArgs e)
        {
            //if camera isn't started
            if (!_isCameraStarted)
            {
                return;
            }

            if (cameraType == CameraFacing.Front)
            {
                cameraType = CameraFacing.Back;

                camera.StopPreview();
                camera.Release();
                camera = Android.Hardware.Camera.Open((int)cameraType);
                camera.SetPreviewTexture(surfaceTexture);
                PrepareAndStartCamera();
            }
            else
            {
                cameraType = CameraFacing.Front;

                camera.StopPreview();
                camera.Release();
                camera = Android.Hardware.Camera.Open((int)cameraType);
                camera.SetPreviewTexture(surfaceTexture);
                PrepareAndStartCamera();
            }
        }
コード例 #10
0
        private void FlashLight()
        {
            if (camera == null || mParams == null)
            {
                return;
            }


            if (!isFlashLight)
            {
                player.Start();
                //mParams = camera.GetParameters();
                //mParams.FlashMode = Parameters.FlashModeTorch;
                //camera.SetParameters(mParams);
                //camera.StartPreview();
                //isFlashLight = true;
                //btnFlash.SetImageResource(Resource.Drawable.power_on);
                camera.Release();
                camera = null;
                camera = Android.Hardware.Camera.Open();
                Android.Hardware.Camera.Parameters mParams = camera.GetParameters();
                mParams.FlashMode = (Android.Hardware.Camera.Parameters.FlashModeTorch);
                camera.SetParameters(mParams);
                var mPreviewTexture = new SurfaceTexture(0);

                camera.SetPreviewTexture(mPreviewTexture);

                camera.StartPreview();
                btnFlash.SetImageResource(Resource.Drawable.power_on);
                isFlashLight = true;
            }
            else
            {
                //camera.Release();
                //camera.StopPreview();
                //camera = null;
                //player.Start();
                //mParams = camera.GetParameters();
                //mParams.FlashMode = Parameters.FlashModeOff;
                //camera.SetParameters(mParams);
                //camera.StartPreview();
                //isFlashLight = false;
                //btnFlash.SetImageResource(Resource.Drawable.power_off);
                camera.Release();
                camera = null;
                camera = Android.Hardware.Camera.Open();
                Android.Hardware.Camera.Parameters mParams = camera.GetParameters();
                mParams.FlashMode = (Android.Hardware.Camera.Parameters.FlashModeOff);
                camera.SetParameters(mParams);
                var mPreviewTexture = new SurfaceTexture(0);

                camera.SetPreviewTexture(mPreviewTexture);

                camera.StartPreview();
                btnFlash.SetImageResource(Resource.Drawable.power_off);
                isFlashLight = false;
            }
        }
コード例 #11
0
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            camera = Camera.Open((int)cameraType);
            textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height);
            surfaceTexture = surface;

            camera.SetPreviewTexture(surface);
            PrepareAndStartCamera();
        }
コード例 #12
0
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            _camera = global::Android.Hardware.Camera.Open((int)_cameraType);
            _textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height);
            _surfaceTexture = surface;

            _camera.SetPreviewTexture(surface);
            PrepareAndStartCamera();
        }
コード例 #13
0
        private void SwitchCameraButton_Click(object sender, EventArgs e)
        {
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
            {
                string[] camPermissions = { Android.Manifest.Permission.Camera };

                // Make sure we can use the camera
                if ((int)Android.OS.Build.VERSION.SdkInt >= 23 && Activity.CheckSelfPermission(camPermissions[0]) != (Permission.Granted))
                {
                    Toast.MakeText(Activity, AppResources.Camera_permissionNeeded, ToastLength.Long).Show();
                    Activity.RequestPermissions(camPermissions, CamPermReqId);
                    return;
                }

                mCameraDevice.Close();

                int newCamIndex = backCam ? 1 : 0;
                backCam = !backCam;

                try
                {
                    OpenCamera(newCamIndex);
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(ex);
                }
            }
            else
            {
                if (cameraType == CameraFacing.Front)
                {
                    cameraType = CameraFacing.Back;

                    camera.StopPreview();
                    camera.Release();
                    camera = Android.Hardware.Camera.Open((int)cameraType);
                    camera.SetPreviewTexture(surfaceTexture);
                    PrepareAndStartCamera1();
                }
                else
                {
                    cameraType = CameraFacing.Front;

                    camera.StopPreview();
                    camera.Release();
                    camera = Android.Hardware.Camera.Open((int)cameraType);
                    camera.SetPreviewTexture(surfaceTexture);
                    PrepareAndStartCamera1();
                }
            }
        }
コード例 #14
0
        /// <summary>
        /// Turn the lamp on
        /// </summary>
        public void TurnOn()
        {
            // Additional information about using the camera light here:
            // http://forums.xamarin.com/discussion/24237/camera-led-or-flash
            // http://stackoverflow.com/questions/5503480/use-camera-flashlight-in-android?rq=1

            if (camera == null)
            {
                camera = Camera.Open();
            }

            if (camera == null)
            {
                Debug.WriteLine("Camera failed to initialize");
                return;
            }

            var p = camera.GetParameters();
            var supportedFlashModes = p.SupportedFlashModes;

            if (supportedFlashModes == null)
            {
                supportedFlashModes = new List <string>();
            }

            var flashMode = string.Empty;

            if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch))
            {
                flashMode = Android.Hardware.Camera.Parameters.FlashModeTorch;
            }

            if (!string.IsNullOrEmpty(flashMode))
            {
                p.FlashMode = flashMode;
                camera.SetParameters(p);
            }

            camera.StartPreview();

            // nexus 5 fix here: http://stackoverflow.com/questions/21417332/nexus-5-4-4-2-flashlight-led-not-turning-on
            try
            {
                camera.SetPreviewTexture(new SurfaceTexture(0));
            }
            catch (IOException ex)
            {
                // Ignore
            }
        }
コード例 #15
0
 public void OnSurfaceTextureAvailable (Android.Graphics.SurfaceTexture surface, int w, int h)
 {
     _camera = Camera.Open ();
     
     _textureView.LayoutParameters = new FrameLayout.LayoutParams (w, h);
     
     try {
         _camera.SetPreviewTexture (surface);
         _camera.StartPreview ();
         
     } catch (Java.IO.IOException ex) {
         Console.WriteLine (ex.Message);
     }
 }
コード例 #16
0
        /// <summary>
        /// Events when surfacetexture is available, sets camera parameters
        /// </summary>
        /// <param name="surface">Surface</param>
        /// <param name="width">Width</param>
        /// <param name="height">Height</param>
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
#pragma warning disable 618
            camera = Camera.Open((int)cameraType);
            var parameters = camera.GetParameters();
            if (parameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture))
            {
                parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
            }
            camera.SetParameters(parameters);
#pragma warning restore 618
            textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height);
            camera.SetPreviewTexture(surface);
            PrepareAndStartCamera();
        }
コード例 #17
0
ファイル: CameraRenderer.cs プロジェクト: meta012/MoodFull
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            try
            {
                camera = Android.Hardware.Camera.Open((int)cameraType);
                textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height);
                surfaceTexture = surface;

                camera.SetPreviewTexture(surface);
                PrepareAndStartCamera();
            }
            catch (Java.Lang.RuntimeException ex)
            {
                Permission.TryToGetPermissions(activity);
            }
        }
コード例 #18
0
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            mCamera     = Open();
            mCameraInfo = GetCameraInfo();


            //https://www.captechconsulting.com/blogs/android-camera-orientation-made-simple
            mCamera.SetDisplayOrientation(getCorrectCameraOrientation(mCameraInfo, mCamera));

            try {
                mCamera.SetPreviewTexture(surface);
                mCamera.SetPreviewCallback(this);
                mCamera.StartPreview();
            } catch (IOException ioe) {
                // Something bad happened
            }
        }
コード例 #19
0
ファイル: CameraPage.cs プロジェクト: JRolandros/PluginTest
        public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int width, int height)
        {
            // var camInfo = new Camera.CameraInfo();

                    _camera = Camera.Open(0);

               // _camera = Camera.Open();
            _textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height);
            try
            {
                _camera.SetPreviewTexture(surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #20
0
ファイル: LiveStreamActivity.cs プロジェクト: Shtml7/Shared
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            //opens the hardware camers
            _camera = Android.Hardware.Camera.Open();

            _textureView.LayoutParameters =
                new FrameLayout.LayoutParams(height, width);

            try
            {
                _camera.SetPreviewTexture(surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #21
0
ファイル: MainActivity.cs プロジェクト: hbons/Invert
        public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int width, int height)
        {
            _camera = Camera.Open ();
            ConfigureCamera ();

            Preview preview = new Preview ();
            preview.OnFrame += OnPreviewFrame;
            _camera.SetPreviewCallback (preview);

            _textureView.LayoutParameters = new FrameLayout.LayoutParams (width, height);

            try {
                _camera.SetPreviewTexture (surface);
                _camera.StartPreview ();

            } catch (Exception e) {
                Console.WriteLine (e.Message);
            }
        }
コード例 #22
0
 public void OnSurfaceTextureAvailable (Android.Graphics.SurfaceTexture surface, int width, int height)
 {
     _camera = Camera.Open ();
     
     var previewSize = _camera.GetParameters ().PreviewSize;
     _textureView.LayoutParameters = 
         new FrameLayout.LayoutParams (previewSize.Width, previewSize.Height, (int)GravityFlags.Center);
     
     try {
         _camera.SetPreviewTexture (surface);
         _camera.StartPreview ();
     } catch (Java.IO.IOException ex) {
         Console.WriteLine (ex.Message);
     }
     
     // this is the sort of thing TextureView enables
     _textureView.Rotation = 45.0f;
     _textureView.Alpha = 0.5f;
 }
コード例 #23
0
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            camera = Android.Hardware.Camera.Open();
            var parameters = camera.GetParameters();
            var aspect     = ((decimal)height) / ((decimal)width);

            // Find the preview aspect ratio that is closest to the surface aspect
            var previewSize = parameters.SupportedPreviewSizes
                              .OrderBy(s => Math.Abs(s.Width / (decimal)s.Height - aspect))
                              .First();

            System.Diagnostics.Debug.WriteLine($"Preview sizes: {parameters.SupportedPreviewSizes.Count}");

            parameters.SetPreviewSize(previewSize.Width, previewSize.Height);
            camera.SetParameters(parameters);

            camera.SetPreviewTexture(surface);
            StartCamera();
        }
コード例 #24
0
        public void OnSurfaceTextureAvailable(
            Android.Graphics.SurfaceTexture surface, int w, int h)
        {
            _camera = Android.Hardware.Camera.Open(1);


            _textureView.LayoutParameters =
                new FrameLayout.LayoutParams(w, h);

            try
            {
                _camera.SetDisplayOrientation(270);
                _camera.SetPreviewTexture(surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
コード例 #25
0
 public void SetupLiveCameraStream(int id = 0)
 {
     lock (lockObj)
     {
         if (surface == null)
         {
             return;
         }
         try
         {
             camera = Android.Hardware.Camera.Open(id);
             camera.SetPreviewTexture(surface);
             camera.StartPreview();
         }
         catch (Java.IO.IOException ex)
         {
             Console.WriteLine(ex.Message);
         }
     }
 }
コード例 #26
0
ファイル: CameraRenderer.cs プロジェクト: meta012/MoodFull
        //turns on or off camera flash
        void ToggleFlashButtonTapped(object sender, EventArgs e)
        {
            //if camera isn't started
            if (!_isCameraStarted)
            {
                return;
            }

            _flashOn = !_flashOn;

            if (_flashOn)
            {
                if (cameraType == CameraFacing.Back)
                {
                    toggleFlashButton.SetBackgroundResource(Resource.Drawable.FlashButton);
                    cameraType = CameraFacing.Back;

                    camera.StopPreview();
                    camera.Release();
                    camera = Android.Hardware.Camera.Open((int)cameraType);
                    var parameters = camera.GetParameters();
                    parameters.FlashMode = Android.Hardware.Camera.Parameters.FlashModeTorch;
                    camera.SetParameters(parameters);
                    camera.SetPreviewTexture(surfaceTexture);
                    PrepareAndStartCamera();
                }
            }
            else
            {
                toggleFlashButton.SetBackgroundResource(Resource.Drawable.NoFlashButton);
                camera.StopPreview();
                camera.Release();

                camera = Android.Hardware.Camera.Open((int)cameraType);
                var parameters = camera.GetParameters();
                parameters.FlashMode = Android.Hardware.Camera.Parameters.FlashModeOff;
                camera.SetParameters(parameters);
                camera.SetPreviewTexture(surfaceTexture);
                PrepareAndStartCamera();
            }
        }
コード例 #27
0
        public static void Start(SurfaceTexture Surface, int Width, int Height)
        {
            if (Cam == null)
            {
                Cam = Camera.Open();
            }

            Camera.Parameters Parms = Cam.GetParameters();
            MaxZoom = Parms.MaxZoom;

            if (Parms.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture))
            {
                Parms.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
                Cam.SetParameters(Parms);
            }

            Cam.SetPreviewTexture(Surface);
            Cam.SetDisplayOrientation(90);
            StartPreview();
            SetVertical();
        }
コード例 #28
0
        void SwitchCameraButtonTapped(object sender, EventArgs e)
        {
            if (_cameraType == CameraFacing.Front)
            {
                _cameraType = CameraFacing.Back;

                _camera.StopPreview();
                _camera.Release();
                _camera = global::Android.Hardware.Camera.Open((int)_cameraType);
                _camera.SetPreviewTexture(_surfaceTexture);
                PrepareAndStartCamera();
            }
            else
            {
                _cameraType = CameraFacing.Front;

                _camera.StopPreview();
                _camera.Release();
                _camera = global::Android.Hardware.Camera.Open((int)_cameraType);
                _camera.SetPreviewTexture(_surfaceTexture);
                PrepareAndStartCamera();
            }
        }
コード例 #29
0
 public void start()
 {
     if (originalStart)
     {
         originalStart = false;
         Thread t = new Thread(new ThreadStart(reStarter));
         t.Start();
     }
     try
     {
         cam = Android.Hardware.Camera.Open();
         cam.GetParameters().PreviewFormat = ImageFormatType.Rgb565;
         var param = cam.GetParameters();
         param.SetPreviewSize(320, 240);
         cam.SetParameters(param);
         var x = cam.GetParameters().SupportedPreviewSizes;
         cam.SetDisplayOrientation(90);
         cam.SetPreviewCallback(this);
         cam.SetPreviewTexture(image.SurfaceTexture);
         cam.StartPreview();
     }
     catch (Exception e) { }
 }
コード例 #30
0
        private void StartCamPrep()
        {
            camera     = Camera.Open((int)cameraType);
            camEnabled = true;
            textureView.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;

            PrepareAndStartCamera();
        }
コード例 #31
0
        private void SwitchCameraButtonTapped(object sender, EventArgs e)
        {
            if (cameraType == CameraFacing.Front)
            {
                cameraType = CameraFacing.Back;

                camera.StopPreview();
                camera.Release();
                camera = Camera.Open((int)cameraType);
                camera.SetPreviewTexture(surfaceTexture);
                PrepareAndStartCamera();
            }
            else
            {
                cameraType = CameraFacing.Front;

                camera.StopPreview();
                camera.Release();
                camera = Camera.Open((int)cameraType);
                camera.SetPreviewTexture(surfaceTexture);
                PrepareAndStartCamera();
            }
        }
コード例 #32
0
        public void OnSurfaceCreated(Javax.Microedition.Khronos.Opengles.IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            // Set the background frame color
            GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            initTex();
            mSTexture = new SurfaceTexture(hTex[0]);
            mSTexture.SetOnFrameAvailableListener(this);

            mCamera = Android.Hardware.Camera.Open();
            try
            {
                mCamera.SetPreviewTexture(mSTexture);
            }
            catch (Exception ioe)
            {
            }

            GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            hProgram = loadShader(vss, fss);

            mTriangle = new Triangle();
        }
コード例 #33
0
        /// <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();
            }
        }
コード例 #34
0
        private void SetupAndStartCamera(SurfaceTexture surface = null)
        {
            try
            {
                if (_isSurfaceAvailable)
                {
                    if (_camera == null)
                    {
                        _camera = Camera.Open((int)_cameraType);
                        _camera.SetErrorCallback(this);

                        for (var ii = 0; ii < Camera.NumberOfCameras - 1; ii++)
                        {
                            var info = new Camera.CameraInfo();
                            Camera.GetCameraInfo(ii, info);
                            if (info.CanDisableShutterSound)
                            {
                                _camera.EnableShutterSound(false);
                            }
                        }

                        var parameters = _camera.GetParameters();
                        parameters.FlashMode          = Camera.Parameters.FlashModeOff;
                        parameters.VideoStabilization = false;
                        parameters.JpegQuality        = 100;
                        TurnOnContinuousFocus(parameters);

                        var landscapePictureDescendingSizes = parameters
                                                              .SupportedPictureSizes
                                                              .Where(p => p.Width > p.Height)
                                                              .OrderByDescending(p => p.Width * p.Height)
                                                              .ToList();
                        var landscapePreviewDescendingSizes = parameters
                                                              .SupportedPreviewSizes
                                                              .Where(p => p.Width > p.Height)
                                                              .OrderByDescending(p => p.Width * p.Height)
                                                              .ToList();

                        foreach (var pictureSize in landscapePictureDescendingSizes)
                        {
                            foreach (var previewSize in landscapePreviewDescendingSizes)
                            {
                                if (Math.Abs((double)pictureSize.Width / pictureSize.Height -
                                             (double)previewSize.Width / previewSize.Height) < 0.0001)
                                {
                                    _pictureSize = pictureSize;
                                    _previewSize = previewSize;
                                    break;
                                }
                            }

                            if (_pictureSize != null)
                            {
                                break;
                            }
                        }

                        if (_pictureSize == null ||
                            _previewSize == null)
                        {
                            _pictureSize = landscapePictureDescendingSizes.First();
                            _previewSize = landscapePreviewDescendingSizes.First();
                        }

                        parameters.SetPictureSize(_pictureSize.Width, _pictureSize.Height);
                        parameters.SetPreviewSize(_previewSize.Width, _previewSize.Height);

                        _camera.SetParameters(parameters);
                        _camera.SetPreviewTexture(_surfaceTexture);
                    }

                    if (surface != null)
                    {
                        _surfaceTexture = surface;
                    }

                    SetOrientation();
                    StartCamera();
                }
            }
            catch (Exception e)
            {
                _cameraModule.ErrorMessage = e.ToString();
            }
        }
コード例 #35
0
        public void OnSurfaceCreated(Javax.Microedition.Khronos.Opengles.IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            // Set the background frame color
            GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            initTex();
            mSTexture = new SurfaceTexture(hTex[0]);
            mSTexture.SetOnFrameAvailableListener(this);

            mCamera = Android.Hardware.Camera.Open();
            try
            {
                mCamera.SetPreviewTexture(mSTexture);
            }
            catch (Exception ioe)
            {
            }

            GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            hProgram = loadShader(vss, fss);

            mTriangle = new Triangle();
        }
コード例 #36
0
        protected override void OnStart()
        {
            try
            {
                if (Camera == null)
                {
                    Camera = Hardware.Camera.Open(cameraIndex);
                }

                if (Texture == null)
                {
                    Texture = new Graphics.SurfaceTexture(0);
                }

                CameraPreviewCallback callback = new CameraPreviewCallback();
                callback.PreviewUpdated += Callback_PreviewUpdated;

                Hardware.Camera.Parameters  parameter   = Camera.GetParameters();
                List <Hardware.Camera.Size> supportSize = parameter.SupportedPreviewSizes.OrderByDescending(x => x.Width).ToList();
                foreach (Hardware.Camera.Size size in supportSize)
                {
                    CvLogger.Log(this, $"Camera Support Size: W{size.Width},H{size.Height}");

                    if (size.Width == 960 && size.Height == 720)
                    {
                        parameter.SetPreviewSize(size.Width, size.Height);
                        CvLogger.Log(this, $"SET Camera Size: W{size.Width},H{size.Height}");
                    }
                }

                string[] supportedFocusMode = parameter.SupportedFocusModes.ToArray();
                if (supportedFocusMode.Contains(Hardware.Camera.Parameters.FocusModeContinuousVideo))
                {
                    parameter.FocusMode = Hardware.Camera.Parameters.FocusModeContinuousVideo;
                }
                else if (supportedFocusMode.Contains(Hardware.Camera.Parameters.FocusModeContinuousPicture))
                {
                    parameter.FocusMode = Hardware.Camera.Parameters.FocusModeContinuousPicture;
                }
                parameter.ColorEffect = Hardware.Camera.Parameters.EffectNone;

                width      = parameter.PreviewSize.Width;
                height     = parameter.PreviewSize.Height;
                fps        = parameter.PreviewFrameRate;
                cameraType = parameter.PreviewFormat;

                CvLogger.Log(this, string.Format("Camera is creating W{0} H{1} FPS{2}", width, height, fps));
                Camera.SetParameters(parameter);

                Camera.SetPreviewCallback(callback);
                Camera.SetPreviewTexture(Texture);
                Camera.StartPreview();

                cameraOn = true;
            }
            catch (Exception ex)
            {
                CvLogger.Log(this, "Camera Init Failed.\n" + ex.ToString());

                Dispose();

                throw new ArgumentException("Camera Exception", ex);
            }
        }