sbs

sbs

QRCode - Implement QR Code in Elixir

Only 650 LOC, wrote for fun :slight_smile:

Most Liked

Eiji

Eiji

@Raddi_On:
You can use for example request for json.
For example:

defmodule MyAppWeb.Controllers.ExampleController do
  def action_name(conn, %{param_name: param_value}) do
    matrix = QRCode.encode(param_value).matrix
    data = matrix |> Tuple.to_list() |> Enum.map(&Tuple.to_list/1)
    json(conn, data)
  end
end

You can also use probably Drab:

Eiji

Eiji

@Raddi_On: Just few steps:

  1. Of course you need somehow send these data to JavaScript (let’s say in json format)
  2. You need a simple function that draws qrcode - it’s just matrix of 0 (white) and 1 (black) squares. Drawing squares in canvas is one of the easiest things to do.
  3. You need to use export as I already show

I will show you how it could look in Elixir:

defmodule Example do
  # @canvas ...

  def draw_image(matrix) do
    # firstly we need to have enumarable - not tuples, so:
    matrix
    |> Tuple.to_list()
    |> Enum.map(&Tuple.to_list/1)
    # then we need to have a row numbers like:
    |> Enum.with_index()
    # then for all rows:
    |> Enum.map(&draw_row/1)
  end

  defp draw_row({row, index}) do
    row
    # we need to know also column index:
    |> Enum.with_index()
    # then for all cells in row:
    |> Enum.map(&draw_cell(&1, index))
  end

  defp draw_cell({0, column}, row) do
    do_draw_cell(row, column, {255, 255, 255})
  end
  defp draw_cell({1, column}, row) do
    do_draw_cell(row, column, {0, 0, 0})
  end

  defp do_draw_cell(row, column, color) do
    # you have row, column and color to draw
    # simply calculate `x` and `y` like:
    square_size = 5
    x = column * square_size
    y = row * square_size
    # now you can call real draw function
    # with x, y, width (square_size), height (square_size) 
    # and finally fill it with color
    Canvas.draw(@canvas, x, y, square_size, square_size, color)
    # use canvas HTML5 API to export image
    # for example on download button click
  end
end

As you can see square coordinates are noting special. You just need to know in which row and column you are and simply multiply them by number of pixels that you wish to have for each cell.

Eiji

Eiji

@sbs: No need to implement png encoder from scratch. Just draw QR code in canvas and add simple option to export it. :smiley:

For example you can draw a canvas and change it to image really easy in JavaScript:

var canvas = document.querySelector("canvas#selector.here");
var data = canvas.toDataURL("image/png");
document.write('<img src="' + data + '"/>');

It’s much more simpler and allows to simple export in much more formats without any library for this!

sztosz

sztosz

Or instead of using js, becaouse… You know… Not everything resolves about web pages :wink: you can just use existing Erlang libraries like i.e. https://github.com/yuce/png/blob/master/README.md

Neurofunk

Neurofunk

Hey that’s quite nice!
Does it only do ANSI output or is there any chance you’d implement images? Perhaps by taking https://gitlab.com/Pacodastre/qrcode as a backend?

Where Next?

Popular in Libraries Top

hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Sub Categories:

We're in Beta

About us Mission Statement