mirror of https://github.com/abpframework/abp.git
6 changed files with 187 additions and 2 deletions
@ -0,0 +1,56 @@ |
|||
using SkiaSharp; |
|||
|
|||
namespace Volo.Abp.Imaging; |
|||
|
|||
public static class SkiaSharpBitmapHelper |
|||
{ |
|||
// The encoders write no EXIF, so the orientation has to be applied to the pixels.
|
|||
public static SKBitmap? DecodeUpright(SKCodec codec) |
|||
{ |
|||
var bitmap = SKBitmap.Decode(codec); |
|||
if (bitmap == null || codec.EncodedOrigin == SKEncodedOrigin.TopLeft) |
|||
{ |
|||
return bitmap; |
|||
} |
|||
|
|||
using (bitmap) |
|||
{ |
|||
return ApplyOrigin(bitmap, codec.EncodedOrigin); |
|||
} |
|||
} |
|||
|
|||
private static SKBitmap ApplyOrigin(SKBitmap source, SKEncodedOrigin origin) |
|||
{ |
|||
var matrix = origin switch |
|||
{ |
|||
SKEncodedOrigin.TopRight => new SKMatrix(-1, 0, source.Width, 0, 1, 0, 0, 0, 1), |
|||
SKEncodedOrigin.BottomRight => new SKMatrix(-1, 0, source.Width, 0, -1, source.Height, 0, 0, 1), |
|||
SKEncodedOrigin.BottomLeft => new SKMatrix(1, 0, 0, 0, -1, source.Height, 0, 0, 1), |
|||
SKEncodedOrigin.LeftTop => new SKMatrix(0, 1, 0, 1, 0, 0, 0, 0, 1), |
|||
SKEncodedOrigin.RightTop => new SKMatrix(0, -1, source.Height, 1, 0, 0, 0, 0, 1), |
|||
SKEncodedOrigin.RightBottom => new SKMatrix(0, -1, source.Height, -1, 0, source.Width, 0, 0, 1), |
|||
SKEncodedOrigin.LeftBottom => new SKMatrix(0, 1, 0, -1, 0, source.Width, 0, 0, 1), |
|||
_ => SKMatrix.Identity |
|||
}; |
|||
|
|||
var swapSides = origin is SKEncodedOrigin.LeftTop or SKEncodedOrigin.RightTop or SKEncodedOrigin.RightBottom or SKEncodedOrigin.LeftBottom; |
|||
var info = swapSides ? source.Info.WithSize(source.Height, source.Width) : source.Info; |
|||
|
|||
// DrawBitmap copies the pixels of a mutable bitmap, which doubles the memory for a large photo.
|
|||
source.SetImmutable(); |
|||
|
|||
var result = new SKBitmap(info); |
|||
try |
|||
{ |
|||
using var canvas = new SKCanvas(result); |
|||
canvas.SetMatrix(matrix); |
|||
canvas.DrawBitmap(source, 0, 0); |
|||
return result; |
|||
} |
|||
catch |
|||
{ |
|||
result.Dispose(); |
|||
throw; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
using System.IO; |
|||
using SkiaSharp; |
|||
|
|||
namespace Volo.Abp.Imaging; |
|||
|
|||
public static class SkiaSharpTestImageHelper |
|||
{ |
|||
public static MemoryStream CreateQuadrantJpegWithExifOrientation(ushort orientation) |
|||
{ |
|||
using var bitmap = new SKBitmap(200, 100); |
|||
using (var canvas = new SKCanvas(bitmap)) |
|||
{ |
|||
using var paint = new SKPaint(); |
|||
paint.Color = SKColors.Red; |
|||
canvas.DrawRect(0, 0, 100, 50, paint); |
|||
paint.Color = SKColors.Lime; |
|||
canvas.DrawRect(100, 0, 100, 50, paint); |
|||
paint.Color = SKColors.Blue; |
|||
canvas.DrawRect(0, 50, 100, 50, paint); |
|||
paint.Color = SKColors.Yellow; |
|||
canvas.DrawRect(100, 50, 100, 50, paint); |
|||
} |
|||
|
|||
using var image = SKImage.FromBitmap(bitmap); |
|||
using var data = image.Encode(SKEncodedImageFormat.Jpeg, 100); |
|||
var jpeg = data.ToArray(); |
|||
|
|||
var exif = new byte[] |
|||
{ |
|||
0xFF, 0xE1, 0x00, 0x22, |
|||
(byte)'E', (byte)'x', (byte)'i', (byte)'f', 0x00, 0x00, |
|||
(byte)'M', (byte)'M', 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08, |
|||
0x00, 0x01, |
|||
0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, (byte)(orientation >> 8), (byte)orientation, 0x00, 0x00, |
|||
0x00, 0x00, 0x00, 0x00 |
|||
}; |
|||
|
|||
var stream = new MemoryStream(); |
|||
stream.Write(jpeg, 0, 2); |
|||
stream.Write(exif, 0, exif.Length); |
|||
stream.Write(jpeg, 2, jpeg.Length - 2); |
|||
stream.Position = 0; |
|||
return stream; |
|||
} |
|||
|
|||
public static string GetQuadrantColors(SKBitmap bitmap) |
|||
{ |
|||
return GetColorInitial(bitmap.GetPixel(bitmap.Width / 4, bitmap.Height / 4)) + |
|||
GetColorInitial(bitmap.GetPixel(bitmap.Width * 3 / 4, bitmap.Height / 4)) + |
|||
GetColorInitial(bitmap.GetPixel(bitmap.Width / 4, bitmap.Height * 3 / 4)) + |
|||
GetColorInitial(bitmap.GetPixel(bitmap.Width * 3 / 4, bitmap.Height * 3 / 4)); |
|||
} |
|||
|
|||
private static string GetColorInitial(SKColor color) |
|||
{ |
|||
var red = color.Red > 200; |
|||
var green = color.Green > 200; |
|||
var blue = color.Blue > 200; |
|||
|
|||
return (red, green, blue) switch |
|||
{ |
|||
(true, true, false) => "Y", |
|||
(true, false, false) => "R", |
|||
(false, true, false) => "G", |
|||
(false, false, true) => "B", |
|||
_ => "?" |
|||
}; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue