mirror of
https://github.com/koreader/koreader
synced 2024-11-10 01:10:34 +00:00
43ba8a1173
2 new widgets: - QRWidget, that's like an ImageWidget, but with a text property that will be converted to a QR code ; - QRMessage, that's like an InfoMessage, but shows the message as QR code. Moreover, it adds the ability to export QR codes to the text editor. 1 new plugin: - Generate QR codes from clipboard Changes to text editor plugin: - Add the ability to export QR codes.
55 lines
1.5 KiB
Lua
55 lines
1.5 KiB
Lua
--[[
|
|
QRWidget shows a QR code for a given text.
|
|
]]
|
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
local ImageWidget = require("ui/widget/imagewidget")
|
|
local logger = require("logger")
|
|
local qrencode = require("ffi/qrencode")
|
|
local _ = require("gettext")
|
|
|
|
local QRWidget = ImageWidget:extend{
|
|
scale_factor = nil,
|
|
text = ""
|
|
-- see ImageWidget for other options.
|
|
}
|
|
|
|
function QRWidget:init()
|
|
local text = self.text
|
|
if #text > 2953 then
|
|
local truncated = _("... (truncated...)")
|
|
text = text:sub(1, 2953 - #truncated) .. truncated
|
|
end
|
|
local ok, grid = qrencode.qrcode(text)
|
|
if not ok then
|
|
logger.info("QRWidget: failed to generate QR code.")
|
|
return
|
|
else
|
|
local sq_size
|
|
if self.width then
|
|
if self.height then
|
|
sq_size = math.min(self.width, self.height)/#grid
|
|
else
|
|
sq_size = self.width/#grid
|
|
end
|
|
elseif self.height then
|
|
sq_size = self.height/#grid
|
|
else sq_size = 1
|
|
end
|
|
sq_size = math.floor(sq_size)
|
|
local grid_size = sq_size * #grid
|
|
local bb = Blitbuffer.new(grid_size, grid_size)
|
|
local white = Blitbuffer.COLOR_WHITE
|
|
for x, col in ipairs(grid) do
|
|
for y, lgn in ipairs(col) do
|
|
if lgn < 0 then
|
|
bb:paintRect((x - 1) * sq_size, (y - 1) * sq_size, sq_size, sq_size, white)
|
|
end
|
|
end
|
|
end
|
|
self.image = bb
|
|
end
|
|
end
|
|
|
|
return QRWidget
|