Scan this:
You will be redirected to https://crunchify.com
QR code
(abbreviated from Quick Response Code
) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan. Bar codes are optical machine-readable labels attached to items that record information related to the item. Initially patented, its patent holder has chosen not to exercise those rights. Recently, the QR Code system has become popular outside the automotive industry due to its fast readability and greater storage capacity compared to standard UPC barcodes.
The code consists of black modules (square dots) arranged in a square grid on a white background. ZXING
is a Multi-format 1D/2D barcode image processing library with clients for Android, Java. It is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages.
Our focus is on using the built-in camera on mobile phones to scan and decode barcodes on the device, without communicating with a server. However the project can be used to encode and decode barcodes on desktops and servers as well. Here is a simple Java Code which generates QR code for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package crunchify.com.tutorial; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.EnumMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * @author Crunchify.com * Updated: 03/20/2016 - added code to narrow border size */ public class CrunchifyQRCode { // Tutorial: http://zxing.github.io/zxing/apidocs/index.html public static void main(String[] args) { String myCodeText = "https://crunchify.com/"; String filePath = "/Users/appshah/Documents/CrunchifyQR.png"; int size = 250; String fileType = "png"; File myFile = new File(filePath); try { Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // Now with zxing version 3.2.1 you could change border size (white border size to just 1) hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */ hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText, BarcodeFormat.QR_CODE, size, size, hintMap); int CrunchifyWidth = byteMatrix.getWidth(); BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth); graphics.setColor(Color.BLACK); for (int i = 0; i < CrunchifyWidth; i++) { for (int j = 0; j < CrunchifyWidth; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } ImageIO.write(image, fileType, myFile); } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("\n\nYou have successfully created QR Code."); } } |
Output: (Scan it by yourself)
Which library do I have to download?
If you have Maven project then include this dependency.
1 2 3 4 5 |
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version> </dependency> |
OR Download .jar file manually
Step-1:
Download link.
Step-2
Once you download it you have to include it into your project’s classpath. How to add .jar file to Project Build Path in Eclipse.