/// <summary> /// A function for responding to S3 create events. It will determine if the object is an image and use Amazon Rekognition /// to detect labels and add the labels as tags on the S3 object. /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public async Task FunctionHandler(S3Event input, ILambdaContext context) { try { foreach (var record in input.Records) { if (!SupportedImageTypes.Contains(Path.GetExtension(record.S3.Object.Key))) { Console.WriteLine($"Object {record.S3.Bucket.Name}:{record.S3.Object.Key} is not a supported image type"); continue; } Console.WriteLine($"Looking for labels in image {record.S3.Bucket.Name}:{record.S3.Object.Key}"); var detectResponses = await this.RekognitionClient.DetectLabelsAsync(new DetectLabelsRequest { MinConfidence = MinConfidence, Image = new Image { S3Object = new Amazon.Rekognition.Model.S3Object { Bucket = record.S3.Bucket.Name, Name = record.S3.Object.Key } } }); var tags = new List <Tag>(); foreach (var label in detectResponses.Labels) { if (tags.Count < 10) { Console.WriteLine($"\tFound Label {label.Name} with confidence {label.Confidence}"); tags.Add(new Tag { Key = label.Name, Value = label.Confidence.ToString() }); } else { Console.WriteLine($"\tSkipped label {label.Name} with confidence {label.Confidence} because the maximum number of tags has been reached"); } } await this.S3Client.PutObjectTaggingAsync(new PutObjectTaggingRequest { BucketName = record.S3.Bucket.Name, Key = record.S3.Object.Key, Tagging = new Tagging { TagSet = tags } }); } return; } catch (Exception ex) { Console.WriteLine(ex); } }
/// <summary> /// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used /// to respond to S3 notifications. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task FunctionHandler(JObject input, ILambdaContext context) { String bucket = (string)input["detail"]["requestParameters"]["bucketName"]; String key = (string)input["detail"]["requestParameters"]["key"]; if (!SupportedImageTypes.Contains(Path.GetExtension(key))) { Console.WriteLine($"Object {bucket}:{key} is not a supported image type"); return; } try { var rs = await this.S3Client.GetObjectMetadataAsync( bucket, key); if (rs.Headers.ContentType.StartsWith("image/")) { using (GetObjectResponse response = await S3Client.GetObjectAsync( bucket, key)) { using (Stream responseStream = response.ResponseStream) { using (StreamReader reader = new StreamReader(responseStream)) { using (var memstream = new MemoryStream()) { var buffer = new byte[512]; var bytesRead = default(int); while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0) { memstream.Write(buffer, 0, bytesRead); } // Perform image manipulation Stream transformedImage = GcImagingOperations.GetConvertedImage(memstream.ToArray()); PutObjectRequest putRequest = new PutObjectRequest() { BucketName = this.TargetBucket, Key = $"thumbnail-{key}", ContentType = rs.Headers.ContentType, InputStream = transformedImage }; await S3Client.PutObjectAsync(putRequest); } } } } } return; } catch (Exception e) { throw; } }
/// <summary> /// 値をコントロールに反映します。 /// </summary> /// <param name="imgTypes">設定する値を表すSupportedImageTypes。</param> private void setValueToControl(SupportedImageTypes imgTypes) { this.targetImages = imgTypes; this.chkBitmap.Checked = ((imgTypes & SupportedImageTypes.Bitmap) != 0); this.chkGif.Checked = ((imgTypes & SupportedImageTypes.Gif) != 0); this.chkJpeg.Checked = ((imgTypes & SupportedImageTypes.Jpeg) != 0); this.chkPng.Checked = ((imgTypes & SupportedImageTypes.Png) != 0); }
/// <summary> /// チェックボックスのCheckdChangedイベントハンドラ。 /// </summary> /// <param name="sender">イベントのソース。</param> /// <param name="e">イベントデータを格納しているEventArgs。</param> private void checkBox_CheckdChanged(object sender, System.EventArgs e) { CheckBox chkBox = (CheckBox)sender; if (chkBox.Checked) { this.targetImages |= (SupportedImageTypes)chkBox.Tag; } else { this.targetImages &= ~(SupportedImageTypes)chkBox.Tag; } }
/// <summary> /// コンストラクタ。 /// </summary> /// <param name="imageTypes">ThumbNailView.TargetExtensionsプロパティを設定します。</param> /// <param name="editorService">プロパティエディタのサービスを表すIWindowsFormsEditorService。</param> public TargetExtensionEditorControl(SupportedImageTypes imageTypes, IWindowsFormsEditorService editorService) : this() { this.setValueToControl(imageTypes); this.editServ = editorService; }