Skip to content

Latest commit

 

History

History
158 lines (108 loc) · 4.34 KB

README.md

File metadata and controls

158 lines (108 loc) · 4.34 KB

lint build debug

License: MIT NuGet

Skia.QrCode

Qr Code generator with Skia.Sharp.

Install

.NET CLI

$ dotnet add package SkiaSharp.QrCode

Package Manager

PM> Install-Package SkiaSharp.QrCode

Motivation

There are many System.Drawing samples to generate QRCode, and there are a lot of cases I want avoid System.Drawing for GDI+ issue. However, you may require many conding to generate QRCode using ZXing.Net or ImageSharp or Core.Compat.System.Drawing.

I just want to create QR in much simpler way.

Why Skia?

Performance and size and .NET Core support status.

.NET Core Image Processing

Sample Code

Here's minimum sample to generate specific qrcode via args.

using SkiaSharp;
using SkiaSharp.QrCode.Image;
using System;
using System.IO;

var content = "testtesttest";
using var output = new FileStream(@"output/hoge.png", FileMode.OpenOrCreate);

// generate QRCode
var qrCode = new QrCode(content, new Vector2Slim(256, 256), SKEncodedImageFormat.Png);
// output to file
qrCode.GenerateImage(output);

If you want specify detail, you can generate manually.

using SkiaQrCode;
using SkiaSharp;
using System;
using System.IO;

namespace SkiaQrCodeSampleConsole;

var content = "testtesttest";
using var generator = new QRCodeGenerator();

// Generate QrCode
var qr = generator.CreateQrCode(content, ECCLevel.L);

// Render to canvas
var info = new SKImageInfo(512, 512);
using var surface = SKSurface.Create(info);
var canvas = surface.Canvas;
canvas.Render(qr, info.Width, info.Height);

// Output to Stream -> File
using var image = surface.Snapshot();
using var data = image.Encode(SKEncodedImageFormat.Png, 100);
using var stream = File.OpenWrite(@"output/hoge.png");
data.SaveTo(stream);

TIPS

Linux support

You have 2 choice to run on Linux. If you don't need font operation, use SkiaSharp.NativeAssets.Linux.NoDependencies.

  1. Use SkiaSharp.NativeAssets.Linux package. In this case, you need to install libfontconfig1 via apt or others.
  2. Use SkiaSharp.NativeAssets.Linux.NoDependencies 2.80.2 or above. In this case, you don't need libfontconfig1.

SkiaSharp.NativeAssets.Linux.NoDependencies still can draw text, however can't search font cased on character or other fonts.

Detail: mono/SkiaSharp#964 (comment)

SkiaSharp.NativeAssets.Linux sample

sudo apt update && apt install -y libfontconfig1
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SkiaSharp.QrCode" Version="0.5.0" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.80.2" />
  </ItemGroup>
</Project>

SkiaSharp.NativeAssets.Linux.NoDependencies sample

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SkiaSharp.QrCode" Version="0.5.0" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.80.2" />
  </ItemGroup>
</Project>

Docker Build & Run

Test Build lib.

docker build -t skiasharp.qrcode .

Test Run on linux.

cd samples/LinuxRunSamples
docker-compose up

License

MIT

Thanks

aloisdeniel/Xam.Forms.QRCode : Qr Sample with Skia codebude/QRCoder : all QRCode generation algorithms