{"id":1328,"date":"2022-07-06T01:45:22","date_gmt":"2023-05-01T10:30:39","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/example-of-a-qr-code-generator-in-java-using-zxing\/"},"modified":"2024-03-07T04:01:52","modified_gmt":"2024-03-07T04:01:52","slug":"qr-code-generator-in-java-using-zxing","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/","title":{"rendered":"QR code generator in Java using zxing."},"content":{"rendered":"<p>Today, our focus will be on the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes. Nowadays, you can find them everywhere &#8211; on blogs, websites, and even in certain public locations. They are particularly popular in mobile apps, where you can scan a QR code using a QR code scanner app. This scan will either reveal the embedded text or direct you to a specific webpage if the QR code contains a URL. I recently came across this and found it fascinating. If you&#8217;re interested in learning more about QR codes, Wikipedia&#8217;s QR code page has a wealth of useful information.<\/p>\n<h2>A QR code generator written in Java.<\/h2>\n<p>After discovering QR code images on numerous websites, I began searching for a java QR code generator. Upon investigating various open source APIs, I concluded that <a href=\"https:\/\/github.com\/zxing\/zxing\">zxing<\/a> is the most user-friendly and uncomplicated option. If you intend to create a QR code image, you simply need to integrate its core library by incorporating the given dependency into your maven project.<\/p>\n<pre class=\"post-pre\"><code>&lt;dependency&gt;\r\n\t&lt;groupId&gt;com.google.zxing&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;core&lt;\/artifactId&gt;\r\n\t&lt;version&gt;3.3.2&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/code><\/pre>\n<p>If you want to use the JavaSE library to read a QR image through the command line, you can include the following dependency.<\/p>\n<pre class=\"post-pre\"><code>&lt;dependency&gt;\r\n\t&lt;groupId&gt;com.google.zxing&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;javase&lt;\/artifactId&gt;\r\n\t&lt;version&gt;3.3.2&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/code><\/pre>\n<div><img decoding=\"async\" class=\"post-images\" title=\"\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655dc28ecdcf9b67579feffa\/6-0.png\" alt=\"zxing maven dependencies\" \/><\/div>\n<h3>An example from <a href=\"https:\/\/github.com\/zxing\/zxing\">zxing<\/a> demonstrating the generation of a QR code image.<\/h3>\n<p>This is the program that you can utilize to produce an image of QR Code using zxing API. The file is named GenerateQRCode.java.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.qrcode.generator;\r\n\r\nimport java.awt.Color;\r\nimport java.awt.Graphics2D;\r\nimport java.awt.image.BufferedImage;\r\nimport java.io.File;\r\nimport java.io.IOException;\r\nimport java.util.Hashtable;\r\n\r\nimport javax.imageio.ImageIO;\r\n\r\nimport com.google.zxing.BarcodeFormat;\r\nimport com.google.zxing.EncodeHintType;\r\nimport com.google.zxing.WriterException;\r\nimport com.google.zxing.common.BitMatrix;\r\nimport com.google.zxing.qrcode.QRCodeWriter;\r\nimport com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;\r\n\r\npublic class GenerateQRCode {\r\n\r\n\tpublic static void main(String[] args) throws WriterException, IOException {\r\n\t\tString qrCodeText = \"https:\/\/www.scdev.com\";\r\n\t\tString filePath = \"JD.png\";\r\n\t\tint size = 125;\r\n\t\tString fileType = \"png\";\r\n\t\tFile qrFile = new File(filePath);\r\n\t\tcreateQRImage(qrFile, qrCodeText, size, fileType);\r\n\t\tSystem.out.println(\"DONE\");\r\n\t}\r\n\r\n\tprivate static void createQRImage(File qrFile, String qrCodeText, int size, String fileType)\r\n\t\t\tthrows WriterException, IOException {\r\n\t\t\/\/ Create the ByteMatrix for the QR-Code that encodes the given String\r\n\t\tHashtable&lt;EncodeHintType, ErrorCorrectionLevel&gt; hintMap = new Hashtable&lt;&gt;();\r\n\t\thintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);\r\n\t\tQRCodeWriter qrCodeWriter = new QRCodeWriter();\r\n\t\tBitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);\r\n\t\t\/\/ Make the BufferedImage that are to hold the QRCode\r\n\t\tint matrixWidth = byteMatrix.getWidth();\r\n\t\tBufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);\r\n\t\timage.createGraphics();\r\n\r\n\t\tGraphics2D graphics = (Graphics2D) image.getGraphics();\r\n\t\tgraphics.setColor(Color.WHITE);\r\n\t\tgraphics.fillRect(0, 0, matrixWidth, matrixWidth);\r\n\t\t\/\/ Paint and save the image using the ByteMatrix\r\n\t\tgraphics.setColor(Color.BLACK);\r\n\r\n\t\tfor (int i = 0; i &lt; matrixWidth; i++) {\r\n\t\t\tfor (int j = 0; j &lt; matrixWidth; j++) {\r\n\t\t\t\tif (byteMatrix.get(i, j)) {\r\n\t\t\t\t\tgraphics.fillRect(i, j, 1, 1);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tImageIO.write(image, fileType, qrFile);\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>This program has generated a QR Code image file, which you can test using your mobile QR Code scanner app. The QR Code should redirect you to the SC Home URL.<\/p>\n<h3>One possible paraphrase could be:<\/h3>\n<p>An instance of zxing being used as an illustration to decipher a QR code.<\/p>\n<p>If you do not possess a mobile application to perform the testing, there is no need to worry. Through the command line, you can utilize the zxing API to scan QR codes. The provided command enables the reading of the QR code image file. Take note of the auxiliary jars in the classpath, which are essential for zxing to function properly.<\/p>\n<pre class=\"post-pre\"><code>$java -cp $HOME\/.m2\/repository\/com\/google\/zxing\/javase\/3.3.2\/javase-3.3.2.jar:.:$HOME\/.m2\/repository\/com\/google\/zxing\/core\/3.3.2\/core-3.3.2.jar:$HOME\/.m2\/repository\/com\/beust\/jcommander\/1.72\/jcommander-1.72.jar:$HOME\/.m2\/repository\/com\/github\/jai-imageio\/jai-imageio-core\/1.3.1\/jai-imageio-core-1.3.1.jar com.google.zxing.client.j2se.CommandLineRunner JD.png\r\n<\/code><\/pre>\n<div><img decoding=\"async\" class=\"post-images\" title=\"\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655dc28ecdcf9b67579feffa\/14-0.png\" alt=\"zxing read qr code image\" \/><\/div>\n<p>You have the option to obtain the QR Code Generator and Reader maven project from our GitHub Repository through download.<\/p>\n<p>More Java Totor:<\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/addition-assignment-operator-in-java\/\" target=\"_blank\" rel=\"noopener\">Addition Assignment Operator mean in Java<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/tutorial-on-hibernate-tomcat-jndi-datasource\/\" target=\"_blank\" rel=\"noopener\">Tutorial on how to set up a Hibernate Tomcat JNDI DataSource.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, our focus will be on the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes. Nowadays, you can find them everywhere &#8211; on blogs, websites, and even in certain public locations. They are particularly popular in mobile apps, where you can scan a [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1328","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>QR code generator in Java using zxing. - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"QR code generator in Java using zxing.\" \/>\n<meta property=\"og:description\" content=\"the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-01T10:30:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-07T04:01:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655dc28ecdcf9b67579feffa\/6-0.png\" \/>\n<meta name=\"author\" content=\"Isabella Edwards\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Isabella Edwards\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/\"},\"author\":{\"name\":\"Isabella Edwards\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\"},\"headline\":\"QR code generator in Java using zxing.\",\"datePublished\":\"2023-05-01T10:30:39+00:00\",\"dateModified\":\"2024-03-07T04:01:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/\"},\"wordCount\":412,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/\",\"name\":\"QR code generator in Java using zxing. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-05-01T10:30:39+00:00\",\"dateModified\":\"2024-03-07T04:01:52+00:00\",\"description\":\"the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"QR code generator in Java using zxing.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\",\"name\":\"Isabella Edwards\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"caption\":\"Isabella Edwards\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"QR code generator in Java using zxing. - Blog - Silicon Cloud","description":"the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/","og_locale":"en_US","og_type":"article","og_title":"QR code generator in Java using zxing.","og_description":"the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes.","og_url":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-05-01T10:30:39+00:00","article_modified_time":"2024-03-07T04:01:52+00:00","og_image":[{"url":"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655dc28ecdcf9b67579feffa\/6-0.png"}],"author":"Isabella Edwards","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Isabella Edwards","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/"},"author":{"name":"Isabella Edwards","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd"},"headline":"QR code generator in Java using zxing.","datePublished":"2023-05-01T10:30:39+00:00","dateModified":"2024-03-07T04:01:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/"},"wordCount":412,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/","url":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/","name":"QR code generator in Java using zxing. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-05-01T10:30:39+00:00","dateModified":"2024-03-07T04:01:52+00:00","description":"the Java QR code generator program. If you keep up with technology and gadgets, then you probably know about QR codes.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/qr-code-generator-in-java-using-zxing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"QR code generator in Java using zxing."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd","name":"Isabella Edwards","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","caption":"Isabella Edwards"},"url":"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=1328"}],"version-history":[{"count":3,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1328\/revisions"}],"predecessor-version":[{"id":1694,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1328\/revisions\/1694"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=1328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=1328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=1328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}