. NET Core using skiasharp text Avatar generation method tutorial (based on docker release), skiasharpdocker
I. Problem background
Currently. the NET Core library for image processing is not integrated by Microsoft. NET FrameWork. the Drawing class library performs simple image processing. NET Core, the next face is awesome, I can only look at Baidu + Google there is no solution, but there are also a lot of online information ,. there are still some open-source libraries for image processing under. NET Core. One of the ones I currently use is SkiaSharp, which is available on the Internet, next we will use this library to implement the small function of a text avatar. Let's take a look at the detailed introduction.
Ii. Simple Design Requirements
- Resolve the input name (Chinese and English)
- The background color of the image is changed randomly.
- Text to center (nonsense, not centered is not ugly)
Iii. Implementation
1. Resolve Name Information
Private String ResolveName (String imageText) {imageText. Replace ("? "," "). Replace (": "," "). Replace ("? ",""). Replace ("*",""). replace ("<",""). replace ("> ",""). replace (@"/",""). replace (@"\",""). replace (@ "| ",""). replace ("\" "," "); // remove information not supported by the path imageText = imageText. trim (''); // remove space information String temp2 = imageText. substring (0, 1); // determines whether to follow the English or Chinese rules based on the first data. if neither of them is true, the first two if (RegexLib. isChineseCharacter (temp2) {// UserName = UserName. trim (''); if (imageText. length> 2 & imageText. length <= 3) {imageText = imageText. substring (1, 2);} else if (imageText. length> = 3) {imageText = imageText. substring (imageText. length-2, 2) ;}} else if (RegexLib. isEnglishCharacter (temp2) {String [] temp1 = imageText. split (''); if (temp1.Length = 2) {imageText = (temp1 [0]. substring (0, 1) + temp1 [1]. substring (0, 1 )). toUpper ();} else {if (imageText. length> 2) {imageText = imageText. substring (0, 2 ). toUpper () ;}} else {if (imageText. length> 2) {imageText = imageText. substring (0, 2) ;}} imageName = imageText; return imageName ;}
2. generate images based on text
Public byte [] Create () {String name = imageName + ". jpg "; SKBitmap bmp = new SKBitmap (128,128); String str = imageName; using (SKCanvas canvas = new SKCanvas (bmp) {Random r = new Random (); int num = r. next (0, 9); canvas. drawColor (colors [num]); // colors is a set of image background colors. The code is not pasted here. A Random using (SKPaint sKPaint = new SKPaint () {sKPaint. color = SKColors. white; // font color sKPaint. textSize = 39; // font size sKPaint. isAntialias = true; // enable anti-aliasing sKPaint. typeface = SkiaSharp. SKTypeface. fromFamilyName ("", SKTypefaceStyle. bold); // font SKRect size = new SKRect (); sKPaint. measureText (str, ref size); // calculate the text width and height float temp = (128-size. size. width)/2; float temp1 = (128-size. size. height)/2; canvas. drawText (str, temp, temp1-size. top, sKPaint); // draw text} // save as image file using (SKImage img = SKImage. fromBitmap (bmp) {using (SKData p = img. encode (SKEncodedImageFormat. jpeg, 100) {return p. toArray (); // using (var stream = File. create (Path. combine (AppDomain. currentDomain. baseDirectory, "photoImage", name) // {// stream. write (p. toArray (), 0, p. toArray (). length); // return stream ;//}}}}}
What I get here is the byte [] array. If you want to convert it to stream or save it as a file, all methods are available. You can find all the methods, so I won't release them,
Iii. docker release pitfalls
Trap 1: IIS is acceptable. docker reports an error.
Because skiasharp uses different dependent libraries in windows and linux, if they are all deployed in The previous method, "The type initializer for 'skiasharp. SKImageInfo 'threw an exception "because skiasharp depends on libSkiaSharp in linux. so, and if the system does not contain libSkiaSharp. so, this error will be reported. At present, my simplest operation is to find the file and put it in the skiasharp directory, and then you can
Pit 2: Chinese font not recognized
Since docker does not have a Chinese font, SO is required to output Chinese characters. For example, the solution is to copy the font to docker.
The following provides my own Dockerfile. For how to use the specific Dockerfile, we can only use Baidu (ps: in fact, the real reason is that I am also a weak chicken --, for fear of misleading everyone), my environment is the environment of CentOS7.3
# Build an image FROM microsoft/dotnet: latest Based on 'Microsoft/dotnet: latest '# COPY all files in the publish folder of the project to the publish folder in the docker container. /publish/# env lang C.UTF-8 # env language C.UTF-8 # ENV LC_ALL C.UTF-8COPY MSYH. TTC/usr/share/fonts/dejavuCOPY MSYHL. TTC/usr/share/fonts/dejavuCOPY MSYHBD. TTC/usr/share/fonts/dejavu # Replace the software source with the domestic software source RUN mv/etc/apt/sources. list/etc/apt/sources. list. bak & \ echo "deb http://mirrors.163.com/debian/ Jessie main non-free contrib ">/etc/apt/sources. list & \ echo" deb http://mirrors.163.com/debian/ Jessie-proposed-updates main non-free contrib ">/etc/apt/sources. list & \ echo" deb-src http://mirrors.163.com/debian/ Jessie main non-free contrib ">/etc/apt/sources. list & \ echo" deb-src http://mirrors.163.com/debian/ Jessie-proposed-updates main non-free contrib ">/etc/apt/sources. list # RUN apt-get update & apt-get install-y libfontconfig1 & apt-get install-y fontconfigRUN apt-get update & apt-get install-y fontconfig # settings the working directory is the '/publish' folder, that is, the container starts the default folder WORKDIR/publish # sets the Docker container to EXPOSE port 5000 EXPOSE 5000 # Use 'dotnet WebApplication1.dll 'to run the application CMD ["dotnet", "WebApplication1.dll"]
The previous figure is successful:
Last DEMO address
Iv. Summary
Docker is a good thing, but I am too weak. It may take more time to learn. programmers may have to live and learn it!
PS: The above is just something I found out by myself. If you have a better implementation method, you are welcome to discuss it. Thank you for watching it.
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.