Code Monkey home page Code Monkey logo

skiasharp.qrcode's Introduction

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

skiasharp.qrcode's People

Contributors

dependabot[bot] avatar feiyun0112 avatar github-actions[bot] avatar guitarrapc avatar ping avatar roger-castaldo avatar yangzhongke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

skiasharp.qrcode's Issues

I need to remove transparency from a PNG and replace White background

I need to remove transparency from a PNG and replace with white background. the commented out lines are things I have tried. it just keeps coming out transparent.

Imports SkiaSharp
Imports SkiaSharp.QrCode.Image
Using output = New FileStream(Application.StartupPath & "geo.png", FileMode.OpenOrCreate)
Dim Code = New QrCode("test qr code", New Vector2Slim(256, 256), SKEncodedImageFormat.Png)
Code.GenerateImage(output)
End Using

Release Build Error

The debug mode is normal

But Release Build Error

mono-aot-cross.exe 中发生了未经处理的 win32 异常。

XABBA7028: System.IO.FileNotFoundException: 未能找到文件“F:\Data\xxx\obj\Release\net7.0-android\android-arm\aot\SkiaSharp.dll.so”。

Windows App Certification Kit fails

I am using this nuget package in my Xamarin Forms UWP project. When I run my project through the Microsoft App Certification Kit, I get the following error:

Supported APIs

  • Error Found: The supported APIs test detected the following errors:

    - API __CxxFrameHandler4 in vcruntime140_1_app.dll is not supported for this application type. libSkiaSharp.dll calls this API.
    - API __CxxFrameHandler4 in vcruntime140_1_app.dll is not supported for this application type. libGLESv2.dll calls this API.
    
  • Impact if not fixed: Using an API that is not part of the Windows SDK for Microsoft Store apps violates the Microsoft Store certification requirements.

  • **How to fix:**Review the error messages to identify the API that is not part of the Windows SDK for Microsoft Store apps. Please note, apps that are built in a debug configuration or without .NET Native enabled (where applicable) can fail this test as these environments may pull in unsupported APIs. Retest your app in a release configuration, and with .NET Native enabled if applicable. See the link below for more information:
    Alternatives to Windows APIs in Microsoft Store apps.

Cake

Do you think it would be useful to add cake next?
(To build the nuget package and run tests in the future, maybe check the code, etc)

This is cake in case you dont know what it is:
https://cakebuild.net/

update SkiaSharp ?

"The SkiaSharp version needs to be upgraded to a version >= 2.0.0 and less than 2.88.6 as there are critical vulnerabilities in versions within this range."

The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.

If i build this for linux, i get the error:
The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.
Exception:

 ---> System.DllNotFoundException: Unable to load DLL 'libSkiaSharp' or one of its dependencies: The specified module could not be found. (0x8007007E)
   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SKImageInfo..cctor()
   --- End of inner exception stack trace ---
   at SkiaSharp.SKImageInfo..ctor(Int32 width, Int32 height)
   at AT.QRCoder.DiscordBot.Commands.QrCoder.GenerateQr(String content, String darkColor, String lightColor, String iconUrl) in C:\Users\Name\Desktop\ProjName\src\Proj.ProjName\Commands\File.cs:line 30```

at this line:
 ` var info = new SKImageInfo(660, 660);`

Do you know what causes that and how to fix that? 

Circular QrCodes

Hello,

How could we change the Square dots with Circular ones ?

Add new method to return byte[] from QrCode instance.

Presudo code.

var content = "foobar";
var qrCode = new QrCode(content, new Vector2Slim(256, 256), SKEncodedImageFormat.Png);
byte[] bytes = qrCode.GetImage(ECCLevel.L); // <- new. return byte[]

// treat byte as you like. .AsSpan() or any.

How change?

Current API is following.

using var stream = new MemoryStream();
var qr = new QrCode("WIFI:T:WPA;S:mynetwork;P:mypass;;", new Vector2Slim(512, 512), SKEncodedImageFormat.Png);
qr.GenerateImage(stream);
Bitmap.FromStream(stream).Dump(); // Show Bitmap on LinqPad

image

This become...

var qrCode = new QrCode("WIFI:T:WPA;S:mynetwork;P:mypass;;", new Vector2Slim(256, 256), SKEncodedImageFormat.Png);
Bitmap.FromStream(new MemoryStream(qrCode.GetImage(ECCLevel.L))).Dump();

.appveyor

I dont know if this is an issue but im gonna report it anyway.

Your nuget api key is exposed in the appveyor.yml file.

not render qr code bigger than 160X160 completely!

Hi
I want to generate QRcode in blazor wasm in canvas but when I use this code

<h3>QrSkia</h3>
<SKCanvasView OnPaintSurface="OnPaintSurfaceQr" />
@code {
 void OnPaintSurfaceQr(SKPaintSurfaceEventArgs e)
    {

        var canvas = e.Surface.Canvas;
        canvas.Clear(SKColors.Red);
        var content = "https://github.com/guitarrapc/SkiaSharp.QrCod";
        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);
}
}
}
}

image

it just renders under 160X600 corect and fulfill canvas.
if i use var info = new SKImageInfo(160, 160);
image

it renders QRcode completely.

Color

Hey, is it possible to change the color of the qr code? (Not the color of the background)

Icons have black borders in linux

Linux: image

Windows:
image

code:
var icon = new IconData
{
Icon = SKBitmap.Decode(byteIcon),
IconSizePercent = 10,
};

        using var generator = new QRCodeGenerator();
        var qr = generator.CreateQrCode(url, ECCLevel.L);
        var info = new SKImageInfo(512, 512);
        using var surface = SKSurface.Create(info);
        var canvas = surface.Canvas;
        canvas.Render(qr, info.Width, info.Height, SKColor.Parse("ffffff"), SKColor.Parse("000000"), icon);
        using var image = surface.Snapshot();
        using var data = image.Encode(SKEncodedImageFormat.Png, 100);

Library used to work on mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-1803 but not on mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1803

While also including SkiaSharp.NativeAssets.NanoServer 1.68.1.1 into my .NET Core 3.1 project, and building and running on mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1803, I now get:

The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.
Unable to load DLL 'libSkiaSharp' or one of its dependencies: The specified module could not be found. (0x8007007E)

However this all did work with the same versions of the libraries when building with .NET Core 2.2 on mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-1803.

Any ideas? Is this a SkiaSharp.NativeAssets.NanoServer issue?

PNG Output creates transparent Image (alpha = 0)

When I create a PNG-Output, I'll get a PNG with alpha = 0 (transparent). Is there a way to set alpha = 1 (fully opaque)?

Also: If just use the option JPEG Output, I get a fully black Image. Is this an actual Issue or supposed to be that way?

Thanks in Advance.

Add information qr

I would like you to add an extension to write information to the qr code.
Here is an example of what it would look like

public static void Render(this SKCanvas canvas, QRCodeData data, int width, int hight, SKColor clearColor, SKColor codeColor, IconData iconData, string iconInfo)
        {
            var hightInconInfo = 25;
            canvas.Clear(clearColor);
            using QRCodeRenderer qRCodeRenderer = new QRCodeRenderer();
            SKRect area = SKRect.Create(0f, 0f, width, hight + hightInconInfo);
            qRCodeRenderer.Render(canvas, area, data, codeColor, null, iconData);

            var fontInitNumber = 35;

            // draw some text
            var paint = new SKPaint
            {
                Color = SKColors.Black,
                IsAntialias = true,
                Style = SKPaintStyle.Fill,
                TextAlign = SKTextAlign.Center,
                TextSize = fontInitNumber
            };

            float widthicon = width / 100f * (float)iconData.IconSizePercent;
            float heigthicon = hight / 100f * (float)iconData.IconSizePercent;
            float xicon = width / 2f - widthicon / 2f;
            float yicon = hight / 2f - heigthicon / 2f;

            var dimText = paint.MeasureText(iconInfo);

            while (dimText > widthicon)
            {
                paint.TextSize = fontInitNumber--;
                dimText = paint.MeasureText(iconInfo);
            }

            var coord = new SKPoint(xicon + (dimText / 2f) + ((widthicon - dimText) / 2f), yicon + heigthicon + 10f);
            canvas.DrawText(iconInfo, coord, paint);
        }
![descargar](https://user-images.githubusercontent.com/45176268/177049680-d0997d0e-9594-48ea-9c9c-654a0877217c.png)

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.