Ejemplo n.º 1
0
 internal static void PutCustomAttributes(this AnswersEvent answersEvent,
                                          Dictionary <string, object> customAttributes)
 {
     if (customAttributes == null)
     {
         return;
     }
     foreach (var customAttribute in customAttributes)
     {
         if (customAttribute.Value is int)
         {
             answersEvent.PutCustomAttribute(customAttribute.Key, Integer.ValueOf((int)customAttribute.Value));
         }
         else if (customAttribute.Value is long)
         {
             answersEvent.PutCustomAttribute(customAttribute.Key, Long.ValueOf((long)customAttribute.Value));
         }
         else if (customAttribute.Value is float)
         {
             answersEvent.PutCustomAttribute(customAttribute.Key, Float.ValueOf((float)customAttribute.Value));
         }
         else if (customAttribute.Value is double)
         {
             answersEvent.PutCustomAttribute(customAttribute.Key,
                                             Double.ValueOf((double)customAttribute.Value));
         }
         else if (customAttribute.Value is short)
         {
             answersEvent.PutCustomAttribute(customAttribute.Key, Short.ValueOf((short)customAttribute.Value));
         }
         else if (customAttribute.Value is sbyte)
         {
             answersEvent.PutCustomAttribute(customAttribute.Key,
                                             Byte.ValueOf((sbyte)customAttribute.Value));
         }
         else if (customAttribute.Value is decimal)
         {
             answersEvent.PutCustomAttribute(customAttribute.Key,
                                             new BigDecimal(customAttribute.Value.ToString()));
         }
         else
         {
             answersEvent.PutCustomAttribute(customAttribute.Key, customAttribute.Value?.ToString());
         }
     }
 }
Ejemplo n.º 2
0
        protected void CapturePicture
        (
            string picturePath
        )
        {
            if (_cameraDevice == null)
            {
                return;
            }

            if (DeviceOrientation == null)
            {
                throw new NullReferenceException(nameof(DeviceOrientation));
            }

            if (DeviceOrientation == OrientationEventListener.OrientationUnknown)
            {
                return;
            }

            var cameraManager = GetCameraManager();

            CameraCharacteristics cameraCharacteristics
                = cameraManager.GetCameraCharacteristics(_cameraDevice.Id);

            Size[] jpegSizes      = null;
            Size[] thumbnailSizes = null;

            int cameraOrientation = 0;

            if (cameraCharacteristics != null)
            {
                var configurationMap = (StreamConfigurationMap)cameraCharacteristics.Get
                                       (
                    CameraCharacteristics.ScalerStreamConfigurationMap
                                       );

                jpegSizes = configurationMap.GetHighResolutionOutputSizes
                            (
                    (int)ImageFormatType.Jpeg
                            );

                if ((jpegSizes == null) || (jpegSizes.Length == 0))
                {
                    jpegSizes = configurationMap.GetOutputSizes
                                (
                        (int)ImageFormatType.Jpeg
                                );
                }

                thumbnailSizes = cameraCharacteristics.Get
                                 (
                    CameraCharacteristics.JpegAvailableThumbnailSizes
                                 )
                                 .ToArray <Size>();

                cameraOrientation = (int)cameraCharacteristics.Get
                                    (
                    CameraCharacteristics.SensorOrientation
                                    );
            }

            int jpegWidth  = 640; // why exactly these default values?!
            int jpegHeight = 480;

            if ((jpegSizes != null) && (jpegSizes.Length > 0))
            {
                jpegWidth  = jpegSizes[0].Width;
                jpegHeight = jpegSizes[0].Height;
            }

            ImageReader imageReader = ImageReader.NewInstance(jpegWidth,
                                                              jpegHeight,
                                                              ImageFormatType.Jpeg,
                                                              maxImages: 1);
            var outputSurfaces = new List <Surface>(2);

            outputSurfaces.Add(imageReader.Surface);
            outputSurfaces.Add(new Surface(TextureView.SurfaceTexture));

            CaptureRequest.Builder captureBuilder
                = _cameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);

            captureBuilder.AddTarget(imageReader.Surface);
            // https://developer.android.com/reference/android/hardware/camera2/CameraMetadata.html#CONTROL_MODE_AUTO
            captureBuilder.Set(CaptureRequest.ControlMode,
                               1); // CONTROL_MODE_AUTO

            int pictureRotation = (DeviceOrientation.Value + cameraOrientation) % 360;

            captureBuilder.Set(CaptureRequest.JpegOrientation,
                               pictureRotation);

            var jpegQuality = new Byte(JpegQuality);

            captureBuilder.Set(CaptureRequest.JpegQuality,
                               jpegQuality);

            captureBuilder.Set(CaptureRequest.JpegThumbnailQuality,
                               jpegQuality);

            var thumbnailSize = GetBestThumbnailSize(jpegWidth,
                                                     jpegHeight,
                                                     thumbnailSizes);

            captureBuilder.Set(CaptureRequest.JpegThumbnailSize,
                               thumbnailSize);

            var pictureFile = new File(picturePath);

            imageReader.SetOnImageAvailableListener(new ImageAvailableListener(pictureFile),
                                                    _backgroundHandler);

            var captureListener = new CameraCaptureSessionCallback(CreateCameraPreview,
                                                                   OnDone);

            _cameraDevice.CreateCaptureSession(outputSurfaces,
                                               new CameraCaptureStateCallback(captureBuilder,
                                                                              captureListener,
                                                                              _backgroundHandler),
                                               _backgroundHandler);
        }