前言

  众所周知,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下它会被放到项目中

 #include "stdafx.h"
#include "CaptchaImage.h"
#include "CImg.h"
using namespace cimg_library; Export_API void GCaptcha(char* file_o, char *captcha_text, int count, int width ,int height , int offset ,int quality, int isjpeg, int fontSize)
{
// Create captcha image
//----------------------
// Write colored and distorted text
CImg<unsigned char> captcha(width, height, , , ), color();
const unsigned char red[] = { ,, }, green[] = { ,, }, blue[] = { ,, }; char letter[] = { };
for (unsigned int k = ; k<count; ++k) {
CImg<unsigned char> tmp;
*letter = captcha_text[k];
if (*letter) {
cimg_forX(color, i) color[i] = (unsigned char)( + (std::rand() % ));
tmp.draw_text((int)( + * cimg::rand()),
(int)( * cimg::rand()), letter, red, , , fontSize).resize(-, -, , ); const float sin_offset = (float)cimg::crand() * , sin_freq = (float)cimg::crand() / ; cimg_forYC(captcha, y, v) captcha.get_shared_row(y, , v).shift((int)( * std::cos(y*sin_freq + sin_offset))); captcha.draw_image(count + offset * k, tmp);
}
} // Add geometric and random noise
CImg<unsigned char> copy = (+captcha).fill();
for (unsigned int l = ; l<; ++l) {
if (l) copy.blur(0.5f).normalize(, );
for (unsigned int k = ; k<; ++k) {
cimg_forX(color, i) color[i] = (unsigned char)( + cimg::rand() * );
if (cimg::rand() < 0.5f) {
copy.draw_circle((int)(cimg::rand()*captcha.width()),
(int)(cimg::rand()*captcha.height()),
(int)(cimg::rand() * ),
color.data(), 0.6f, ~0U);
}
else {
copy.draw_line((int)(cimg::rand()*captcha.width()),
(int)(cimg::rand()*captcha.height()),
(int)(cimg::rand()*captcha.width()),
(int)(cimg::rand()*captcha.height()),
color.data(), 0.6f);
}
}
}
captcha |= copy;
captcha.noise(, ); captcha = (+captcha).fill() - captcha; // Write output image and captcha text
//-------------------------------------
//std::printf("%s\n",captcha_text); if (isjpeg) {
captcha.save_jpeg(file_o, quality);
}
else {
captcha.save(file_o);
} }

CaptchaImage.cpp

  头文件:

 #ifdef PROJECT_EXPORTS
#define Export_API extern "C" __declspec(dllexport)
#else
#define Export_API extern "C"
#endif Export_API void GCaptcha(char* file_o, char *captcha_text, int count, int width, int height, int offset, int quality, int isjpeg, int fontSize);

CaptchaImage.h

  这里为了跨平台编译我将stdafx.h文件进行了修改如下:

 #pragma once
#ifdef _MSC_VER //I'm in VS
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
#endif

stdafx.h

Dotnet Core平台封装:

  public class CaptchaImageCore
{ [DllImport("libcaptchaimage.so", EntryPoint = "GCaptcha")]
public static extern void libCaptcha(string file_o, string captcha_text, int count, int width, int height, int offset, int quality, int isjpeg, int fontSize); [DllImport("libcaptchaimage.dll", EntryPoint = "GCaptcha")]
public static extern void GCaptcha(string file_o, string captcha_text, int count, int width, int height, int offset, int quality, int isjpeg, int fontSize); public string Text { set; get; } public int ImageWidth { set; get; }
public int ImageHeight { set; get; } public int ImageOffset { set; get; } public int ImageQuality { set; get; } public int FontSize { set; get; } public CaptchaImageCore(int w,int h,int fontSize)
{
this.ImageWidth = w;
this.ImageHeight = h;
this.FontSize = fontSize;
this.ImageOffset = ;
this.ImageQuality = ; } public MemoryStream GetStream(string fileName)
{
this.Save(fileName);
MemoryStream ms = new MemoryStream();
using (var fileStream = new FileStream(fileName, FileMode.Open))
{
fileStream.CopyTo(ms);
}
try
{
File.Delete(fileName);
}
catch { }
return ms; } public void Save(string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new NullReferenceException("file name is null");
if (string.IsNullOrEmpty(this.Text))
this.Text = this.GenerateCheckCode(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
GCaptcha(fileName, this.Text, this.Text.Length, this.ImageWidth, this.ImageHeight,
this.ImageOffset, this.ImageQuality, , this.FontSize);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
libCaptcha(fileName, this.Text, this.Text.Length, this.ImageWidth, this.ImageHeight,
this.ImageOffset, this.ImageQuality, , this.FontSize);
}
} public string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty; System.Random random = new Random(); for (int i = ; i < ; i++)
{
number = random.Next(); if (number % == )
//生成'0'-'9'字符
code = (char)('' + (char)(number % ));
else
//生成'A'-'Z'字符
code = (char)('A' + (char)(number % )); checkCode += code.ToString();
} return checkCode;
//两个字符相加等于=asicc码加
} }

LibCaptchaImageWarp.cs

编译:

Win32编译就不用说了,直接在VS2015里编译就好,但是一定要注意的是,要编译为X64平台的目标代码,因为我们的Dotnet Core只支持x64平台;

主要说下Linux编译,目前我只在Ubuntu 14.04进行了编译测试,编译时CImg依赖也X11,所以要在编译环境中安装X11开发库,当然Ubuntu也需要64位;

sudo apt-get install libx11-dev

接下下是编译:

把那个Win32项目Copy到Linux中,然后Bash到目录下执行:

g++ CaptchaImage.cpp -fPIC -shared -o libcaptchaimage.so

然后当你发布程序时一定要将libcaptachaImageWarp.dll 和 Win32 Dll 或 libcaptchaimage.so 文件一起放到程序执行目录。

最后:

看看效果吧:

验证码源码:https://github.com/maxzhang1985/YOYOFx/tree/master/Native

Demo:https://github.com/maxzhang1985/YOYOFx/tree/master/CoreHost

QQ群:214741894

Demo和源码在:https://github.com/maxzhang1985/YOYOFx

YOYOFx是一个基于Core和Owin的框架,项目没有依赖微软的MVC框架,支持在.net 4.5和Mono上直接SelfHost或使用Tinyfox跨平台运行, 也支持在Dotnet Core 1.0 RC2 实现跨平台运行; 框架刚刚写出来还没有文档,请大家见谅。

欢迎大家Star和Fork

最新文章

  1. Outlook~设置
  2. PHP面试题4
  3. 725C
  4. LeetCode OJ 274. H-Index
  5. 改变tableView索引颜色
  6. Knockout.Js官网学习(event绑定、submit绑定)
  7. js 字符串扩展
  8. zoj1025 Wooden Sticks
  9. Django 缓存模块 page_cache 源码阅读
  10. iOS中 图文混排/自定义图文混排 作者:韩俊强
  11. Django小技巧——使用package管理app
  12. 别老扯什么hadoop,你的数据根本不够大
  13. PTA地下迷宫探索
  14. Django后端项目---- Rest Framework(2)
  15. Python dict 将元祖转成字典
  16. 3. group_concat与oracle中wm_concat用法一样
  17. Android内存泄漏原因
  18. 遭遇java.lang.NoClassDefFoundError: org/apache/tomcat/PeriodicEventListener
  19. ASP.NET#命名空间&quot;System.Data&quot;中不存在类型或命名空间名称&quot;Linq&quot;(是否缺少程序集引用?)
  20. MYSQL常用命令(转载)

热门文章

  1. 我的MYSQL学习心得(一) 简单语法
  2. 前端框架 EasyUI (1)熟悉一下EasyUI
  3. 探究@property申明对象属性时copy与strong的区别
  4. 谈谈JS中的函数节流
  5. Linux常用命令操作
  6. Oracle安装部署,版本升级,应用补丁快速参考
  7. 卸载oracle之后,如何清除注册表
  8. notepad++设置默认打开txt文件失效的解决方法
  9. BPM配置故事之案例13-触发消息通知
  10. android手机登录时遇到“QQ安全登录发现病毒”解决