Introduction

This week, the market people from my work wanted to put QR codes in shopping cart handles, but when they tested it, the QR code did not work. I noted that the cylindrical curvature (even small) distorted the image, and the cell phone can't read it.

Here is some test QR code:

I thought that this would be a nice thing to do with Mathematica, to
try to figure out how I could print the QR code into some way that
when attached to the cylindrical form, it would be like a normal
square, at least at some angles.

Teaser

See how the QR code projection looks, when the image is rotated:

Everything here can be used for real applications. At the end of this post you'll find images of the QR code printed on a real cylinder. But applications are not restricted to this. You can easily adapt the approach to keep you up all night

The theory

What shape should a QR code have in order to look like a normal square, at least at some angles, when attached to a
cylinder?

Exactly the viewpoint, more specifically the perspective projection, is crucial to determine how you have to transform your label so that it is squared again. Let me give an example where I drew something onto a paper-roll which obviously has nothing to do with the transformation used in bills answer:

If I now inspect this roll from a specific viewpoint it looks like a QR code should be recognized again:

The question is what happens here. The theory behind it is pretty easy and the good thing is, it explains what you have to do from any (meaningful) viewpoint. Let's use a simple cylinder graphic as example to explain what I mean

When you finally see the image on your screen, two transformations took place. First, ParametricPlot3D used my formula to transform from cylinder coordinates {u,v} into 3D Cartesian coordinates {x,y,z}. This transformation of the {u,v} plane can easily be simulated by sampling it with Table, doing the transformation to 3D by yourself and drawing lines

The next thing that happens is often taken for granted: The transformation of 3D points onto your final image plane you are seeing on the screen. This final ViewMatrix can (with some work) be extracted from a Mathematica graphics. It should work with AbsoluteOptions[gr3d, ViewMatrix] but it doesn't. Fortunately, Heike posted an answer how to do this.

Let's do it

OK, to say it with the words of Dr. Faust "Grau, teurer Freund, ist alle Theorie, und grün des Lebens goldner Baum". After trying it I noticed that the last two paragraphs of my first version are not necessary.

Let us first create a 3D plot of a cylinder, where we extract the matrices for viewing and keep them up to date even when we rotate the view.

Please note that this is no 3D graphics. We transform directly from {u,v} cylinder to {x,y} screen-coordinates. Those screen-coordinates are always in the range [0,1] for x and y.

Now comes the important step: This transformation can directly be used with TextureCoordinateFunction because this function provides you with {u,v} values and wants to know {x,y} texture positions. The only thing I do is, that I scale and translate the texture coordinates a bit so that the QR code is completely visible in the center of the image:

Don't rotate this graphics directly, because although it uses specific settings for ViewMatrix, it jumps directly to default settings when rotated the first time. Instead, copy our original cylinder image to a new notebook and rotate this. The Dymamic's will make, that both graphics are rotated.

Conclusion: When I use the following viewpoint to initialize the view point

and then evaluate the projection definition line again and recreate the textured cylinder, I get

which looks as if I just added a QR code layer to the image. Rotating and scaling reveals that it is specific texture projection instead

Going into real life

When you want to create a printable version of this, you could do the following. Interpolate the QR code image and use the same projection function you used in the texture (note that I used a factor 3 and {1/3,0} inside ipf here. You use whatever you used as texture):

Please note the Reverse since image matrices are always reversed and additionally, that I create now the image matrix for u from [-Pi,Pi]. This was a bug in the last version which created the back-side of the cylinder. Therefore, the perspective was not correct in the final result.

This can now be glued around a cylinder (after printing it with the appropriate height) and with the corrected print version, the result looks