Exemplo n.º 1
0
        private static void DetectTextGetJson()
        {
            var configuration = Construction.GetConfiguration();

            var bucketName = configuration["S3BucketName"];

            var imageFilePath = configuration["ImageFilePath"];
            var objectKey     = Construction.GetKeyNameFromFilePath(imageFilePath);

            using (var rekognitionClient = Construction.GetAmazonRekognitionClient())
            {
                rekognitionClient.AfterResponseEvent += RekognitionClient_AfterResponseEvent;

                var detectTextRequest = new DetectTextRequest
                {
                    Image = new Image
                    {
                        S3Object = new S3Object
                        {
                            Bucket = bucketName,
                            Name   = objectKey,
                        },
                    },
                };

                var detectTextResponse = rekognitionClient.DetectTextAsync(detectTextRequest).Result;

                JsonFileSerializer.Serialize(@"C:\Temp\temp.json", detectTextResponse);
            }
        }
Exemplo n.º 2
0
        public void OnExecute()
        {
            // Don't execute if we are showing help.
            if (ShowHelp)
            {
                return;
            }

            MtxEncoding encoding = null;
            var         version  = Version ?? "auto";

            if (version == "auto")
            {
                if (FntPath != null || FpdPath != null)
                {
                    version = "1";
                }
                else
                {
                    version = "2";
                }
            }
            if (version == "1")
            {
                if (FntPath != null)
                {
                    if (!File.Exists(FntPath))
                    {
                        Error(string.Format(ErrorMessages.FileDoesNotExist, FntPath));
                        return;
                    }

                    var fntFile = new FntFile(FntPath);
                    encoding = new CharacterMapMtxEncoding(fntFile.Characters);
                }
                else if (FpdPath != null)
                {
                    if (!File.Exists(FpdPath))
                    {
                        Error(string.Format(ErrorMessages.FileDoesNotExist, FpdPath));
                        return;
                    }

                    var fpdFile = new FpdFile(FpdPath);
                    encoding = new CharacterMapMtxEncoding(fpdFile.Characters);
                }
                else
                {
                    Error(ErrorMessages.FntOrFpdOptionMustBeSet);
                    return;
                }
            }
            else if (version == "2")
            {
                encoding = new Utf16MtxEncoding();
            }

            foreach (var file in Files)
            {
                if (!File.Exists(file))
                {
                    Error(string.Format(ErrorMessages.FileDoesNotExist, file));
                    continue;
                }

                var format = Format ?? "auto";
                if (format == "auto")
                {
                    var extension = Path.GetExtension(file);

                    if (extension.Equals(".mtx", StringComparison.OrdinalIgnoreCase))
                    {
                        format = "json";
                    }
                    else if (extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
                    {
                        format = "mtx";
                    }
                    else
                    {
                        Error(string.Format(ErrorMessages.CouldNotDetermineOutputFormat, file));
                        continue;
                    }
                }

                if (format == "json")
                {
                    var outputFilename = Output ?? Path.ChangeExtension(file, ".json");

                    try
                    {
                        var mtxFile = new MtxFile(file, encoding);
                        var mtxJson = new MtxJson
                        {
                            Has64BitOffsets = mtxFile.Has64BitOffsets,
                            Entries         = mtxFile.Entries,
                        };
                        JsonFileSerializer.Serialize(outputFilename, mtxJson);
                    }
                    catch (Exception e)
                    {
                        Error(e.Message);
                    }
                }
                else if (format == "mtx")
                {
                    var outputFilename = Output ?? Path.ChangeExtension(file, ".mtx");

                    try
                    {
                        var mtxJson = JsonFileSerializer.Deserialize <MtxJson>(file);
                        var mtxFile = new MtxFile(mtxJson.Entries, encoding, mtxJson.Has64BitOffsets ? true : Has64BitOffsets);
                        mtxFile.Save(outputFilename);
                    }
                    catch (Exception e)
                    {
                        Error(e.Message);
                    }
                }
            }
        }