首页 > 其他分享 >【SkiaSharp绘图05】SKPaint详解(三)之ImageFilter(颜色、组合、膨胀、移位、光照、反射、阴影、腐蚀、变换)

【SkiaSharp绘图05】SKPaint详解(三)之ImageFilter(颜色、组合、膨胀、移位、光照、反射、阴影、腐蚀、变换)

时间:2024-06-18 20:33:21浏览次数:11  
标签:canvas 05 SkiaSharp float ImageFilter paint 滤镜 SKImageFilter

文章目录

ImageFilter

CreateColorFilter 颜色滤镜

public static SkiaSharp.SKImageFilter CreateColorFilter (SkiaSharp.SKColorFilter cf, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

SKImageFilter.CreateColorFilter 的主要作用是根据指定的颜色滤镜,对图像进行颜色调整。它的意义在于能够灵活地对图像进行颜色处理,提供了强大的工具来实现各种颜色效果。常见的颜色滤镜包括灰度滤镜、色相/饱和度/亮度调整滤镜等。

var canvas = e.Surface.Canvas;
canvas.Clear(SKColors.White);

if (skBmp == null) skBmp = SKBitmap.Decode(@"Images\AIWoman.png");

// 创建一个灰度颜色滤镜
var colorFilter = SKColorFilter.CreateColorMatrix(new float[]
{
    0.299f, 0.587f, 0.114f, 0, 0,
    0.299f, 0.587f, 0.114f, 0, 0,
    0.299f, 0.587f, 0.114f, 0, 0,
    0, 0, 0, 1, 0
});
using (var colorFilterImageFilter = SKImageFilter.CreateColorFilter(colorFilter))
using (var paint = new SKPaint())
{
    paint.FilterQuality = SKFilterQuality.High;
    canvas.DrawBitmap(skBmp, new SKRect(10, 10, 400, 400), paint);//原图

    paint.ImageFilter = colorFilterImageFilter;
    canvas.DrawBitmap(skBmp, new SKRect(410, 10, 800, 400), paint);//灰色滤镜
}

创建一个灰度滤镜,将图像转为灰度图。
CreateColorFilter

CreateCompose 滤镜组合

public static SkiaSharp.SKImageFilter CreateCompose (SkiaSharp.SKImageFilter outer, SkiaSharp.SKImageFilter inner);

滤镜组合,将应用inner(第二个)滤镜,再应用outer(第一个)滤镜。

CreateDilate 膨胀滤镜

public static SkiaSharp.SKImageFilter CreateDilate (int radiusX, int radiusY, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

SKImageFilter.CreateDilate 是 SkiaSharp 中用于创建膨胀滤镜的方法。膨胀滤镜是一种形态学滤波器,通过扩展图像中亮(或白)区域的边缘,可以增强或扩大这些区域。这在图像处理和计算机视觉中具有多种用途,如增强特定特征、减少噪声等。

CreateDisplacementMapEffect 移位映射

public static SkiaSharp.SKImageFilter CreateDisplacementMapEffect (SkiaSharp.SKColorChannel xChannelSelector, SkiaSharp.SKColorChannel yChannelSelector, float scale, SkiaSharp.SKImageFilter displacement, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

SKImageFilter.CreateDisplacementMapEffect 是 SkiaSharp 中用于创建位移映射效果的方法。位移映射是一种高级图像处理技术,通过使用一个位移图(displacement map)来偏移目标图像中的像素位置,从而产生扭曲、波纹或其他变形效果。

作用与意义

位移映射效果的主要作用和意义包括:

  1. 图像扭曲:可以用来创建各种图像扭曲效果,如波纹、水流、热空气导致的扭曲等。
  2. 变形效果:可以实现复杂的变形效果,通过调整位移图可以实现不同的视觉效果。
  3. 动态效果:在动画中,可以用位移映射来创建动态变形效果,使得图像呈现出更加生动的视觉效果。
[System.ComponentModel.Description("SKPaint.ImageFilter的CreateDilate/CreateDisplacementMapEffect/CreateCompose")]
public void OnPaintSurface05_02(object sender, SkiaSharp.Views.Desktop.SKPaintGLSurfaceEventArgs e)
{
    var canvas = e.Surface.Canvas;
    canvas.Clear(SKColors.White);

    if (skBmp == null) skBmp = SKBitmap.Decode(@"Images\AIWoman.png");

    using (var filter = SKImageFilter.CreateDilate(3F, 3F))
    using (var paint = new SKPaint())
    {
        paint.FilterQuality = SKFilterQuality.High;
        canvas.DrawBitmap(skBmp, new SKRect(10, 10, 400, 400), paint);//原图

        paint.ImageFilter = filter;
        canvas.DrawBitmap(skBmp, new SKRect(410, 10, 800, 400), paint);//

        // 创建位移映射效果滤镜
        using (var bmpFilter = SKImageFilter.CreateImage(SKImage.FromBitmap(skBmp)))
        using (var displacementMapEffect = SKImageFilter.CreateDisplacementMapEffect(
            SKColorChannel.R, // 使用红色通道作为水平位移
            SKColorChannel.G, // 使用绿色通道作为垂直位移
            20.0f, // 缩放系数
            bmpFilter))
        {
            paint.ImageFilter = displacementMapEffect;
            canvas.DrawBitmap(skBmp, new SKRect(10, 410, 400, 800), paint);//

            using(var composeFilter=SKImageFilter.CreateCompose(filter, displacementMapEffect))
            {
                paint.ImageFilter = composeFilter;
                canvas.DrawBitmap(skBmp, new SKRect(410, 410, 800, 800), paint);//
            }
        }

        paint.ImageFilter = null;
        paint.Color = SKColors.Red;
        paint.TextSize = 24;
        paint.Typeface = SKTypeface.FromFamilyName("宋体");
        canvas.DrawText($"原图", 20, 200, paint);
        canvas.DrawText($"CreateDilate", 420, 200, paint);
        canvas.DrawText($"CreateDisplacementMapEffect", 20, 600, paint);
        canvas.DrawText($"CreateCompose", 420, 600, paint);
    }
}

示例膨胀、移位映射效果、组合滤镜。
ImageFilter

CreateDistantLitDiffuse 光照

public static SkiaSharp.SKImageFilter CreateDistantLitDiffuse (SkiaSharp.SKPoint3 direction, SkiaSharp.SKColor lightColor, float surfaceScale, float kd, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

SKImageFilter.CreateDistantLitDiffuse 用于创建一种特定光照效果的滤镜方法。它模拟的是远距离光源照射在图像上的漫反射效果。这种效果可以用来模拟三维物体在光照下的阴影和高光,从而增加图像的立体感和真实感。

** 作用与意义**

  1. 增加立体感:通过模拟光源的照射,图像可以呈现出类似于三维物体的效果,增加深度感。
  2. 增强真实感:光照效果可以使图像看起来更真实,因为现实世界中的物体都是受到光源影响的。
  3. 图像处理与特效:可以在图像处理、游戏开发、UI设计等领域应用,增加视觉效果。
var canvas = e.Surface.Canvas;
var info = e.Info;

canvas.Clear();
const string TEXT = "SkiaSharp绘图";
float z = 2F;
float surfaceScale = 1F;
float lightConstant = 0.3F;
// 应用远距离漫反射照明过滤器
using (SKPaint paint = new SKPaint())
{
    paint.IsAntialias = true;

    // Size text to 90% of canvas width
    paint.TextSize = 100;
    paint.Typeface = SKTypeface.FromFamilyName("微软雅黑");

    float textWidth = paint.MeasureText(TEXT);
    paint.TextSize *= 0.9f * info.Width / textWidth;

    // Find coordinates to center text
    SKRect textBounds = new SKRect();
    paint.MeasureText(TEXT, ref textBounds);

    float xText = info.Rect.MidX - textBounds.MidX;
    float yText = info.Rect.MidY - textBounds.MidY;

    // Create distant light image filter
    paint.ImageFilter = SKImageFilter.CreateDistantLitDiffuse(
                            new SKPoint3(2, 3, z),
                            SKColors.White,
                            surfaceScale,
                            lightConstant);

    canvas.DrawText(TEXT, xText, yText, paint);
}

CreateDistantLitDiffuse

CreateDistantLitSpecular 反射光照

public static SkiaSharp.SKImageFilter CreateDistantLitSpecular (SkiaSharp.SKPoint3 direction, SkiaSharp.SKColor lightColor, float surfaceScale, float ks, float shininess, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

创建应用远距离镜面反射照明的图像滤镜。
SKImageFilter.CreateDistantLitSpecular 用于创建一种特定光照效果的滤镜方法。它模拟的是远距离光源照射在图像上的镜面反射(高光)效果。这种效果可以用来模拟物体在光照下产生的镜面高光,从而增加图像的真实感和立体感。

作用与意义

  1. 增加真实感和立体感:通过模拟光源的镜面反射效果,图像可以看起来更加真实和立体。
  2. 突出物体表面的细节:镜面反射可以突出物体表面的光滑和凹凸不平。
  3. 图像处理与特效:可以在图像处理、游戏开发、UI设计等领域应用,增加视觉效果。
var canvas = e.Surface.Canvas;
var info = e.Info;

canvas.Clear();
const string TEXT = "SkiaSharp绘图";
float z = 2F;
float surfaceScale = 1F;
float shininess = 0.3F;
float ks = 0.8f; 
using (SKPaint paint = new SKPaint())
{
    paint.IsAntialias = true;

    // Size text to 90% of canvas width
    paint.TextSize = 100;
    paint.Typeface = SKTypeface.FromFamilyName("微软雅黑");

    float textWidth = paint.MeasureText(TEXT);
    paint.TextSize *= 0.9f * info.Width / textWidth;

    // Find coordinates to center text
    SKRect textBounds = new SKRect();
    paint.MeasureText(TEXT, ref textBounds);

    float xText = info.Rect.MidX - textBounds.MidX;
    float yText = info.Rect.MidY - textBounds.MidY;


    paint.ImageFilter = SKImageFilter.CreateDistantLitSpecular(
                            new SKPoint3(2, 3, z),
                            SKColors.White,
                            surfaceScale,
                            ks,
                            shininess);

    canvas.DrawText(TEXT, xText, yText, paint);
}

CreateDistantLitSpecular

CreateDropShadow阴影效果

public static SkiaSharp.SKImageFilter CreateDropShadow (float dx, float dy, float sigmaX, float sigmaY, SkiaSharp.SKColor color, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

创建阴影效果

var canvas = e.Surface.Canvas;
var info = e.Info;

canvas.Clear(SKColors.White);
const string TEXT = "SkiaSharp绘图";

using (SKPaint paint = new SKPaint())
{
    paint.IsAntialias = true;

    // Size text to 90% of canvas width
    paint.TextSize = 100;
    paint.Typeface = SKTypeface.FromFamilyName("微软雅黑");

    float textWidth = paint.MeasureText(TEXT);
    paint.TextSize *= 0.9f * info.Width / textWidth;

    // Find coordinates to center text
    SKRect textBounds = new SKRect();
    paint.MeasureText(TEXT, ref textBounds);

    float xText = info.Rect.MidX - textBounds.MidX;
    float yText = info.Rect.MidY - textBounds.MidY;

    // 创建阴影的颜色
    SKColor shadowColor = new SKColor(0, 0, 0, 128); // 半透明的黑色阴影

    paint.ImageFilter = SKImageFilter.CreateDropShadow(6, 10, 2, 2, shadowColor);

    canvas.DrawText(TEXT, xText, yText, paint);

    paint.ImageFilter = SKImageFilter.CreateDropShadowOnly(6, 10, 2, 2, shadowColor);

    canvas.DrawText(TEXT, xText, yText+100, paint);
}

阴影

CreateDropShadowOnly 只阴影效果

public static SkiaSharp.SKImageFilter CreateDropShadowOnly (float dx, float dy, float sigmaX, float sigmaY, SkiaSharp.SKColor color, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

只显示阴影效果,不显示前景。

CreateErode腐蚀效果

public static SkiaSharp.SKImageFilter CreateErode (int radiusX, int radiusY, SkiaSharp.SKImageFilter input = default, SkiaSharp.SKImageFilter.CropRect cropRect = default);

图像腐蚀
CreateErode

CreateMatrix变换矩阵

public static SkiaSharp.SKImageFilter CreateMatrix (SkiaSharp.SKMatrix matrix, SkiaSharp.SKFilterQuality quality, SkiaSharp.SKImageFilter input = default);

用于创建一个矩阵滤镜(Matrix Filter),该滤镜可以应用一个仿射变换(如旋转、缩放、平移)到图像上。这个方法允许开发者通过指定矩阵来改变图像的几何形状和位置。

var canvas = e.Surface.Canvas;
canvas.Clear(SKColors.White);

if (skBmp == null) skBmp = SKBitmap.Decode(@"Images\AIWoman.png");

var destRect = new SKRect(410, 10, 800, 400);
// 创建一个旋转45度的仿射变换矩阵,以图像中心为旋转中心
var matrix = SKMatrix.CreateRotationDegrees(45, (destRect.Left+ destRect.Right)/2F, (destRect.Top+ destRect.Bottom)/2F);
// 创建矩阵滤镜
using (var filter = SKImageFilter.CreateMatrix(matrix, SKFilterQuality.High))
using (var paint = new SKPaint())
{
    paint.FilterQuality = SKFilterQuality.High;
    canvas.DrawBitmap(skBmp, new SKRect(10, 10, 400, 400), paint);//原图

    paint.ImageFilter = filter;
    canvas.DrawBitmap(skBmp, new SKRect(410, 10, 800, 400), paint);//

    paint.ImageFilter = null;
    paint.Color = SKColors.Red;
    paint.TextSize = 24;
    paint.Typeface = SKTypeface.FromFamilyName("宋体");
    canvas.DrawText($"原图", 20, 200, paint);
    canvas.DrawText($"CreateMatrix", 420, 200, paint);
}

注意旋转中心为目标矩形的中心。
CreateMatrix

标签:canvas,05,SkiaSharp,float,ImageFilter,paint,滤镜,SKImageFilter
From: https://blog.csdn.net/TyroneKing/article/details/139755042

相关文章

  • Openwrt19.07及23.05的Vlan配置
    openwrt19.07因友switch功能,因此配置vlan较为简单,如下图:vlan1是lan,vlan2是wan,vlan3是IPTV,如下图:openwrt22以后的版本没有switch接口,因此步骤多了一些配置,思路大概是首先新建wlan基于lan的,这样可以保证在删除原有接口后也可以通过wifi访问到设备进行配置,配置完成后通过网口进入......
  • LeetCode 2055. Plates Between Candles
    原题链接在这里:https://leetcode.com/problems/plates-between-candles/description/题目:Thereisalongtablewithalineofplatesandcandlesarrangedontopofit.Youaregivena 0-indexed string s consistingofcharacters '*' and '|' only,......
  • 05梦断代码阅读笔记
    《梦断代码5》前台与人对话,后台与比特对话,言简意赅。创建还是复用?每个软件迟早都会到达这个岔路口。的确,我们不也是这样,复用固然便利简单,但能否完全适合自己现在的编程环境仍是一个未知数。而创建虽然费时费力但无疑是针对自己的状况,两者各有优劣。向往未来那种程序可由复用......
  • Flink - [05] 时间语义 & Watermark
    题记部分 一、时间语义Flink中的时间语义分为以下,(1)EventTime:事件创建的时间(2)IngestionTime:数据进入Flink的时间(3)ProcessingTime:执行操作算子的本地系统事件,与机器相关 哪种时间语义更重要?不同的时间语义有不同的应用场合,我们往往更关心事件时间(Event Time)某些......
  • 程序员修炼之道:从小工到专家阅读笔记05
    程序员所应该遵循的实用主义原则。 我的源码让猫给吃了:出现错误时,要诚实,不要推诿或者找借口。要提供各种可能的解决方案与后果并与他人沟通,而不是提供借口。 软件的熵:这是著名的破窗户原理。项目中一个小的、无人料理的问题可能带来后续编码时的懈怠,从而造成更大的问题。不......
  • LAETUS LLS570-05 激光扫描仪
    LLS570-05特点:高速扫描和检测:LLS570-05激光扫描仪具有快速的扫描速度和高精度的检测能力,能够快速而准确地进行产品表面的检测和分析。表面缺陷检测:该扫描仪可用于检测产品表面的各种缺陷,如裂纹、凹陷、污点、异物等,确保产品质量符合规定标准。条形码和字符识别:LLS570-05还......
  • 2023年05月二级
    青少年软件编程(图形化)等级考试试卷(二级)一、单选题(共25题,共50分)1.运行下列哪段程序,可以让狗狗走到木屋门口?( )A. B. C. D. 试题编号:20220501wyh04-001试题类型:单......
  • 0055-跳跃游戏
    55.跳跃游戏给你一个非负整数数组nums,你最初位于数组的第一个下标。数组中的每个元素代表你在该位置可以跳跃的最大长度。判断你是否能够到达最后一个下标,如果可以,返回true;否则,返回false。示例1:输入:nums=[2,3,1,1,4]输出:true解释:可以先跳1步,从下标0到达下标......
  • 冲刺05
    activity_parent.xml<?xmlversion="1.0"encoding="UTF-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layo......
  • 05-5.3.1_1 二叉树的先中后序遍历
    ......