生成包含图片的二维码

生成包含图片的二维码

我怎样才能生成xelatex包含任何给定图像的二维码,例如所以或者更好的是,使用pstricks或新的二维码包?请注意,添加图像后,二维码仍必须有效。

答案1

遵循使用 tikz 的提议。为了使代码正确编译,必须存在要嵌入的图像(此代码中名为“image.jpg”),如下所示:

要嵌入的图像

结果如下:

带有嵌入图像的二维码

产生此结果的代码如下:

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

相关内容