前言

 

  众所周知,Dotnet Core目前没有图形API,以前的System.Drawing程序集并没有包含在Dotnet Core 1.0环境中。不过在dotnet core labs项目里可以见到MS已经在移植这个项目,不过目前的版本只能在Windows上和NET541+或DNX环境中才可以使用。

  不过在dotnetConf的第两天有一个叫做SkiaSharp的开源项目被提及;它是Google开源的跨平台2D图形API,Skia的.NET封装;目前只能在Full Framework上运行,不过它以后会支持Core。

 现状

  据我了解,Dotnet Core目前没有可用的验证码组件可用,原因就是没有Core的图形接口。所以我的方案是通过开源的图形库来对dotnet core进行支持。

使用CImg开源库

  CImg 库是一个免费、开源的图像处理C++库,名称原意是 Cool Image,正如其名,CImg是一个非常优秀、功能强大、代码简洁、使用方便的C++ 图像处理库。它不仅非常适合科学家、研究生做科研时使用,也适合在工业应用工程开发中使用,更适合的是,对于有志于开发简洁、高效、功能强大的图像处理库的人而言,CImg的源码是不可多得的学习和参考资料。

  CImg 官网:http://cimg.sourceforge.net/

 

  可移植性:它完全兼容于操作系统如Windows, Unix, Linux, MacOS X, *BSD...,也完全兼容与编译器如 VC++, g++, icc...等,具有高度的可移植性。

 

  轻便性:CImg 非常轻便,整个库只用一个文件:cimg.h。任何C++应用程序只需要将该头文件包含进工程中即可使用该库的全部功能。它只定义了四了类(模板)和两个名称空间。该库只依赖与标准C++和STL,只在显示类部分依赖与操作系统的GDI,再也不依赖任何其他的外部库。

C++封装:

  我把绘图逻辑都放到了一个C++项目中,再用Core项目使用DllImport进行调用。

  而且想到跨平台在Win下我们使用Win32的DLL库进行编译,在Linux下使用g++直接对源代码进行链接编译; 

  下面是项目中最主要的CaptchaImage.cpp,Win32下它会被放到项目中

 1 #include "stdafx.h"
 2 #include "CaptchaImage.h"
 3 #include "CImg.h"
 4 using namespace cimg_library;
 5 
 6 Export_API void GCaptcha(char* file_o, char *captcha_text, int count, int width ,int height , int offset ,int quality, int isjpeg, int fontSize)
 7 {
 8     // Create captcha image
 9     //----------------------
10     // Write colored and distorted text
11     CImg<unsigned char> captcha(width, height, 1, 3, 0), color(3);
12     const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };
13 
14     char letter[2] = { 0 };
15     for (unsigned int k = 0; k<count; ++k) {
16         CImg<unsigned char> tmp;
17         *letter = captcha_text[k];
18         if (*letter) {
19             cimg_forX(color, i) color[i] = (unsigned char)(128 + (std::rand() % 127));
20             tmp.draw_text((int)(2 + 8 * cimg::rand()),
21                           (int)(12 * cimg::rand()), letter, red, 0, 1, fontSize).resize(-100, -100, 1, 3);
22 
23             const float sin_offset = (float)cimg::crand() * 3, sin_freq = (float)cimg::crand() / 7;
24             
25             cimg_forYC(captcha, y, v) captcha.get_shared_row(y, 0, v).shift((int)(4 * std::cos(y*sin_freq + sin_offset)));
26             
27             captcha.draw_image(count + offset * k, tmp);
28         }
29     }
30 
31     // Add geometric and random noise
32     CImg<unsigned char> copy = (+captcha).fill(0);
33     for (unsigned int l = 0; l<3; ++l) {
34         if (l) copy.blur(0.5f).normalize(0, 148);
35         for (unsigned int k = 0; k<10; ++k) {
36             cimg_forX(color, i) color[i] = (unsigned char)(128 + cimg::rand() * 127);
37             if (cimg::rand() < 0.5f) {
38                 copy.draw_circle((int)(cimg::rand()*captcha.width()),
39                                      (int)(cimg::rand()*captcha.height()),
40                                      (int)(cimg::rand() * 30),
41                                      color.data(), 0.6f, ~0U);
42             }
43             else {
44                 copy.draw_line((int)(cimg::rand()*captcha.width()),
45                                     (int)(cimg::rand()*captcha.height()),
46                                     (int)(cimg::rand()*captcha.width()),
47                                     (int)(cimg::rand()*captcha.height()),
48                                     color.data(), 0.6f);
49             }
50         }
51     }
52     captcha |= copy;
53     captcha.noise(10, 2);
54 
55     captcha = (+captcha).fill(255) - captcha;
56 
57     // Write output image and captcha text
58     //-------------------------------------
59     //std::printf("%s\n",captcha_text);
60 
61     if (isjpeg) {
62         captcha.save_jpeg(file_o, quality);
63     }
64     else {
65         captcha.save(file_o);
66     }
67 
68 }
CaptchaImage.cpp

相关文章:

  • 2022-01-30
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-08-05
  • 2021-09-03
猜你喜欢
  • 2021-07-08
  • 2022-01-01
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2021-08-06
相关资源
相似解决方案