/* * /// <summary> * /// Number of pending user requests to capture a photo. * /// </summary> * static int mPendingUserCaptures = 0; */ /// <summary> /// Closes the current {@link CameraDevice}. /// </summary> void CloseCamera() { try { mCameraOpenCloseLock.Acquire(); lock (mCameraStateLock) { // Reset state and clean up resources used by the camera. // Note: After calling this, the ImageReaders will be closed after any background // tasks saving Images from these readers have been completed. //mPendingUserCaptures = 0; mState = STATE_CLOSED; if (null != mCaptureSession) { mCaptureSession.Close(); mCaptureSession = null; } if (null != mCameraDevice) { mCameraDevice.Close(); mCameraDevice = null; } if (null != mYuvImageReader) { mYuvImageReader.Close(); mYuvImageReader = null; } } } catch (Java.Lang.InterruptedException e) { throw new Java.Lang.RuntimeException("Interrupted while trying to lock camera closing.", e); } finally { mCameraOpenCloseLock.Release(); } }
/// <summary> /// Sets up state related to camera that is needed before opening a {@link CameraDevice}. /// </summary> /// <returns><c>true</c>, if up camera outputs was set, <c>false</c> otherwise.</returns> bool SetUpCameraOutputs() { var activity = this; CameraManager manager = (CameraManager)activity.GetSystemService(Context.CameraService); if (manager == null) { Log.Error(TAG, "This device doesn't support Camera2 API."); return(false); } try { // Find a CameraDevice that supports YUV captures, and configure state. foreach (string cameraId in manager.GetCameraIdList()) { CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId); /* * // We only use a camera that supports YUV in this sample. * if (!Contains(characteristics.Get( * CameraCharacteristics.RequestAvailableCapabilities).ToArray<int>(), * (int)RequestAvailableCapabilities.YuvReprocessing)) * { * continue; * }*/ StreamConfigurationMap map = (StreamConfigurationMap)characteristics.Get( CameraCharacteristics.ScalerStreamConfigurationMap); // For still image captures, we use the largest available size. Android.Util.Size[] yuvs = map.GetOutputSizes((int)ImageFormatType.Yuv420888); Android.Util.Size largestYuv = yuvs.OrderByDescending(element => element.Width * element.Height).Last(); lock (mCameraStateLock) { // Set up ImageReaders for JPEG and RAW outputs. Place these in a reference // counted wrapper to ensure they are only closed when all background tasks // using them are finished. if (mYuvImageReader == null || mYuvImageReader.GetAndRetain() == null) { mYuvImageReader = new RefCountedAutoCloseable <ImageReader>( ImageReader.NewInstance(largestYuv.Width, largestYuv.Height, ImageFormatType.Yuv420888, /*maxImages*/ 5)); } mYuvImageReader.Get().SetOnImageAvailableListener( mOnYuvImageAvailableListener, mBackgroundHandler); mCharacteristics = characteristics; mCameraId = cameraId; } return(true); } } catch (CameraAccessException e) { e.PrintStackTrace(); } // If we found no suitable cameras for capturing YUV, warn the user. Log.Error(TAG, "This device doesn't support capturing YUV photos"); return(false); }