UWP圖片編輯器(塗鴉、裁剪、合成)

1、編輯器簡介

寫這個控件以前總想找一找開源的,能夠偷下懶省點事。但是各類地方都搜遍了也沒有找到。git

因而,那就作第一個吃螃蟹的人吧!github

控件主要有三個功能:塗鴉、裁剪、合成。 算法

塗鴉:主要是用到了InkToolbar和InkCanvas。 canvas

裁剪:這個用到的比較複雜,源碼會公佈出來。 編輯器

合成:將塗鴉圖層按比例縮放到原圖大小,而後兩個圖層進行合成。 this

本文GitHub地址spa

2、塗鴉功能實現方法

這裏爲了省事用了一個別人寫好的控件,主要是切換畫筆、顏色方便。其實能夠本身單獨寫控件的。code

能用現成的就用現成的,少寫好多行代碼了。orm

Inktoolbar下載地址: blog

https://visualstudiogallery.msdn.microsoft.com/58194dfe-df44-4c4e-893a-1eca40675269

初始化Ink相關控件:

                    
<InkCanvas Name="ink_canvas">
<ink:InkToolbar x:Name="inktoolbar"  ButtonHeight="60" ButtonWidth="60" ButtonBackground="Transparent" >
ink_canvas.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Touch | CoreInputDeviceTypes.Pen;
inktoolbar.TargetInkCanvas = this.ink_canvas;


獲取塗鴉方法:

一、從InkCanvas中獲取:

這個獲取的就是屏幕渲染出來的圖片,也就是說圖片基本都是被縮放過的。

優勢:速度快。

缺點:圖片是被放縮成控件大小的,不是原圖的大小。好比原圖是2880*1600的圖,塗鴉事後取出來的圖片就變成288*160的大小了。

CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)ink_canvas.ActualWidth, (int)ink_canvas.ActualHeight, 96);
renderTarget.SetPixelBytes(new byte[(int)ink_canvas.ActualWidth * 4 * (int)ink_canvas.ActualHeight]);

using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Png, 1f);
}

二、圖層合成

先獲取ink圖層,縮放至原圖大小。而後將Ink圖層和原圖圖層合併。縮放和合並算法的源碼會在文章末尾。

優勢:圖片大小不改變,至關因而在原圖上塗鴉。

缺點:計算複雜,比較耗時。

CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)ink_canvas.ActualWidth, (int)ink_canvas.ActualHeight, 96);
renderTarget.SetPixelBytes(new byte[(int)ink_canvas.ActualWidth * 4 * (int)ink_canvas.ActualHeight]);
using (var ds = renderTarget.CreateDrawingSession())
{
    IReadOnlyList<InkStroke> inklist = ink_canvas.InkPresenter.StrokeContainer.GetStrokes();

    Debug.WriteLine("Ink_Strokes Count:  " + inklist.Count);
   ds.DrawInk(inklist);
}
var inkpixel = renderTarget.GetPixelBytes();
WriteableBitmap bmp = new WriteableBitmap((int)ink_canvas.ActualWidth, (int)ink_canvas.ActualHeight);
Stream s = bmp.PixelBuffer.AsStream();
s.Seek(0, SeekOrigin.Begin);
s.Write(inkpixel, 0, (int)ink_canvas.ActualWidth * 4 * (int)ink_canvas.ActualHeight);

WriteableBitmap ink_wb = await ImageProcessing.ResizeByDecoderAsync(bmp, sourceImage.PixelWidth, sourceImage.PixelHeight, true);

WriteableBitmap combine_wb = await ImageProcessing.CombineAsync(sourceImage, ink_wb);

 

3、裁剪功能實現方法

在WPF中已經有不少前人寫過的模板了,這裏不須要作太多修改就可使用。代碼會在文章末尾給出

可是有一個問題在UWP中會引發卡頓現象。

剪裁的時候爲了方便用戶對齊,會將裁剪區域分紅九宮格。

這時候想到了畫四個矩形,可是這樣子會卡頓,很是卡頓、很是卡頓、很是卡頓

 

<Rectangle x:Name="horizontalLine" Canvas.Left="{Binding SelectedRect.Left}" Canvas.Top="{Binding HorizontalLineCanvasTop}" Height="1" Width="{Binding SelectedRect.Width}" Fill="{ThemeResource ApplicationForegroundThemeBrush}"/>
<Rectangle x:Name="verticalLine" Canvas.Left="{Binding VerticalLineCanvasLeft}" Canvas.Top="{Binding SelectedRect.Top}" Width="1" Height="{Binding SelectedRect.Height}" Fill="{ThemeResource ApplicationForegroundThemeBrush}"/>
<Rectangle x:Name="horizontalLine1" Canvas.Left="{Binding SelectedRect.Left}" Canvas.Top="{Binding HorizontalLine1CanvasTop}" Height="1" Width="{Binding SelectedRect.Width}" Fill="{ThemeResource ApplicationForegroundThemeBrush}"/>
<Rectangle x:Name="verticalLine1" Canvas.Left="{Binding VerticalLine1CanvasLeft}" Canvas.Top="{Binding SelectedRect.Top}" Width="1" Height="{Binding SelectedRect.Height}" Fill="{ThemeResource ApplicationForegroundThemeBrush}"/>

解決方案是畫Path,因爲繪圖機制的緣由,這樣子就不會有卡頓現象,給用戶如絲般潤滑的感受。

<Path x:Name="horizontalLine" Fill="{ThemeResource ApplicationForegroundThemeBrush}" Stroke="{ThemeResource ApplicationForegroundThemeBrush}" StrokeThickness="0.5">
  <Path.Data>
      <RectangleGeometry Rect="{Binding HorizontalLine1}"/>
  </Path.Data>
</Path>
<Path x:Name="horizontalLine1" Fill="{ThemeResource ApplicationForegroundThemeBrush}" Stroke="{ThemeResource ApplicationForegroundThemeBrush}" StrokeThickness="0.5">
  <Path.Data>
    <RectangleGeometry Rect="{Binding HorizontalLine2}"/>
  </Path.Data>
</Path>
<Path x:Name="verticalLine" Fill="{ThemeResource ApplicationForegroundThemeBrush}" Stroke="{ThemeResource ApplicationForegroundThemeBrush}" StrokeThickness="0.5">
  <Path.Data>
    <RectangleGeometry Rect="{Binding VerticalLine1}"/>
  </Path.Data>
</Path>
<Path x:Name="verticalLine1" Fill="{ThemeResource ApplicationForegroundThemeBrush}" Stroke="{ThemeResource ApplicationForegroundThemeBrush}" StrokeThickness="0.5">
  <Path.Data>
    <RectangleGeometry Rect="{Binding VerticalLine2}"/>
  </Path.Data>
</Path>

 

4、圖片合成

先對塗鴉圖層進行縮放,這裏能夠用Fant、雙三次樣條插值等算法。而後根據塗鴉圖層和原圖層通透度進行合成。

 圖層合併的方法不必定正確,感受上是這樣的:上層的通透度若是是0.7。那麼合成後的像素就是 :上層像素值 x 0.7 + 下層像素值 x (1-0.7) 。若是有多層圖層的話,從上至下依次進行合成。

      public static byte[] Combine(byte[] basesrc, byte[] floatsrc,int width, int height)
        {
            byte[] retsrc = new byte[height * 4 * width];

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    int[] color_float = getBGR(floatsrc, x, y, width);
                    int alpha_float = GAP(floatsrc, x, y, width);

                    int[] color_base = getBGR(basesrc, x, y, width);
                    int alpha_base = GAP(basesrc, x, y, width);

                    int R=0, G=0, B=0, A=0;

                    if (alpha_base != 255)
                    {
                        color_base[0] = color_base[1] = color_base[2] = alpha_base = 255;
                        color_base[0] = (255 * (255 - alpha_float) + color_float[0] * alpha_base) / 255;
                        color_base[1] = (255 * (255 - alpha_float) + color_float[1] * alpha_base) / 255;
                        color_base[2] = (255 * (255 - alpha_float) + color_float[2] * alpha_base) / 255;
                        alpha_base = 255;
                    }

                    if (color_float[0] == 0 && color_float[1] == 0 && color_float[2] == 0 && alpha_float == 0)
                    {
                        B = color_base[0];
                        G = color_base[1];
                        R = color_base[2];
                        A = alpha_base;
                    }
                    else
                    {
                        B = (color_base[0] * (255 - alpha_float) + color_float[0] * alpha_float) / 255;
                        G = (color_base[1] * (255 - alpha_float) + color_float[1] * alpha_float) / 255;
                        R = (color_base[2] * (255 - alpha_float) + color_float[2] * alpha_float) / 255;
                        A = alpha_float+(255-alpha_float)*(alpha_base/255);
                        A = A > 255 ? 255 : A;
                    }
                    
                    putpixel(retsrc, x, y, width, R, G, B, A);
                }
            }

            return retsrc;
        }

 

GtiHub地址:https://github.com/LeoLeeCN/UWP_Toolkit

相關文章
相關標籤/搜索