首页 > 系统相关 >SkiaSharp画的验证码在Linux下无法正常显示

SkiaSharp画的验证码在Linux下无法正常显示

时间:2024-07-22 19:07:54浏览次数:9  
标签:drawStyle using SkiaSharp random 验证码 Next Linux new height

  1. SkiaSharp是Google的Skia图形库的.NET封装版,可用于跨移动、服务器和桌面平台绘制 2D 图形。SkiaSharp 可与 OpenGL 一起用于硬件加速渲染。SkiaSharp 最初由 Mono 开发,但现在由 Microsoft 维护,并根据MIT License提供。

  2. 使用SkiaSharp生成验证码图片示例代码

using SkiaSharp;
using System.Text;

namespace TestApi;

public static class VerificationCodeHelper
{
    public static string GetRandomCode(int length)
    {
        StringBuilder code = new();
        string text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        Random random = new();
        for (int i = 0; i < length; i++)
        {
            code.Append(text[random.Next(0, text.Length)]);
        }
        return code.ToString();
    }

    public static byte[] GetVerificationCode(string text)
    {
        int width = 128;
        int height = 45;
        Random random = new();

        using SKBitmap image = new(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
        using SKCanvas canvas = new(image);
        canvas.DrawColor(SKColors.White);

        // 画干扰线
        // 干扰线占图片面积的比例
        decimal distractorLineRate = 0.0005M;
        for (int i = 0; i < (width * height * distractorLineRate); i++)
        {
            using SKPaint _drawStyle = new();
            _drawStyle.Color = new(Convert.ToUInt32(random.Next(int.MaxValue)));
            canvas.DrawLine(random.Next(0, width), random.Next(0, height), random.Next(0, width), random.Next(0, height), _drawStyle);
        }

        // 画文本
        using SKPaint drawStyle = new();
        drawStyle.Color = SKColors.Red;
        drawStyle.TextSize = height;
        drawStyle.IsAntialias = true; // 抗锯齿

        // 文本居中显示
        float textWidth = drawStyle.MeasureText(text);
        float x = (width - textWidth) / 2;
        float y = (height + drawStyle.TextSize) / 2; // 调整Y轴位置以居中

        // 绘制每个字符
        foreach (char c in text)
        {
            string character = c.ToString();
            float charWidth = drawStyle.MeasureText(character);
            float charRotation = random.Next(-15, 15); // 随机旋转角度

            canvas.Save();
            canvas.RotateDegrees(charRotation, x + charWidth / 2, y - drawStyle.TextSize / 2);
            canvas.DrawText(character, x, y, drawStyle);
            canvas.Restore();

            x += charWidth;
        }

        // 绘制噪点
        for (int i = 0; i < (width * height * 0.6); i++)
        {
            image.SetPixel(random.Next(0, width), random.Next(0, height), new SKColor(Convert.ToUInt32(random.Next(int.MaxValue))));
        }

        using var img = SKImage.FromBitmap(image);
        using SKData p = img.Encode(SKEncodedImageFormat.Png, 100);
        return p.ToArray();
    }
}
  1. 调用示例代码
app.MapGet("/testVerificationCode", (HttpContext httpContext) =>
{
    var randomText = VerificationCodeHelper.GetRandomCode(4);
    return Results.File(VerificationCodeHelper.GetVerificationCode(randomText), "image/png");
})
.WithName("TestVerificationCode")
.WithOpenApi();
  1. Windows下运行效果展示
    image

  2. Linux下运行效果展示
    image

检查日志得到,缺少了libSkiaSharp或任意它的分装库:
image

  1. 换源并安装libgdiplus(使用的镜像是官方镜像,基于debian12):
#备份旧源文件
cp -rf /etc/apt/sources.list.d /etc/apt/sources.list.d.bak
#删除旧源文件
rm -rf /etc/apt/sources.list.d/*
#创建源文件指向华为云源
cat <<'EOF'> /etc/apt/sources.list.d/huawei-cloud.list
deb https://mirrors.huaweicloud.com/debian/ bookworm main contrib non-free
deb https://mirrors.huaweicloud.com/debian/ bookworm-updates main contrib non-free
deb https://mirrors.huaweicloud.com/debian/ bookworm-backports main contrib non-free
deb https://mirrors.huaweicloud.com/debian-security/ bookworm-security main contrib non-free
EOF

cat <<'EOF'> fix_skiasharp_broke.sh
#!/bin/bash

# 更新包依赖树,确保最新
apt-get update -y

# 安装 libgdiplus 库,这是 System.Drawing.Common 在 Linux 上的依赖
apt-get install -y libgdiplus

# 清理 apt 缓存,减少镜像大小
apt-get clean

# 检查是否存在 /usr/lib/gdiplus.dll 文件,如果不存在,则创建一个指向 libgdiplus.so 的符号链接
# 这是为了兼容某些依赖 gdiplus.dll 的应用
if [ ! -f /usr/lib/gdiplus.dll ]; then
    ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
fi
EOF

sh fix_skiasharp_broke.sh
  1. 重启容器并检查运行效果
    image

  2. End

标签:drawStyle,using,SkiaSharp,random,验证码,Next,Linux,new,height
From: https://www.cnblogs.com/Ar4te-blog/p/18316693

相关文章

  • Linux-shell脚本链接Oracle执行查询
    #!/bin/bash#zkm2024-07-22Linux脚本链接Oracle数据库,用户判断sftp、ftp生成文件目录是否为空,若为空则短信表插入一条数据,用于短信提醒。#注意:#1、当前服务器需要安装Oracle客户端#2、sqlplus验证连接Oracle正常#当前时间date_time=`date+"%Y%m%d%H%M"`#输出时间echo"开......
  • linux最大线程数限制及打开最大文件数
    1.root用户下执行ulimit-a然后查看maxuserprocesses这个值通常是系统最大线程数的一半maxuserprocesses:当前用户同时打开的进程(包括线程)的最大个数为  2.普通用户下ulimit-a出现的maxuserprocesses的值默认是/etc/security/limits.d/20-nproc.conf文件中......
  • Linux--进程绑定NUMA节点或CPU核心
    对于CPU和NUMA架构的介绍本文不再做叙述,感兴趣的可自行查看:Linux--CPU简述,Linux--内存管理浅谈。 1、进程绑定NUMA节点或cpu核心的意义NUMA架构将内存和cpu分散在不同的NUMA节点上,每个节点都有自己的本地内存和cpu处理器,将进程绑定到特定的NUMA节点或cpu上,可以让进程直接......
  • linux内核 ip_unprivileged_port_start
    ip_local_port_range定义了TCP和UDP用于选择本地端口的范围。这个范围由两个整数表示,第一个数字是范围的起始端口号,第二个数字是范围的结束端口号。通常建议这两个数字的奇偶性不同(一个为偶数,一个为奇数),这样可以在一定程度上提高端口分配的随机性和安全性。这两个数字必须大于或......
  • mariadb安装在服务器(Linux)
    在大多数Linux发行版上,您可以使用包管理器来安装MariaDB。以下是几种常见Linux发行版的安装命令:对于基于Debian的系统(如Ubuntu):sudoapt-getupdatesudoapt-getinstallmariadb-serversudosystemctlstartmariadbsudosystemctlenablemariadb对于基于RPM的系统(如Cen......
  • 【Kernel】关于Linux内核参数 net.ipv4.ip_local_reserved_ports
    网络端口号是如何分配的?除了给常用服务保留的Well-knownPortnumbers之外,给客户端的端口号通常是动态分配的,称为ephemeralport(临时端口),在Linux系统上临时端口号的取值范围是通过这个内核参数定义的:net.ipv4.ip_local_port_range(/proc/sys/net/ipv4/ip_local_port_range),......
  • 在linux上操作实现git github vscode的联动工作流(待更新)
    目录一、介绍与要求二、git和github联动2.1git与github的连接2.2git的操作三、vscode与git的方便操作(待更新)总结一、介绍与要求介绍:我刚刚改到linux系统(ubuntu22.04)上进行学习和工作,深感到linux的方便快捷,于是想在此记录以下git和github的学习过程,也权当备忘录。......
  • centos stream9(linux): 编译安装python 3.12.4
    一,官方下载地址:https://www.python.org/downloads/点击进入具体版本的下载页面,我们选择稳定版本,地址:https://www.python.org/downloads/release/python-3124/如图:复制得到下载链接:https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz 二,下载:从命令行下载:......
  • Linux下MySQL的安装部署
    文章目录前言一、MySQL是什么?二、MySQL安装部署(两种)1.手动安装MySQL(1)下载MySQL安装包(2)解压压缩包至opt文件夹下(3)重命名mysql-8.0.33-linux-glibc2.12-x86_64文件夹(4)创建用户组和密码(5)为mysql用户授权(6)创建数据文件的存储位置(7)初始化MySQL数据库(8)编辑MySQL的配置文件(9)......
  • Linux 部署DVWA靶场
    Linux部署DVWA靶场DVWA是一款开源的网络安全漏洞实践平台,专为安全学习者设计。它涵盖了XXS、SQL注入、文件上传、文件包含、CSRF和暴力破解等多种安全漏洞环境,每个漏洞都有从简单到复杂的多个难度级别。环境部署安装httpd及其相关的组件yuminstall-yhttpdhttpd-devel......