VinnoImageEncoder.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using SkiaSharp;
  2. using System;
  3. using System.IO;
  4. using Vinno.IUS.Common.Log;
  5. namespace Vinno.vCloud.Common.Vid2.Codec
  6. {
  7. public class VinnoImageEncoder
  8. {
  9. private readonly float _xScale;
  10. private readonly float _yScale;
  11. private int _currentIndex;
  12. public VinnoImageEncoder(float xScale = 1.0f, float yScale = 1.0f)
  13. {
  14. _currentIndex = -1;
  15. _xScale = xScale;
  16. _yScale = yScale;
  17. }
  18. /// <summary>
  19. /// Convert bitmap to jpeg image bytes.
  20. /// </summary>
  21. /// <param name="image"></param>
  22. /// <param name="compressLevel">The compress level for image, default is 75</param>
  23. /// <returns></returns>
  24. private byte[] ImageToBytes(SKBitmap image, long compressLevel = 80)
  25. {
  26. try
  27. {
  28. var newWidth = (int)(image.Width * _xScale);
  29. var newHeight = (int)(image.Height * _yScale);
  30. SKBitmap tempImage = image;
  31. if (image.Width != newWidth || image.Height != newHeight)
  32. {
  33. tempImage = image.Resize(new SKImageInfo(newWidth, newHeight), SKFilterQuality.None);
  34. }
  35. try
  36. {
  37. using (var map = new SKPixmap(new SKImageInfo(tempImage.Width, tempImage.Height, tempImage.ColorType), tempImage.GetPixels()))
  38. {
  39. using (var stream = new SKDynamicMemoryWStream())
  40. {
  41. SKPixmap.Encode(stream, map, SKEncodedImageFormat.Jpeg, (int)compressLevel);
  42. return stream.CopyToData().ToArray();
  43. }
  44. }
  45. }
  46. catch (Exception exception)
  47. {
  48. throw new Exception($"ImageToBytes error:{exception}");
  49. }
  50. finally
  51. {
  52. tempImage.Dispose();
  53. }
  54. }
  55. catch (Exception e)
  56. {
  57. Logger.WriteLineError(
  58. $"Vinno image encode error image width :{image.Width}, image height :{image.Height}, stack is {e}");
  59. }
  60. return new byte[0];
  61. }
  62. /// <summary>
  63. /// Encode a bitmap to VinnoImage.
  64. /// </summary>
  65. /// <param name="image">The bitmap to be encoded.</param>
  66. /// <returns>The VinnoImage</returns>
  67. public VinnoImage Encode(SKBitmap image)
  68. {
  69. _currentIndex++;
  70. return new VinnoImage(_currentIndex, image.Width, image.Height, ImageToBytes(image));
  71. }
  72. /// <summary>
  73. /// Compress a VinnoImage to a 480p image with 50 quality.
  74. /// </summary>
  75. /// <param name="vinnoImage"></param>
  76. /// <returns></returns>
  77. public VinnoImage Compress(VinnoImage vinnoImage)
  78. {
  79. byte[] compressedData = vinnoImage.ImageData;
  80. using (var ms = new MemoryStream(compressedData))
  81. {
  82. using (var image = SKBitmap.Decode(ms))
  83. {
  84. //Get 480p ratio
  85. var ratio = (double)480 / image.Height;
  86. if (ratio < 1)
  87. {
  88. var width = (int)(image.Width * ratio);
  89. var height = (int)(image.Height * ratio);
  90. SKBitmap tempImage = image;
  91. if (image.Width != width || image.Height != height)
  92. {
  93. tempImage = image.Resize(new SKImageInfo(width, height), SKFilterQuality.High);
  94. }
  95. try
  96. {
  97. using (var map = new SKPixmap(new SKImageInfo(tempImage.Width, tempImage.Height, tempImage.ColorType), tempImage.GetPixels()))
  98. {
  99. using (var stream = new SKDynamicMemoryWStream())
  100. {
  101. SKPixmap.Encode(stream, map, SKEncodedImageFormat.Jpeg, 50);
  102. compressedData = stream.CopyToData().ToArray();
  103. }
  104. }
  105. }
  106. catch (Exception exception)
  107. {
  108. throw new Exception($"Compress error:{exception}");
  109. }
  110. finally
  111. {
  112. tempImage.Dispose();
  113. }
  114. }
  115. }
  116. }
  117. var updater = new VinnoImageUpdater(vinnoImage);
  118. updater.Update(compressedData);
  119. return vinnoImage;
  120. }
  121. }
  122. }