TransWikia.com

Generate QR Code with image inside

TeX - LaTeX Asked by Til Hund on January 4, 2021

How can I generate in xelatex a QR code with any given image inside, like so or better like this, using either pstricks or the new qrcode package? Note that the QR-code must still work after including the image.

One Answer

Follows a proposal using tikz. For the code to properly compile, the image to embed (named "image.jpg" in this code) must be present, like this one:

image to embed

The result is given by:

QR code with and embedded image

The code to produce this result is the following:

documentclass[border={10pt}]{standalone}
usepackage[forget]{qrcode}
usepackage{tikz}

% Program to draw a QR code and insert an image in
% its center without disrupting the contents
% v.santos, 2020

begin{document}

% Fraction of QR code width to replace
% Maximal value depends on the level of robustness of QR code
defwFrac{0.33}%   % 0.3 is usually OK for M (sometimes can go up to 0.4 for H)

% Dimension registers to use later
newdimenqrWidth
newdimenimWidth

% The actual desired size of the QR code in the page (should not affect the result)
setlength{qrWidth}{4cm}%

% define the text to embed (can change the level
% of robustness with option "level")
% The qrcode package requires some chars to be escaped (like space)
newcommand{myQR}{
qrcode[height=qrWidth,level=M]{
My QR code with an image in it.
 }
}%

fbox{% % to put a frame around the QR code
begin{tikzpicture}
node [anchor=south west,inner sep=0] at (0,0) (QR) {myQR};
pgfmathsetlength{imWidth}{wFrac*qrWidth}
begin{scope}[x={(QR.south east)},y={(QR.north west)}]
    node [inner sep=0,anchor=south west] at (0.5-wFrac/2,0.5-wFrac/2) {includegraphics[width=imWidth,height=imWidth]{image.jpg}};
end{scope}
end{tikzpicture}%
}

end{document}

Correct answer by vsantos on January 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP