Skip to content

Commit

Permalink
Big bang.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaFrock committed Apr 8, 2021
1 parent 69f1832 commit 20e2292
Show file tree
Hide file tree
Showing 27 changed files with 833 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*.user
*.userosscache
*.sln.docstates
[Ff]onts/

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# CaptchaN

[DotNetUrl]: https://dotnet.microsoft.com/download
[ImageSharpUrl]: https://github.com/SixLabors/ImageSharp
[CaptchaN-SvgUrl]: https://img.shields.io/nuget/v/CaptchaN.svg
[CaptchaN-NugetUrl]: https://www.nuget.org/packages/CaptchaN
[CaptchaN-DI-Microsoft-SvgUrl]: https://img.shields.io/nuget/v/CaptchaN.DI.Microsoft.svg
[CaptchaN-DI-Microsoft-NugetUrl]: https://www.nuget.org/packages/CaptchaN.DI.Microsoft

Generate captcha images based on [ImageSharp][ImageSharpUrl] and .NET.
>基于[ImageSharp][ImageSharpUrl]项目和.NET,生成图形验证码。
[![.NET 5](https://img.shields.io/badge/.NET-5.x-brightgreen)][DotNetUrl]
![License](https://img.shields.io/badge/License-MIT-green)

## Packages |Nuget包

| Name | Description | NugetPackage |
| :-------------------: | :-----------------------------: | :--------------------------------------------------------------------------------------: |
| CaptchaN | Core module | [![CaptchaN][CaptchaN-SvgUrl]][CaptchaN-NugetUrl] |
| CaptchaN.DI.Microsoft | DI module for default container | [![CaptchaN.DI.Microsoft][CaptchaN-DI-Microsoft-SvgUrl]][CaptchaN-DI-Microsoft-NugetUrl] |

## QuickStart |快速入门

The following is main codes for basic usage.

``` C#
// Load or install fonts(*.ttf) which are provided by yourself
IFontRandomerFactory fontRandomerFactory = new ...;
IFontRandomer fontRandomer = fontRandomerFactory.CreateFontRandomer();
// Init color randomer
IColorRandomer colorRandomer = new ...;
IPainter painter = new Painter(fontRandomer, colorRandomer);
// Init the content settings for images
PainterOption painterOption = new(){...};
// Optional. Generate random text for solid length
ICodeTextGenerator codeTextGenerator = new ...;
string codeText = codeTextGenerator.Generate(4);
// Generate an image
await painter.GenerateImageAsync(codeText, painterOption);
```

Please see [Wiki](https://github.com/LeaFrock/CaptchaN/wiki) and [Samples](https://github.com/LeaFrock/CaptchaN/tree/main/Samples) for details.

## Contribution |贡献

Issues and pull requests are welcomed if you have any questions!
>如果您有任何疑问,欢迎提交Issue和PR!
11 changes: 11 additions & 0 deletions Samples/CaptchaN.WebApi/CaptchaN.WebApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CaptchaN.DI.Microsoft" Version="0.1.1" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions Samples/CaptchaN.WebApi/CaptchaN.WebApi.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaptchaN.WebApi", "CaptchaN.WebApi.csproj", "{F95C056B-72E2-4865-9811-0EAC5B2F5523}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F95C056B-72E2-4865-9811-0EAC5B2F5523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F95C056B-72E2-4865-9811-0EAC5B2F5523}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F95C056B-72E2-4865-9811-0EAC5B2F5523}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F95C056B-72E2-4865-9811-0EAC5B2F5523}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B9424246-0196-4633-82DF-54A8233C3C16}
EndGlobalSection
EndGlobal
34 changes: 34 additions & 0 deletions Samples/CaptchaN.WebApi/Controllers/CaptchaController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Threading.Tasks;
using CaptchaN.Abstractions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace CaptchaN.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class CaptchaController : ControllerBase
{
private readonly IPainter _painter;
private readonly ICodeTextGenerator _codeTextGenerator;
private readonly PainterOption _painterOption;

public CaptchaController(
IOptions<PainterOption> painterOpt,
ICodeTextGenerator codeTextGenerator,
IPainter painter)
{
_painterOption = painterOpt.Value;
_codeTextGenerator = codeTextGenerator;
_painter = painter;
}

[HttpGet]
public async Task<IActionResult> Get()
{
string codeText = _codeTextGenerator.Generate(4);
var image = await _painter.GenerateImageAsync(codeText, _painterOption);
return File(image, "image/jpeg; charset=UTF-8");
}
}
}
26 changes: 26 additions & 0 deletions Samples/CaptchaN.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace CaptchaN.WebApi
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
31 changes: 31 additions & 0 deletions Samples/CaptchaN.WebApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:13693",
"sslPort": 44319
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "captcha",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CaptchaN.WebApi": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "captcha",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
55 changes: 55 additions & 0 deletions Samples/CaptchaN.WebApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.IO;
using CaptchaN.Abstractions;
using CaptchaN.Factories;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace CaptchaN.WebApi
{
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
HostEnvironment = env;
}

public IConfiguration Configuration { get; }

protected IWebHostEnvironment HostEnvironment { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();

services.Configure<PainterOption>(Configuration.GetSection("CaptchaN"));
services.AddCaptchaN()
.UseFontRandomer(new DirectoryFontRandomerFactory() { FontDir = new(Path.Combine(HostEnvironment.ContentRootPath, "Fonts")) });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();
app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
19 changes: 19 additions & 0 deletions Samples/CaptchaN.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"CaptchaN": {
"Width": 200,
"Height": 50,
"PointCount": 12,
"LineCount": 3,
"BubbleCount": 0,
"InterferCharCount": 4,
"UseBlackWhiteOnly": false
}
}
27 changes: 27 additions & 0 deletions Src/CaptchaN.DI.Microsoft/CaptchaN.DI.Microsoft.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<PackageId>CaptchaN.DI.Microsoft</PackageId>
<Title>CaptchaN.DI.Microsoft</Title>
<Authors>LeaFrock</Authors>
<Version>0.1.1</Version>
<Product>CaptchaN.DI.Microsoft</Product>
<Description>CaptchaN DI for .NET default container.</Description>
<Copyright>Copyright © LeaFrock 2021</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>captcha;csharp</PackageTags>
<PackageProjectUrl>https://github.com/LeaFrock/CaptchaN</PackageProjectUrl>
<RepositoryUrl>https://github.com/LeaFrock/CaptchaN.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Remove FontRandomer.</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CaptchaN" Version="0.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 20e2292

Please sign in to comment.