예제 #1
0
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            try
            {
                if (!(Context.GetSystemService(Class.FromType(typeof(CameraManager))) is CameraManager manager))
                {
                    return;
                }

                string frontCameraId = manager.GetCameraIdList().FirstOrDefault(id => IsFrontFacingCamera(manager, id));
                CameraCharacteristics frontCamera = manager.GetCameraCharacteristics(frontCameraId);

                if (!(frontCamera?.Get(CameraCharacteristics.ScalerStreamConfigurationMap) is StreamConfigurationMap map))
                {
                    return;
                }

                Size[] outputSizes = map.GetOutputSizes(Class.FromType(holder.GetType()));
                Size   firstSmall  = outputSizes
                                     .Where(size => (double)size.Width / size.Height == 16d / 9)
                                     .First(size => size.Height < 900 && size.Width < 900);

                holder.SetFixedSize(firstSmall.Width, firstSmall.Height);

                CameraDevice.StateCallback stateCallback = new MyCameraCallback(holder.Surface, Element);
                manager.OpenCamera(frontCameraId, stateCallback, new Handler(msg => { }));
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
예제 #2
0
        public void PlatformToSystemException(float width, float height)
        {
#if __IOS__
            var platform = new CoreGraphics.CGSize(width, height);
            Assert.Throws <ArgumentOutOfRangeException>(() => platform.ToSystemSize());
#elif __ANDROID__
            var platform = new Android.Util.Size((int)width, (int)height);
            Assert.Throws <ArgumentOutOfRangeException>(() => platform.ToSystemSize());
#elif WINDOWS_UWP
            var platform = new Windows.Foundation.Size(width, height);
            Assert.Throws <ArgumentOutOfRangeException>(() => platform.ToSystemSize());
#endif
        }
예제 #3
0
        protected override void FindLargestResolution()
        {
            StreamConfigurationMap map =
                (StreamConfigurationMap)CameraCharacteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);

            DroidSize[] imageSupportedSizesAndroid = map.GetOutputSizes((int)ImageFormatType.Jpeg);

            DroidSize largestSizeAndroid = imageSupportedSizesAndroid
                                           .OrderByDescending(droidSize => (long)droidSize.Height * droidSize.Width)
                                           .FirstOrDefault();

            _largestImageResolution = new Size(largestSizeAndroid.Width, largestSizeAndroid.Height);
        }
예제 #4
0
        private static Android.Util.Size GetRotatedPreviewSize(Android.Util.Size requestSize, bool swappedDimensions)
        {
            var rotatedPreviewWidth  = requestSize.Width;
            var rotatedPreviewHeight = requestSize.Height;

            if (!swappedDimensions)
            {
                return(new Android.Util.Size(rotatedPreviewWidth, rotatedPreviewHeight));
            }
            rotatedPreviewWidth  = requestSize.Height;
            rotatedPreviewHeight = requestSize.Width;

            return(new Android.Util.Size(rotatedPreviewWidth, rotatedPreviewHeight));
        }
예제 #5
0
        public void PlatformToSystem()
        {
#if __IOS__
            var platform = new CoreGraphics.CGSize(width, height);
#elif __ANDROID__
            var platform = new Android.Util.Size(width, height);
#else
            var platform = new Windows.Foundation.Size(width, height);
#endif
            var system = platform.ToSystemSize();

            Assert.Equal(width, system.Width);
            Assert.Equal(height, system.Height);
        }
예제 #6
0
        internal void Recycle()
        {
            if (Content?.Element != null)
            {
                Content.Element.MeasureInvalidated -= ElementMeasureInvalidated;
            }

            if (Content?.View != null)
            {
                RemoveView(Content.View);
            }

            Content = null;
            _size   = null;
        }
예제 #7
0
        private Android.Util.Size GetBufferSize(Android.Util.Size requestSize)
        {
            var characteristics = _manager.GetCameraCharacteristics(_camera.Id);
            var map             = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);
            // ReSharper disable once CoVariantArrayConversion
            var largest = (Android.Util.Size)Collections.Max(
                Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Yuv420888)),
                new CompareSizesByArea());

            var swappedDimensions = IsSwappedDimensions(characteristics);

            var rotatedPreviewSize = GetRotatedPreviewSize(requestSize, swappedDimensions);
            var maxPreviewSize     = GetMaxPreviewSize(swappedDimensions);

            return(ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))),
                                     rotatedPreviewSize.Width, rotatedPreviewSize.Height, maxPreviewSize.Width,
                                     maxPreviewSize.Height, largest));
        }
예제 #8
0
        /// <summary>
        /// プレビューの画面サイズを算出
        /// </summary>
        /// <param name="cameraId"></param>
        /// <returns></returns>
        private Android.Util.Size GetPreviewSize(string cameraId)
        {
            CameraManager          manager         = (CameraManager)activity.GetSystemService(Context.CameraService);
            var                    characteristics = manager.GetCameraCharacteristics(cameraId);
            StreamConfigurationMap map             = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);
            // 最大サイズからratioを取得
            var maxSize = (Android.Util.Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), new CompareSizesByArea());
            int h       = maxSize.Height;
            int w       = maxSize.Width;

            System.Diagnostics.Debug.WriteLine($"max size: width={w},height={h}");

            // ディスプレイサイズ
            var displaySize = new Android.Graphics.Point();

            activity.WindowManager.DefaultDisplay.GetSize(displaySize);
            // 横向きに補正
            var maxWidth  = displaySize.X > displaySize.Y ? displaySize.X : displaySize.Y;
            var maxHeight = displaySize.Y < displaySize.X ? displaySize.Y : displaySize.X;

            System.Diagnostics.Debug.WriteLine($"display: width={maxWidth},height={maxHeight}");

            // 画面サイズに収まり、アスペクト比が一致するサイズ
            var list  = new List <Android.Util.Size>();
            var sizes = map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture)));

            foreach (var size in sizes)
            {
                System.Diagnostics.Debug.WriteLine($"size: width={size.Width},height={size.Height}");

                if ((size.Width <= maxWidth) && (size.Height <= maxHeight) && size.Height == size.Width * h / w)
                {
                    list.Add(size);
                }
            }

            // 最大のやつを取得
            var prevSize = (Android.Util.Size)Collections.Max(list, new CompareSizesByArea());
            // 無理から縦向きに変更
            var ret = new Android.Util.Size(prevSize.Height, prevSize.Width);

            return(ret);
        }
예제 #9
0
        public void Bind(object itemBindingContext, ItemsView itemsView,
                         Action <ASize> reportMeasure = null, ASize size = null)
        {
            var template = _template.SelectDataTemplate(itemBindingContext, itemsView);

            if (template != _selectedTemplate)
            {
                _itemContentView.Recycle();
                View = (View)template.CreateContent();
                _itemContentView.RealizeContent(View);
                _selectedTemplate = template;
            }

            _itemContentView.HandleItemSizingStrategy(reportMeasure, size);

            // Set the binding context before we add it as a child of the ItemsView; otherwise, it will
            // inherit the ItemsView's binding context
            View.BindingContext = itemBindingContext;

            itemsView.AddLogicalChild(View);
        }
예제 #10
0
        /// <summary>
        /// 背面カメラを開く
        /// </summary>
        private void OpenBackCamera()
        {
            System.Diagnostics.Debug.WriteLine("CameraPageRenderer.OpenBackCamera");

            string        selectedCameraId = string.Empty;
            CameraManager manager          = (CameraManager)activity.GetSystemService(Context.CameraService);

            // 背面カメラを探す
            foreach (var cameraId in manager.GetCameraIdList())
            {
                var characteristics = manager.GetCameraCharacteristics(cameraId);
                var facing          = (Integer)characteristics.Get(CameraCharacteristics.LensFacing);
                if (facing != null && facing == Integer.ValueOf((int)LensFacing.Back))
                {
                    selectedCameraId = cameraId;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(selectedCameraId))
            {
                // プレビューサイズの取得
                PreviewSize = this.GetPreviewSize(selectedCameraId);
                System.Diagnostics.Debug.WriteLine($"previewSize: w={PreviewSize.Width},h={PreviewSize.Height}");

                // これでいいの?
                TextureView.LayoutParameters = new FrameLayout.LayoutParams(PreviewSize.Width, PreviewSize.Height);
                SurfaceTexture.SetDefaultBufferSize(PreviewSize.Width, PreviewSize.Height);

                // カメラを開く (成功したらコールバックが呼ばれる)
                manager.OpenCamera(selectedCameraId, new CameraStateListener(this), null);
            }
            else
            {
                string msg = "背面カメラが見つけられなかった";
                System.Diagnostics.Debug.WriteLine(msg);
                throw new System.Exception(msg);
            }
        }
예제 #11
0
 public static Size ToSystemSize(this AndroidSize size) =>
 new Size(size.Width, size.Height);
예제 #12
0
        private static SizeOptions GetSizeOptions(IReadOnlyList <Android.Util.Size> choices, int textureViewWidth,
                                                  int textureViewHeight, int maxWidth, int maxHeight, Android.Util.Size aspectRatio)
        {
            // Collect the supported resolutions that are at least as big as the preview Surface
            var bigEnough = new List <Android.Util.Size>();
            // Collect the supported resolutions that are smaller than the preview Surface
            var notBigEnough = new List <Android.Util.Size>();
            var w            = aspectRatio.Width;
            var h            = aspectRatio.Height;

            foreach (var option in choices)
            {
                if (option.Width > maxWidth || option.Height > maxHeight ||
                    option.Height != option.Width * h / w)
                {
                    continue;
                }
                if (option.Width >= textureViewWidth &&
                    option.Height >= textureViewHeight)
                {
                    bigEnough.Add(option);
                }
                else
                {
                    notBigEnough.Add(option);
                }
            }

            return(new SizeOptions(bigEnough, notBigEnough));
        }
예제 #13
0
        private static Android.Util.Size ChooseOptimalSize(IReadOnlyList <Android.Util.Size> choices,
                                                           int textureViewWidth,
                                                           int textureViewHeight, int maxWidth, int maxHeight, Android.Util.Size aspectRatio)
        {
            var sizeOptions = GetSizeOptions(choices, textureViewWidth, textureViewHeight, maxWidth, maxHeight,
                                             aspectRatio);

            // Pick the smallest of those big enough. If there is no one big enough, pick the
            // largest of those not big enough.
            if (sizeOptions.BigEnough.Count > 0)
            {
                return((Android.Util.Size)Collections.Min(sizeOptions.BigEnough, new CompareSizesByArea()));
            }

            if (sizeOptions.NotBigEnough.Count > 0)
            {
                return((Android.Util.Size)Collections.Max(sizeOptions.NotBigEnough, new CompareSizesByArea()));
            }

            Debug.WriteLine("Couldn't find any suitable preview size");
            return(choices[0]);
        }
예제 #14
0
 internal void HandleItemSizingStrategy(Action <ASize> reportMeasure, ASize size)
 {
     _reportMeasure = reportMeasure;
     _size          = size;
 }
예제 #15
0
 public static Android.Graphics.Bitmap Resize(this Android.Graphics.Bitmap image, Android.Util.Size newSize)
 {
     throw new NotImplementedException();
 }
예제 #16
0
 public static Bitmap ToBitmap(this Android.Graphics.Bitmap image, Android.Util.Size scaledSize)
 {
     throw new NotImplementedException();
 }
예제 #17
0
 void SetStaticSize(ASize size)
 {
     _size = size;
 }
예제 #18
0
 public CameraCallBack(MediaRecorder recorder, SurfaceTexture surfaceTexture, Android.Util.Size previewSize)
 {
     _recorder       = recorder;
     _surfaceTexture = surfaceTexture;
     _previewSize    = previewSize;
 }