8

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.

1 Answer 1

6

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
\def\wFrac{0.33}%   % 0.3 is usually OK for M (sometimes can go up to 0.4 for H)

% Dimension registers to use later
\newdimen\qrWidth
\newdimen\imWidth

% 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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .