/// <summary> /// Calls Generate template function and processes result /// </summary> /// <param name="descriptionFileName">Name of the description file</param> /// <param name="descriptionData">Template description data in bytes</param> /// <param name="imagesPath">Cloud storage folder where images are located</param> /// <param name="additionalParams">Internal parameters</param> /// <returns>Generated content with template and image</returns> public static TemplateGenerationContent GenerateTemplate(string descriptionFileName, byte[] descriptionData, string imagesPath, string additionalParams) { imagesPath = @"{ ""ExtraStoragePath"":""" + imagesPath + @"""}"; OMRResponse response = RunOmrTask(OmrFunctions.GenerateTemplate, descriptionFileName, descriptionData, imagesPath, false, false, additionalParams); OmrResponseContent responseResult = response.Payload.Result; CheckTaskResult(response.Payload.Result); byte[] template = responseResult.ResponseFiles.First(x => x.Name.Contains(".omr")).Data; byte[] imageFile = responseResult.ResponseFiles.First(x => x.Name.Contains(".png")).Data; TemplateViewModel templateViewModel = TemplateSerializer.JsonToTemplate(Encoding.UTF8.GetString(template)); templateViewModel.ImageFileFormat = ".png"; TemplateGenerationContent generationContent = new TemplateGenerationContent(); generationContent.ImageData = imageFile; generationContent.Template = templateViewModel; return(generationContent); }
/// <summary> /// Converts template data model to template view model /// </summary> /// <param name="template">OmrTemplate to convert</param> /// <returns>Resulting TemplateViewModel</returns> public static TemplateViewModel ConvertModelToViewModel(OmrTemplate template) { TemplateViewModel templateViewModel = new TemplateViewModel("Loaded template"); templateViewModel.TemplateId = template.TemplateId; templateViewModel.FinalizationComplete = template.FinalizationComplete; OmrPage page = template.Pages[0]; templateViewModel.TemplateImageName = page.ImageName; templateViewModel.ImageFileFormat = page.ImageFormat; List <BaseQuestionViewModel> elements = new List <BaseQuestionViewModel>(); foreach (OmrElement modelElement in page.Elements) { if (modelElement is ChoiceBoxElement) { var choiceBoxViewModel = CreateChoiceBoxViewModel((ChoiceBoxElement)modelElement); elements.Add(choiceBoxViewModel); } else if (modelElement is GridElement) { var gridViewModel = CreateGridViewModel((GridElement)modelElement); elements.Add(gridViewModel); } } templateViewModel.AddQuestions(elements); if (page.ImageData != null) { // loading from file double monitorWidth, monitorHeight; ResolutionUtility.GetMonitorResolution(out monitorWidth, out monitorHeight); var image = TemplateSerializer.DecompressImage(page.ImageData); templateViewModel.TemplateImage = image; templateViewModel.PageWidth = page.Width; templateViewModel.PageHeight = page.Height; TemplateViewModel.ZoomKoefficient = image.PixelWidth / page.Width < 1 ? image.PixelWidth / page.Width : 1; } else { // processing server response templateViewModel.PageWidth = page.Width; templateViewModel.PageHeight = page.Height; } return(templateViewModel); }
/// <summary> /// Performs template correction /// </summary> /// <param name="imageName">The name of the template image</param> /// <param name="imageData">The template image data</param> /// <param name="templateData">The template data</param> /// <param name="wasUploaded">Indicates if image was already uploaded on cloud</param> /// <param name="additionalParams">The additional parameters</param> /// <returns>Corrected template</returns> public static TemplateViewModel CorrectTemplate(string imageName, byte[] imageData, string templateData, bool wasUploaded, string additionalParams) { OMRResponse response = RunOmrTask("CorrectTemplate", imageName, imageData, templateData, wasUploaded, false, additionalParams); OmrResponseContent responseResult = response.Payload.Result; CheckTaskResult(response.Payload.Result); byte[] correctedTemplateData = responseResult.ResponseFiles .First(x => x.Name.Equals("corrected_template.dat")) .Data; TemplateViewModel templateViewModel = TemplateSerializer.JsonToTemplate(Encoding.UTF8.GetString(correctedTemplateData)); templateViewModel.TemplateId = responseResult.TemplateId; return(templateViewModel); }
/// <summary> /// Performs template correction /// </summary> /// <param name="imageName">The name of the template image</param> /// <param name="imageData">The template image data</param> /// <param name="templateData">The template data</param> /// <param name="wasUploaded">Indicates if image was already uploaded on cloud</param> /// <param name="additionalParams">The additional parameters</param> /// <returns>Corrected template</returns> public static TemplateViewModel CorrectTemplate(string imageName, byte[] imageData, string templateData, bool wasUploaded, string additionalParams) { string packedTemplate = PackTemplate(imageName, templateData); OmrResponse response = RunOmrTask(OmrFunctions.CorrectTemplate, imageName, imageData, packedTemplate, wasUploaded, false, additionalParams); OmrResponseContent responseResult = response.Payload.Result; CheckTaskResult(response.Payload.Result); byte[] correctedTemplateData = responseResult.ResponseFiles .First(x => x.Name.Contains(".omrcr")) .Data; TemplateViewModel templateViewModel = TemplateSerializer.JsonToTemplate(Encoding.UTF8.GetString(correctedTemplateData)); templateViewModel.TemplateId = responseResult.TemplateId; return(templateViewModel); }
/// <summary> /// Check provided image size and format and compress image /// </summary> /// <param name="templateImage">Template image</param> /// <param name="imageFileFormat">Provided image format</param> /// <param name="imageSizeInBytes">Image size in bytes</param> /// <returns>Image as base64 string</returns> public static string CheckAndCompressImage(BitmapImage templateImage, string imageFileFormat, long imageSizeInBytes) { string imageData; long imageSize = imageSizeInBytes; if (imageSize > 1024 * 500) { imageData = TemplateSerializer.CompressImageBase64(templateImage, 90); } else { if (string.IsNullOrEmpty(imageFileFormat)) { imageData = TemplateSerializer.CompressImageBase64(templateImage, 100); } else if (imageFileFormat.ToLowerInvariant().Equals(".png")) { imageData = TemplateSerializer.PngToBase64(templateImage); } else if (imageFileFormat.ToLowerInvariant().Equals(".tiff") || imageFileFormat.ToLowerInvariant().Equals(".tif")) { imageData = TemplateSerializer.TiffToBase64(templateImage); } else if (imageFileFormat.ToLowerInvariant().Equals(".gif")) { imageData = TemplateSerializer.GifToBase64(templateImage); } else if (imageFileFormat.ToLowerInvariant().Equals(".jpg") || imageFileFormat.ToLowerInvariant().Equals(".jpeg")) { imageData = TemplateSerializer.CompressImageBase64(templateImage, 100); } else { imageData = TemplateSerializer.CompressImageBase64(templateImage, 100); } } return(imageData); }