/// <summary> /// Initialize custom serializer. /// </summary> /// <param name="serializers">Known serializers</param> /// <param name="targetSchema">Target type schema</param> /// <returns>Type schema</returns> public TypeSchema Initialize(KnownSerializers serializers, TypeSchema targetSchema) { if (targetSchema == null) { TypeMemberSchema[] schemaMembers = new TypeMemberSchema[6] { new TypeMemberSchema("compression", typeof(CompressionMethod).AssemblyQualifiedName, false), new TypeMemberSchema("image", typeof(UnmanagedBuffer).AssemblyQualifiedName, true), new TypeMemberSchema("width", typeof(int).AssemblyQualifiedName, true), new TypeMemberSchema("height", typeof(int).AssemblyQualifiedName, true), new TypeMemberSchema("stride", typeof(int).AssemblyQualifiedName, true), new TypeMemberSchema("pixelFormat", typeof(Imaging.PixelFormat).AssemblyQualifiedName, true), }; var type = typeof(Imaging.Image); var name = TypeSchema.GetContractName(type, serializers.RuntimeVersion); this.schema = new TypeSchema(name, TypeSchema.GetId(name), type.AssemblyQualifiedName, TypeFlags.IsCollection, schemaMembers, Version); } else { this.schema = targetSchema; } // Check to see if we can retrieve the custom compression serializer try { var assembly = System.Reflection.Assembly.Load(new System.Reflection.AssemblyName("Microsoft.Psi.Imaging.Windows")); if (assembly != null) { var ic = assembly.CreateInstance("Microsoft.Psi.Imaging.ImageCompressor"); if (ic != null) { this.imageCompressor = ic as IImageCompressor; this.imageCompressor.Initialize(compressionMethod); } } } catch (System.IO.FileNotFoundException) { this.imageCompressor = null; } return(this.schema); }
public override async Task Run(IJobExecutionContext context) { logger.Info("开始处理图片素材事务..."); await Console.Out.WriteLineAsync("开始处理图片素材事务..."); var count = 0; IImageCompressor imageCompressor = _provider.GetService <IImageCompressor>(); IMaterialCenterConfigService _configService = _provider.GetService <IMaterialCenterConfigService>(); MaterialCenterConfig config = await _configService.GetAsync(); //var folder = Path.Combine(config.PictureSavePath, oppositePath); //if (config != null && !string.IsNullOrEmpty(config.PictureSavePath)) //{ // folder = Path.Combine(config.PictureSavePath, oppositePath); //} //取得待压缩列表 using (var dapper = DapperFactory.Create()) { var builder = new QueryBuilder().Select <Picture>().From <Picture>() .Where(new ConditionBuilder().Equal <Picture>(m => m.IsCompressed, false) .And().Equal <Picture>(m => m.IsDeleted, false)); var items = await dapper.QueryListAsync <Picture>(builder); foreach (var item in items) { //判断文件是否存在,如果不存在则自动删除该条数据 if (!File.Exists(Path.Combine(config.PictureSavePath, item.FilePath))) { await dapper.DeleteAsync <Picture>(new { item.Id }); continue; } if ("png".EqualOrdinalIgnoreCase(item.FileType) || "jpg".EqualOrdinalIgnoreCase(item.FileType) || "jpeg".EqualOrdinalIgnoreCase(item.FileType)) { var fileName = item.Id + "." + item.FileType; var filePath = item.Folder + fileName; try { //压缩图片 await imageCompressor.Compress(Path.Combine(config.PictureSavePath, item.FilePath), Path.Combine(config.PictureSavePath, filePath)); //缩略图 var thumbPath = item.Folder + item.Id + "_thumb." + item.FileType; await imageCompressor.Resize(Path.Combine(config.PictureSavePath, item.FilePath), Path.Combine(config.PictureSavePath, thumbPath), new { method = "fit", width = 320, height = 320 }); var fileInfo = new FileInfo(Path.Combine(config.PictureSavePath, item.FilePath)); var model = new { FilePath = filePath, FileName = fileName, ThumbPath = thumbPath, IsCompressed = true, FileSize = fileInfo.Length }; await dapper.UpdateAsync <Picture>(model, new { item.Id }); } catch (Exception ex) { logger.Error(ex); await Console.Out.WriteLineAsync(ex.ToString()); } } else { await dapper.UpdateAsync <Picture>(new { IsCompressed = true, ThumbPath = item.FilePath }, new { item.Id }); } count++; } } logger.Info($"共处理{count}张照片"); await Console.Out.WriteLineAsync($"共处理{count}张照片"); logger.Info($"结束处理图片素材事务..."); await Console.Out.WriteLineAsync("结束处理图片素材事务..."); }
/// <summary> /// Maybe called to initialize type of compression to use. Default is no compression. /// </summary> /// <param name="compressor">Compressor to be used.</param> public static void ConfigureCompression(IImageCompressor compressor) { imageCompressor = compressor; }
/// <summary> /// Configure the type of compression to use when serializing images. Default is no compression. /// </summary> /// <param name="imageCompressor">Compressor to be used.</param> public static void ConfigureCompression(IImageCompressor imageCompressor) { CustomSerializer.imageCompressor = imageCompressor; }
public override async Task Run(IJobExecutionContext context) { logger.Info("开始处理视频素材事务..."); await Console.Out.WriteLineAsync("开始处理视频素材事务..."); var count = 0; IImageCompressor imageCompressor = _provider.GetService <IImageCompressor>(); IMaterialCenterConfigService _configService = _provider.GetService <IMaterialCenterConfigService>(); MaterialCenterConfig config = await _configService.GetAsync(); //var folder = Path.Combine(config.PictureSavePath, oppositePath); //if (config != null && !string.IsNullOrEmpty(config.PictureSavePath)) //{ // folder = Path.Combine(config.PictureSavePath, oppositePath); //} //取得待压缩列表 using (var dapper = DapperFactory.Create()) { var builder = new QueryBuilder().Select <Video>().From <Video>() .Where(new ConditionBuilder().IsNullOrEmpty <Video>(x => x.ThumbPath) .And().Equal <Video>(m => m.IsDeleted, false)); var items = await dapper.QueryListAsync <Video>(builder); foreach (var item in items) { //判断文件是否存在 if (!File.Exists(Path.Combine(config.PictureSavePath, item.FilePath))) { continue; } try { //缩略图 var thumbPath = item.Folder + item.Id + "_thumb.jpg"; var thumb = VideoHelper.GetPreviewImage(Path.Combine(config.PictureSavePath, item.FilePath), Path.Combine(config.PictureSavePath, thumbPath), 320, 180, 1); if (thumb != null) { thumb = thumbPath; } //时长 var duration = VideoHelper.GetVideoDuration(Path.Combine(config.PictureSavePath, item.FilePath)); await dapper.UpdateAsync <Video>(new { ThumbPath = thumb, Duration = duration }, new { item.Id }); } catch (Exception ex) { logger.Error(ex); await Console.Out.WriteLineAsync(ex.ToString()); } count++; } } logger.Info($"共处理{count}个视频"); await Console.Out.WriteLineAsync($"共处理{count}个视频"); logger.Info($"结束处理视频素材事务..."); await Console.Out.WriteLineAsync("结束处理视频素材事务..."); }
public static async Task <List <IGenericOcrRunner> > GetStandardOcrs(IOcrCache ocrCache, StandardOcrSettings standardOcrSettings, IImageCompressor imageCompressor) { List <IGenericOcrRunner> genericOcrRunners = new List <IGenericOcrRunner>(); var ocrParams = new HighLevelOcrServiceParams { OcrCache = ocrCache, ImageCompressor = imageCompressor }; if (standardOcrSettings.AwsOcrSettings != null) { var awsSettings = standardOcrSettings.AwsOcrSettings; genericOcrRunners.Add(new AwsOcrService(awsSettings.AccessKey, awsSettings.SecretKey, awsSettings.Region, ocrParams)); } if (standardOcrSettings.AzureOcrSettings != null) { var azureSettings = standardOcrSettings.AzureOcrSettings; genericOcrRunners.Add(new AzureOcrService(azureSettings.SubscriptionKey, azureSettings.Endpoint, ocrParams)); } if (standardOcrSettings.GoogleOcrSettings != null) { var googleSettings = standardOcrSettings.GoogleOcrSettings; genericOcrRunners.Add(new GoogleOcrService(googleSettings.ApiToken, ocrParams)); } if (standardOcrSettings.TesseractOcrSettings != null) { var tesseractSettings = standardOcrSettings.TesseractOcrSettings; if (tesseractSettings.ShouldTryToAutomaticallInstall && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var tesseractInstaller = new WindowsTesseractInstaller(tesseractSettings.TesseractExecutable); await tesseractInstaller.Install(); tesseractSettings.Installer = tesseractInstaller; tesseractSettings.TesseractExecutable = tesseractInstaller.TesseractExePath; } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { tesseractSettings.TesseractExecutable = "tesseract"; } genericOcrRunners.Add(new TesseractOcrService(tesseractSettings.TesseractExecutable, tesseractSettings.DataDir, ocrParams)); } if (standardOcrSettings.WindowsOcrSettings != null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { genericOcrRunners.Add(new WindowsOcrService(ocrParams)); } return(genericOcrRunners); }
public StandardMultiOcrRunnerFactory(StandardOcrSettings settings, IOcrCache ocrCache) { this.Settings = settings; this.ocrCache = ocrCache; this.imageCompressor = settings.ImageCompressor; }
public StandardOcrSettings(IImageCompressor imageCompressor) { ImageCompressor = imageCompressor; }