Exemplo n.º 1
0
        public void RecogniseTest()
        {
            Stream stream = new MemoryStream(GetFileBytes());

            //Arrange
            var req = new RecogniseParams()
            {
                PhotoName    = "steve",
                PhotoVersion = String.Empty,
                BucketName   = "nvirginiadekanybucket",
                RegEndpoint  = RegionEndpoint.USEast1,
                InputStream  = stream,
                ContentType  = "image/jpeg"
            };

            //Act
            AmazonRekognition service = new AmazonRekognition(awsAccessKeyId, awsSecretAccessKey);
            var resp = service.Recognise(req);

            //Assert
            Assert.AreEqual(System.Net.HttpStatusCode.OK, resp.HttpStatusCode);
        }
        //public IHttpActionResult Recognize([FromBody]String base64String)
        public IHttpActionResult Recognize(RecognizeParam param)
        {
            if (String.IsNullOrEmpty(param.Base64String))
            {
                return(BadRequest("Bad request: base64String parameter must have value."));
            }

            var stream     = new MemoryStream();
            var photoBytes = Convert.FromBase64String(param.Base64String);

            using (var ms = new MemoryStream(photoBytes, 0, photoBytes.Length))
            {
                Image image = Image.FromStream(ms, true);
                image.Save(stream, ImageFormat.Jpeg);
            }

            var awsAccessKeyId     = ConfigurationManager.AppSettings["awsAccessKeyId"];
            var awsSecretAccessKey = ConfigurationManager.AppSettings["awsSecretAccessKey"];
            var bucketName         = ConfigurationManager.AppSettings["bucketName"];
            var photoName          = $"{DateTime.Now}_{Guid.NewGuid()}";

            var req = new RecogniseParams()
            {
                PhotoName    = photoName,
                PhotoVersion = "1",
                BucketName   = bucketName,
                RegEndpoint  = RegionEndpoint.USEast1,
                InputStream  = stream,
                ContentType  = "image/jpeg"
            };

            AmazonRekognition service = new AmazonRekognition(awsAccessKeyId, awsSecretAccessKey);
            var resp = service.Recognise(req);

            return(Ok(resp));
        }