views:

155

answers:

6

I'm creating barcodes on the fly, and embedding them in web pages. What format should I use? I'm wavering between JPG and PNG, and I don't really understand the advantages of both. Size matters in this, as does readability (better a clear barcode than a blurry one). Is there another format that would be better yet?

+9  A: 

PNG, or even GIF. Jpeg is right out, because it's designed for the smooth gradients of photographs, not the sharp edges of bar codes. Draw a simple Bar code by hand in MS Paint, save it, and then reload it, you'll see all kinds of compression artifacts.

Aric TenEyck
With only two colors (black and white), both PNG and GIF should compress extremely well.
Mark Ransom
And totally lossless, too!
Mark Ransom
+3  A: 

PNG should be better than JPG (JPG is lossy). GIF might be even better since the image is going to be monochrome (but check it in your case).

Michael Todd
I would not recommend GIF as it's not free.
Assaf Lavie
I believe the GIF patent has expired in most countries, which makes it free enough.
Mark Ransom
@Mark Ransom: Yeah, in 2001 I think.
Lucas Jones
You made me curious, so I checked. 2003 for the US, 2004 everywhere else. http://en.wikipedia.org/wiki/Gif#Unisys_and_LZW_patent_enforcement
Mark Ransom
+5  A: 

Definitely PNG. It's much better at storing vector graphics, such as bar codes (as opposed to photos).

Wikipedia has a nice comparison of JPG vs. PNG.

Assaf Lavie
+1  A: 

PNG is the better option between PNG and JPG. The main issue here is of compression - there are two forms of compression techniques in images, lossy and lossless. Lossy compression algorithms (such as the one used by JPG files) "lose" information during the compression->decompression cycle. Because of this, there will be artifacts in your image. PNG, on the other hand, uses lossless compression, so the displayed image will be exactly the same as the original, uncompressed imagery prior to the initial saving.

In the case of barcodes, the exact pixel-by-pixel placement of black/white is very important (that's the entire point), so using a lossy compression routine like JPG could be very bad. If you're using a 2D barcode, it will be even worse, and your scanner may have a very difficult time reading the barcode accurately. PNG will completely eliminate this problem.

Reed Copsey
A: 

I have used .gif and .png successfully. My answer (and other answers, to be fair) to this question has more detail.

Doug L.
+1  A: 

PNG all the way. It will not only avoid the compression artifacts you'll get with JPG, but will almost certainly yield better compression as well. JPG is very badly suited to images with sharp edges.

Michael Borgwardt