Esempio n. 1
0
        //Start the camera preview
        public void startPreview()
        {
            if (null == cameraDevice || !textureView.IsAvailable || null == previewSize)
            {
                return;
            }

            try
            {
                closePreviewSession();
                SurfaceTexture texture = textureView.SurfaceTexture;
                //Assert.IsNotNull(texture);

                texture.SetDefaultBufferSize(previewSize.Width, previewSize.Height);

                previewBuilder = cameraDevice.CreateCaptureRequest(CameraTemplate.Record);
                surfaces       = new List <Surface>();
                var previewSurface = new Surface(texture);
                surfaces.Add(previewSurface);
                previewBuilder.AddTarget(previewSurface);

                if (previewBuilder != null)
                {
                    if (flashon)
                    {
                        previewBuilder.Set(CaptureRequest.FlashMode, (int)FlashMode.Torch);
                    }

                    Rect sensor_rect = max;
                    int  left        = sensor_rect.Width() / 2;
                    int  right       = left;
                    int  top         = sensor_rect.Height() / 2;
                    int  bottom      = top;
                    int  hwidth      = (int)(sensor_rect.Width() / (2.0 * zoomlev));
                    int  hheight     = (int)(sensor_rect.Height() / (2.0 * zoomlev));
                    left   -= hwidth;
                    right  += hwidth;
                    top    -= hheight;
                    bottom += hheight;
                    previewBuilder.Set(CaptureRequest.ScalerCropRegion, new Rect(left, top, right, bottom));
                }

                previewBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.Off);
                previewBuilder.Set(CaptureRequest.ControlAfTrigger, (int)ControlAFTrigger.Cancel);

                //var recorderSurface = mediaRecorder.Surface;
                //surfaces.Add(recorderSurface);
                //previewBuilder.AddTarget(recorderSurface);
                mPreviewSession = new PreviewCaptureStateCallback(this, false);
                cameraDevice.CreateCaptureSession(surfaces, mPreviewSession, backgroundHandler);
            }
            catch (CameraAccessException e)
            {
                e.PrintStackTrace();
            }
            catch (IOException e)
            {
                e.PrintStackTrace();
            }
        }
Esempio n. 2
0
        private void StartRecordingVideo()
        {
            try
            {
                closePreviewSession();
                SetUpMediaRecorder();
                SurfaceTexture texture = textureView.SurfaceTexture;
                //Assert.IsNotNull(texture);
                //texture.SetDefaultBufferSize(previewSize.Width, previewSize.Height);
                previewBuilder = cameraDevice.CreateCaptureRequest(CameraTemplate.Record);
                var surfaces       = new List <Surface>();
                var previewSurface = new Surface(texture);
                surfaces.Add(previewSurface);
                previewBuilder.AddTarget(previewSurface);

                if (flashon)
                {
                    previewBuilder.Set(CaptureRequest.FlashMode, (int)FlashMode.Torch);
                }

                Rect sensor_rect = max;
                int  left        = sensor_rect.Width() / 2;
                int  right       = left;
                int  top         = sensor_rect.Height() / 2;
                int  bottom      = top;
                int  hwidth      = (int)(sensor_rect.Width() / (2.0 * zoomlev));
                int  hheight     = (int)(sensor_rect.Height() / (2.0 * zoomlev));
                left   -= hwidth;
                right  += hwidth;
                top    -= hheight;
                bottom += hheight;
                previewBuilder.Set(CaptureRequest.ScalerCropRegion, new Rect(left, top, right, bottom));

                var recorderSurface = mediaRecorder.Surface;
                surfaces.Add(recorderSurface);
                previewBuilder.AddTarget(recorderSurface);
                mPreviewSession = new PreviewCaptureStateCallback(this, true);
                cameraDevice.CreateCaptureSession(surfaces, mPreviewSession, backgroundHandler);
            }
            catch (IllegalStateException e)
            {
                e.PrintStackTrace();
            }
        }