private void UpdateCameraAspect() { try { Camera.Parameters camParams = camera.GetParameters(); Camera.CameraInfo info = new Camera.CameraInfo(); Camera.GetCameraInfo((int)Android.Hardware.CameraFacing.Back, info); Camera.Size size = GetOptimalPreviewSize(camParams.SupportedPreviewSizes, width, height); camParams.SetPreviewSize(size.Width, size.Height); int rotation = (info.Orientation + 360) % 360; camParams.SetRotation(rotation); if (camParams.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { camParams.FocusMode = Camera.Parameters.FocusModeContinuousPicture; } camera.SetParameters(camParams); } catch (Exception e) { Console.WriteLine(e.Message); } }
/** * Attempts to find a preview size that matches the provided width and height (which * specify the dimensions of the encoded video). If it fails to find a match it just * uses the default preview size. * <p> * TODO: should do a best-fit match. */ private void choosePreviewSize(Camera.Parameters parms, int width, int height) { // We should make sure that the requested MPEG size is less than the preferred // size, and has the same aspect ratio. Camera.Size ppsfv = parms.PreferredPreviewSizeForVideo; if (AppSettings.Logging.SendToConsole && ppsfv != null) { Log.Debug(TAG, "Camera preferred preview size for video is " + ppsfv.Width + "x" + ppsfv.Height); } foreach (Camera.Size size in parms.SupportedPreviewSizes) { if (size.Width == width && size.Height == height) { parms.SetPreviewSize(width, height); return; } } Log.Warn(TAG, "Unable to set preview size to " + width + "x" + height); if (ppsfv != null) { parms.SetPreviewSize(ppsfv.Width, ppsfv.Height); } }
/** * Updated rotation matrix, aspect ratio etc. */ public void UpdateRotation() { if (MyCamera == null || _sharedData == null) { return; } var orientation = _cameraInfo.Orientation; Matrix.SetRotateM(_sharedData._orientationM, 0, orientation, 0f, 0f, 1f); Camera.Size size = MyCamera.GetParameters().PreviewSize; if (orientation % 90 == 0) { var w = size.Width; size.Width = size.Height; size.Height = w; } _sharedData._aspectRatioPreview [0] = (float)Math.Min(size.Width, size.Height) / size.Width; _sharedData._aspectRatioPreview [1] = (float)Math.Min(size.Width, size.Height) / size.Height; }
private Camera.Size GetOptimalPreviewSize(IList <Camera.Size> sizes, int w, int h) { const double ASPECT_TOLERANCE = 0.05; double targetRatio = (double)w / h; int maxSize = 1228800 * 2; if (sizes == null) { return(null); } Camera.Size optimalSize = null; double minDiff = Double.MaxValue; int targetHeight = h; // Try to find an size match aspect ratio and size foreach (Camera.Size size in sizes) { double ratio = (double)size.Height / size.Width; if (size.Height * size.Width > maxSize) { continue; } if (Math.Abs(ratio - targetRatio) > ASPECT_TOLERANCE) { continue; } if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } // Cannot find the one match the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = Double.MaxValue; foreach (Camera.Size size in sizes) { if (size.Height * size.Width > maxSize) { continue; } if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } } return(optimalSize); }
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = ResolveSize(SuggestedMinimumWidth, widthMeasureSpec); int height = ResolveSize(SuggestedMinimumHeight, heightMeasureSpec); SetMeasuredDimension(width, height); if (supportedPreviewSizes != null) { previewSize = GetOptimalPreviewSize(supportedPreviewSizes, width, height); } }
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { // We purposely disregard child measurements because act as a // wrapper to a SurfaceView that centers the camera preview instead // of stretching it. int width = ResolveSize(SuggestedMinimumWidth, widthMeasureSpec); int height = ResolveSize(SuggestedMinimumHeight, heightMeasureSpec); SetMeasuredDimension(width, height); if (mSupportedPreviewSizes != null) { mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height); } }
public BarcodeScannerRenderer(Context context) : base(context) { cameraInfo = new Camera.CameraInfo(); Camera.GetCameraInfo(0, cameraInfo); cam = Camera.Open(0); parameters = cam.GetParameters(); Camera.Size size = parameters.PictureSize; cheight = size.Height; cwidth = size.Width; maxZoom = parameters.MaxZoom; cam.SetPreviewCallback(this); }
private Android.Hardware.Camera.Size getOptimalPreviewSize(List <Android.Hardware.Camera.Size> sizes, int w, int h) { double ASPECT_TOLERANCE = 0.1; double targetRatio = (double)w / h; if (sizes == null) { return(null); } Android.Hardware.Camera.Size optimalSize = null; double minDiff = double.MaxValue; int targetHeight = h; // Try to find an size match aspect ratio and size foreach (Android.Hardware.Camera.Size size in sizes) { double ratio = (double)size.Width / size.Height; if (Math.Abs(ratio - targetRatio) > ASPECT_TOLERANCE) { continue; } if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } // Cannot find the one match the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = double.MaxValue; foreach (Android.Hardware.Camera.Size size in sizes) { if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } } return(optimalSize); }
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height) { Camera.Parameters parameters = Parent.camera.GetParameters(); Camera.Size size = Parent.GetBestPreviewSize(width, height, parameters); if (size != null) { parameters.SetPreviewSize(size.Width, size.Height); parameters.SetPictureSize(size.Width, size.Height); parameters.SetRotation(90); parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture; Parent.camera.SetParameters(parameters); Parent.camera.StartPreview(); Parent.inPreview = true; } }
Camera.Size GetOptimalPreviewSize(IList <Camera.Size> sizes, int w, int h) { const double AspectTolerance = 0.1; double targetRatio = (double)w / h; if (sizes == null) { return(null); } Camera.Size optimalSize = null; double minDiff = double.MaxValue; int targetHeight = h; foreach (Camera.Size size in sizes) { double ratio = (double)size.Width / size.Height; if (Math.Abs(ratio - targetRatio) > AspectTolerance) { continue; } if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } if (optimalSize == null) { minDiff = double.MaxValue; foreach (Camera.Size size in sizes) { if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } } return(optimalSize); }
private Android.Hardware.Camera.Size GetBestPreviewSize(int width, int height, Android.Hardware.Camera.Parameters parameters) { Android.Hardware.Camera.Size bestSize = null; IList <Android.Hardware.Camera.Size> sizeList = parameters.SupportedPreviewSizes; bestSize = sizeList[0]; for (int i = 1; i < sizeList.Count; i++) { if ((sizeList[i].Width * sizeList[i].Height) > (bestSize.Width * bestSize.Height)) { bestSize = sizeList[i]; } } return(bestSize); }
private Android.Hardware.Camera.Size GetOptimalPreviewSize(IList <Android.Hardware.Camera.Size> sizes, int w, int h) { double ASPECT_TOLERANCE = 0.1; double targetRatio = (double)h / w; if (sizes == null) { return(null); } Android.Hardware.Camera.Size optimalSize = null; double minDiff = Double.MaxValue; int targetHeight = h; foreach (Android.Hardware.Camera.Size size in sizes) { double ratio = (double)size.Height / size.Width; if (Math.Abs(ratio - targetRatio) > ASPECT_TOLERANCE) { continue; } if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } if (optimalSize == null) { minDiff = Double.MaxValue; foreach (Android.Hardware.Camera.Size size in sizes) { if (Math.Abs(size.Height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.Abs(size.Height - targetHeight); } } } return(optimalSize); }
public void SurfaceChanged(ISurfaceHolder holder, Format format, int w, int h) { // Now that the size is known, set up the camera parameters and begin // the preview. Camera.Parameters parameters = camera.GetParameters(); IList <Camera.Size> sizes = parameters.SupportedPreviewSizes; Camera.Size optimalSize = GetOptimalPreviewSize(sizes, w, h); parameters.SetPreviewSize(optimalSize.Width, optimalSize.Height); camera.SetDisplayOrientation(90); camera.SetParameters(parameters); int dataBufferSize = (int)(optimalSize.Width * optimalSize.Height * (ImageFormat.GetBitsPerPixel(camera.GetParameters().PreviewFormat) / 8.0)); _reader = new QRCodeReader(); camera.StartPreview(); }
private void prepareCamera(int encWidth, int encHeight) { if (_camera != null) { throw new RuntimeException("camera already initialized"); } Camera.CameraInfo info = new Camera.CameraInfo(); // Try to find a front-facing camera (e.g. for videoconferencing). int numCameras = Camera.NumberOfCameras; for (int i = 0; i < numCameras; i++) { Camera.GetCameraInfo(i, info); if (info.Facing == Camera.CameraInfo.CameraFacingFront) { _camera = Camera.Open(i); break; } } if (_camera == null) { Log.Debug(TAG, "No front-facing camera found; opening default"); _camera = Camera.Open(); // opens first back-facing camera } if (_camera == null) { throw new RuntimeException("Unable to open camera"); } Camera.Parameters parms = _camera.GetParameters(); choosePreviewSize(parms, encWidth, encHeight); // leave the frame rate set to default _camera.SetParameters(parms); Camera.Size size = parms.PreviewSize; Log.Debug(TAG, "Camera preview size is " + size.Width + "x" + size.Height); }
private void ConfigurePreviewSize() { var cameraParams = mCamera.GetParameters(); var supportedPreviewSizes = cameraParams.SupportedPreviewSizes; int minDiff = int.MaxValue; Android.Hardware.Camera.Size bestSize = null; if (Application.Context.Resources.Configuration.Orientation == Android.Content.Res.Orientation.Landscape) { foreach (Android.Hardware.Camera.Size size in supportedPreviewSizes) { var diff = Math.Abs(size.Width - mTextureView.Width); if (diff < minDiff) { minDiff = diff; bestSize = size; } } } else { foreach (Android.Hardware.Camera.Size size in supportedPreviewSizes) { var diff = Math.Abs(size.Height - mTextureView.Width); if (diff < minDiff) { minDiff = diff; bestSize = size; } } } cameraParams.SetPreviewSize(bestSize.Width, bestSize.Height); mCamera.SetParameters(cameraParams); }
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { var width = _context.Resources.DisplayMetrics.WidthPixels; var height = _context.Resources.DisplayMetrics.HeightPixels; if (_supportedPreviewSizes != null) { _previewSize = GetOptimalPreviewSize(_supportedPreviewSizes, width, height); _picSize = GetOptimalPicSize(_supportedPicSizes, width, height); } float ratio; if (_previewSize.Height >= _previewSize.Width) { ratio = (float)_previewSize.Height / (float)_previewSize.Width; } else { ratio = (float)_previewSize.Width / (float)_previewSize.Height; } float camHeight = (int)(width * ratio); float newCamHeight; float newHeightRatio; if (camHeight < height) { newHeightRatio = (float)height / (float)_previewSize.Height; newCamHeight = (newHeightRatio * camHeight); SetMeasuredDimension((int)(width * newHeightRatio), (int)newCamHeight); } else { newCamHeight = camHeight; SetMeasuredDimension(width, (int)newCamHeight); } }
/** * [IMPORTANT!] This is a convenient function to determine what's the proper * preview/picture size to be assigned to the camera, by looking at * the list of supported sizes and the maximum value given * @param sizes sizes that are currently supported by the camera hardware, * retrived with {@link Camera.Parameters#getSupportedPictureSizes()} or {@link Camera.Parameters#getSupportedPreviewSizes()} * @param widthThreshold the maximum value we want to apply * @return an optimal size <= widthThreshold */ private Camera.Size GetBestSize(IList <Camera.Size> sizes, int widthThreshold) { Camera.Size bestSize = null; foreach (var currentSize in sizes) { var isDesiredRatio = ((currentSize.Width / AspectRationWidth) == (currentSize.Height / AspectRationHeight)); var isBetterSize = (bestSize == null || currentSize.Width > bestSize.Width); var isInBounds = currentSize.Width <= widthThreshold; if (isDesiredRatio && isInBounds && isBetterSize) { bestSize = currentSize; } } if (bestSize == null) { bestSize = sizes[0]; Console.WriteLine("determineBestSize(): can't find a good size. Setting to the very first..."); } return(bestSize); }
public Camera.Size GetBestPreviewSize(int width, int height, Camera.Parameters parameters) { Camera.Size result = null; foreach (Camera.Size size in parameters.SupportedPreviewSizes) { if (size.Width <= width && size.Height <= height) { if (result == null) { result = size; } else { int resultArea = result.Width * result.Height; int newArea = size.Width * size.Height; if (newArea > resultArea) { result = size; } } } } return (result); }
private void initRecorder() { recorder = new MediaRecorder(); path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/PincidentReport.mp4"; recorder.SetCamera(camera); string manufacturer = Build.Manufacturer; if (ActivityCompat.CheckSelfPermission(CurrentContext, Manifest.Permission.RecordAudio) != Permission.Granted) { ActivityCompat.RequestPermissions(CurrentContext, new System.String[] { Manifest.Permission.RecordAudio }, 10); } else { if (manufacturer.ToLower().Contains("samsung")) { recorder.SetAudioSource(AudioSource.VoiceCommunication); } else { recorder.SetAudioSource(AudioSource.Default); } } recorder.SetVideoSource(VideoSource.Camera); recorder.SetOutputFile(path); recorder.SetOnInfoListener(this); recorder.SetPreviewDisplay(videoView.Holder.Surface); recorder.SetOutputFormat(OutputFormat.Default); var SupportedVideoFrameRateByCamera = camera.GetParameters().SupportedPreviewFrameRates; var SmallToBigFrameRates = SupportedVideoFrameRateByCamera.Reverse(); var maxFrameRate = SmallToBigFrameRates.First().IntValue(); recorder.SetVideoFrameRate(maxFrameRate); var SupportedSizesByCamera = camera.GetParameters().SupportedVideoSizes; var SmallToBigSizesList = SupportedSizesByCamera.Reverse(); Android.Hardware.Camera.Size size = new Android.Hardware.Camera.Size(camera, 0, 0); foreach (var videoSize in SmallToBigSizesList) // The closest Height to 480 { if (videoSize.Height == 480) { size = videoSize; break; } else if (videoSize.Height > 480) { size = videoSize; break; } } if (size.Height == 0) { size = SmallToBigSizesList.ElementAt(0); } recorder.SetVideoSize(size.Width, size.Height); recorder.SetVideoEncoder(VideoEncoder.Mpeg4Sp);// MPEG_4_SP recorder.SetAudioEncoder(AudioEncoder.Aac); recorder.SetMaxDuration(15000); recorder.SetOrientationHint(90); }
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(); } }
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height) { if (holder.Surface == null) { return; } try { camera.StopPreview(); } catch (Exception e) { Log.Debug("Exception", e.Message); return; } try { //FrameLayout layout = (FrameLayout)FindViewById(Resource.Id.frame); //ViewGroup.LayoutParams parms = layout.LayoutParameters; //parms.Height = 960; //parms.Width = 1280; //parms.Height = 1440; //parms.Width = 1920; //layout.LayoutParameters = parms; Display display = WindowManager.DefaultDisplay; Point size = new Point(); display.GetSize(size); int displayWidth = size.X; int displayHeight = size.Y; // resolution of desktop viewer is 640x480 scaleX = Convert.ToInt32(displayWidth / 640); scaleY = Convert.ToInt32(displayHeight / 480); //Configration Camera Parameter(full-size) Android.Hardware.Camera.Parameters parameters = camera.GetParameters(); Android.Hardware.Camera.Size bestPreviewSize = GetBestPreviewSize(width, height, parameters); parameters.SetPictureSize(320, 240); //parameters.SetPreviewSize(bestPreviewSize.Width, bestPreviewSize.Height); parameters.SetPreviewSize(320, 240); parameters.PreviewFormat = Android.Graphics.ImageFormatType.Nv21;// ImageFormat.Nv21; parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeContinuousPicture; camera.SetParameters(parameters); camera.SetPreviewDisplay(holder); camera.SetPreviewCallback(this); camera.StartPreview(); } catch (Exception e) { Log.Debug("Exception", e.Message); } }
/// <summary> /// Filters picture sizes based on the given preview size by considering the aspect ratio and the resolution. /// The first size in the remaining list is then selected. /// </summary> private AndroidCamera.Size determinePictureResolution(AndroidCamera.Parameters parameters, AndroidCamera.Size preview) { /** * The picture aspect ratio has to be as close as possible to the preview ratio. */ Func <AndroidCamera.Size, bool> ratioFilter = picture => { var rawr = ((double)picture.Width / (double)picture.Height) - ((double)preview.Width / (double)preview.Height); return(Math.Abs(rawr) <= 0.1); }; return(parameters.SupportedPictureSizes .Where(ratioFilter) // the picture resolution should not exceed the preview resolution .Where(picture => picture.Width * picture.Height <= preview.Width * preview.Height) .OrderByDescending(picture => picture.Width * picture.Height) .DefaultIfEmpty(parameters.PictureSize) .First()); }
protected override void OnMeasure (int widthMeasureSpec, int heightMeasureSpec) { // We purposely disregard child measurements because act as a // wrapper to a SurfaceView that centers the camera preview instead // of stretching it. int width = ResolveSize (SuggestedMinimumWidth, widthMeasureSpec); int height = ResolveSize (SuggestedMinimumHeight, heightMeasureSpec); SetMeasuredDimension (width, height); if (mSupportedPreviewSizes != null) { mPreviewSize = GetOptimalPreviewSize (mSupportedPreviewSizes, width, height); } }