Git Product home page Git Product logo

skiasharp.qrcode's Introduction

dotnet-build release codecov 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;

namespace SimpleGenerate
{
    class Program
    {
        static void Main(string[] args)
        {
            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
{
    class Program
    {
        static void Main(string[] args)
        {
            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>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SkiaSharp.QrCode" Version="0.4.1" />
    <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>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SkiaSharp.QrCode" Version="0.4.1" />
    <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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.