/** Stops the stream. */ public void stop() { lock (this) { if (mCamera != null) { if (mMode == MODE_MEDIACODEC_API) { mCamera.SetPreviewCallbackWithBuffer(null); } if (mMode == MODE_MEDIACODEC_API_2) { ((SurfaceView)mSurfaceView).removeMediaCodecSurface(); } base.stop(); // We need to restart the preview if (!mCameraOpenedManually) { destroyCamera(); } else { try { startPreview(); } catch (RuntimeException e) { e.PrintStackTrace(); } } } } }
// Stop preview and release the camera private void stopPreviewAndReleaseCamera() { if (camera != null) { camera.SetPreviewCallbackWithBuffer(null); stopPreview(); camera.Release(); camera = null; } }
private void configureCameraAndStartPreview(Android.Hardware.Camera camera) { // Setting camera parameters when preview is running can cause crashes on some android devices stopPreview(); // Configure camera orientation. This is needed for both correct preview orientation // and recognition orientation = getCameraOrientation(); camera.SetDisplayOrientation(orientation); // Configure camera parameters Android.Hardware.Camera.Parameters parameters = camera.GetParameters(); // Select preview size. The preferred size for Text Capture scenario is 1080x720. In some scenarios you might // consider using higher resolution (small text, complex background) or lower resolution (better performance, less noise) cameraPreviewSize = null; foreach (Android.Hardware.Camera.Size size in parameters.SupportedPreviewSizes) { if (size.Height <= 720 || size.Width <= 720) { if (cameraPreviewSize == null) { cameraPreviewSize = size; } else { int resultArea = cameraPreviewSize.Width * cameraPreviewSize.Height; int newArea = size.Width * size.Height; if (newArea > resultArea) { cameraPreviewSize = size; } } } } parameters.SetPreviewSize(cameraPreviewSize.Width, cameraPreviewSize.Height); // Zoom parameters.Zoom = cameraZoom; // Buffer format. The only currently supported format is NV21 parameters.PreviewFormat = Android.Graphics.ImageFormatType.Nv21; // Default focus mode parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeAuto; // Done camera.SetParameters(parameters); // The camera will fill the buffers with image data and notify us through the callback. // The buffers will be sent to camera on requests from recognition service (see implementation // of ITextCaptureService.Callback.onRequestLatestFrame above) camera.SetPreviewCallbackWithBuffer(cameraPreviewCallback); // Clear the previous recognition results if any clearRecognitionResults(); // Width and height of the preview according to the current screen rotation int width = 0; int height = 0; switch (orientation) { case 0: case 180: width = cameraPreviewSize.Width; height = cameraPreviewSize.Height; break; case 90: case 270: width = cameraPreviewSize.Height; height = cameraPreviewSize.Width; break; } // Configure the view scale and area of interest (camera sees it as rotated 90 degrees, so // there's some confusion with what is width and what is height) surfaceViewWithOverlay.setScaleX(surfaceViewWithOverlay.Width, width); surfaceViewWithOverlay.setScaleY(surfaceViewWithOverlay.Height, height); // Area of interest int marginWidth = (areaOfInterestMargin_PercentOfWidth * width) / 100; int marginHeight = (areaOfInterestMargin_PercentOfHeight * height) / 100; surfaceViewWithOverlay.setAreaOfInterest( new Rect(marginWidth, marginHeight, width - marginWidth, height - marginHeight)); // Start preview camera.StartPreview(); setCameraFocusMode(Android.Hardware.Camera.Parameters.FocusModeAuto); autoFocus(finishCameraInitialisationAutoFocusCallback); inPreview = true; }