SlideShare a Scribd company logo
Creating QR codes in Java
    Alison McGreavy
What’s a QR code?
                    QR is short for “Quick
                    Response”.

                    Developed in Japan by Denso
                    Wave http://www.denso-
                    wave.com/qrcode/index-e.html


                    Is a 2D barcode.

                    Can hold
                    •URL
                    •VCard
                    •Text
                    •meCard

                    http://en.wikipedia.org/wiki/QR_code
How can I decode a QR code?
                                  Use an app on your smartphone to scan in the QR code




Use an online tool to enter the
URL of an image or upload an
image
How can I create a QR code?

                         Use a smartphone app


                         Or an online tool such as
                         http://zxing.appspot.com/generator
Yes, but how can I create a QR code?
              (preferably in Java)
ZXing

• Download the ZXing-2.0.zip files from http://code.google.com/p/zxing/
• Unzip this on your local machine i.e. C:/zxing-2.0
• Install the ‘core’ package C:/zxing-2.0/core
• Install the ‘javase’ package C:/zxing-2.0/javase
Java
  • Java download – use JDK for development
       http://www.oracle.com/technetwork/java/javase/downloads/index.html
  • Set JAVA_HOME environment variable to location of JDK ie C:Program
    FilesJavajdk1.6.0_32bin
  • Add JAVA_HOME variable to PATH variable
  • Create Java project
  • Add code.jar and jse.jar to your project classpath
  • Start to code
Java class to create & decode QR codes
 package uk.co.girlgeek;

 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import javax.imageio.ImageIO;

 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
 import com.google.zxing.client.j2se.MatrixToImageWriter;
 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.common.HybridBinarizer;
 import com.google.zxing.qrcode.QRCodeWriter;
 import com.google.zxing.BarcodeFormat;
 import com.google.zxing.BinaryBitmap;
 import com.google.zxing.LuminanceSource;
 import com.google.zxing.MultiFormatReader;
 import com.google.zxing.Reader;

 public class QRCreator {

       private static String IMAGEDIR = "C:/Alison/images";
       private static int IMAGE_WIDTH = 400;
       private static int IMAGE_HEIGHT = 400;
Main method
public static void main(String[] args) {
   try {
       QRCreator qrTest = new QRCreator();

      // Create a QR code from a text message
      String filenameOfQrFromText = IMAGEDIR + "/qr_fromtext.png";
      String messageToEncode = "Hi there my name is Alison and my email is almcgreavy@yahoo.co.uk";
      qrTest.createQRCode(messageToEncode, filenameOfQrFromText);

      // Create a QR code from a URL
      String filenameofQrfromURL = IMAGEDIR + "/qr_fromurl.png";
      String urlToEncode = "http://www.geekgirlmeetup.co.uk";
      qrTest.createQRCode(urlToEncode, filenameofQrfromURL);

      // Read a text QR code
      qrTest.readQRCode(filenameOfQrFromText);

      // Read a URL QR code
      qrTest.readQRCode(filenameofQrfromURL);

    } catch (Exception e) {
       e.printStackTrace();
       System.exit(-1);
    }
}
Method to Create QR code


 private void createQRCode(String message, String filename) throws Exception {

       File file = new File(filename);
       BitMatrix bitMatrix = new QRCodeWriter().encode(message, BarcodeFormat.QR_CODE, IMAGE_WIDTH,
                IMAGE_HEIGHT);
       MatrixToImageWriter.writeToStream(bitMatrix, "PNG", new FileOutputStream(file));

       System.out.println("Creating QR Image: " + file.getAbsolutePath());
 }
Method to Decode a QR code

 private void readQRCode(String imageFilename) throws Exception {

       InputStream barCodeInputStream = new FileInputStream(imageFilename);
       BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

       LuminanceSource source = new BufferedImageLuminanceSource(
       barCodeBufferedImage);
       BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
       Reader reader = new MultiFormatReader();
       com.google.zxing.Result result = reader.decode(bitmap);

       System.out.println("QR text is: " + result.getText());
 }
Java Tools & Tutorials
 IDE : Eclipse http://www.eclipse.org/
 Build Tools : Ant / Maven http://maven.apache.org/
 Version Control: CVS http://en.wikipedia.org/wiki/Concurrent_Versions_System
 Continuous Integration: Jenkins http://en.wikipedia.org/wiki/Jenkins_(software)
 Unit testing: Junit http://en.wikipedia.org/wiki/JUnit
                    Jmock http://www.jmock.org/
 Quality Metrics: Checkstyle http://checkstyle.sourceforge.net/
                    PMD http://pmd.sourceforge.net/pmd-5.0.0/
 Issue Management: Bugzilla http://www.bugzilla.org/about/

 Java Tutorials : http://docs.oracle.com/javase/tutorial/
          http://www.javaranch.com/
          http://www.coderanch.com/how-to/java/how-to-create-java-program
Alison McGreavy almcgreavy@yahoo.co.uk

More Related Content

Viewers also liked (10)

Data Communication and Networking
Data Communication and NetworkingData Communication and Networking
Data Communication and Networking
 
Java codes
Java codesJava codes
Java codes
 
Easy IP Addressing and Subnetting Manual for Starters
Easy IP Addressing and Subnetting Manual for StartersEasy IP Addressing and Subnetting Manual for Starters
Easy IP Addressing and Subnetting Manual for Starters
 
School admission process management system (Documention)
School admission process management system (Documention)School admission process management system (Documention)
School admission process management system (Documention)
 
Methods of data collection
Methods of data collectionMethods of data collection
Methods of data collection
 
Source of Data in Research
Source of Data in ResearchSource of Data in Research
Source of Data in Research
 
Chapter 9-METHODS OF DATA COLLECTION
Chapter 9-METHODS OF DATA COLLECTIONChapter 9-METHODS OF DATA COLLECTION
Chapter 9-METHODS OF DATA COLLECTION
 
Tools of data collection
Tools of data collectionTools of data collection
Tools of data collection
 
Data Collection-Primary & Secondary
Data Collection-Primary & SecondaryData Collection-Primary & Secondary
Data Collection-Primary & Secondary
 
Computer networking devices
Computer networking devicesComputer networking devices
Computer networking devices
 

Similar to Qr codesinjava gg_london

Qr presentation
Qr presentation Qr presentation
Qr presentation
kfischer08
 
QRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptx
QRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptxQRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptx
QRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptx
Renu36791
 
Qr codes to support learning christine
Qr codes to support learning   christineQr codes to support learning   christine
Qr codes to support learning christine
Christine Ling
 
125 고성능 web view-deview 2013 발표 자료_공유용
125 고성능 web view-deview 2013 발표 자료_공유용125 고성능 web view-deview 2013 발표 자료_공유용
125 고성능 web view-deview 2013 발표 자료_공유용
NAVER D2
 

Similar to Qr codesinjava gg_london (20)

How to generate qr code in asp.net c# ?
How to generate qr code in asp.net c# ?How to generate qr code in asp.net c# ?
How to generate qr code in asp.net c# ?
 
Q rcodes
Q rcodesQ rcodes
Q rcodes
 
HTML for the Mobile Web, Firefox OS
HTML for the Mobile Web, Firefox OSHTML for the Mobile Web, Firefox OS
HTML for the Mobile Web, Firefox OS
 
PacNOG 25: Life of a QR code
PacNOG 25: Life of a QR codePacNOG 25: Life of a QR code
PacNOG 25: Life of a QR code
 
Introduction to Apache Cordova (Phonegap)
Introduction to Apache Cordova (Phonegap)Introduction to Apache Cordova (Phonegap)
Introduction to Apache Cordova (Phonegap)
 
Qr presentation
Qr presentation Qr presentation
Qr presentation
 
QRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptx
QRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptxQRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptx
QRcode USING PYTHON LANGUAGE ELECTRONICS STUDENT.pptx
 
Intro to computer vision in .net update
Intro to computer vision in .net   updateIntro to computer vision in .net   update
Intro to computer vision in .net update
 
Qr code presentation
Qr code presentationQr code presentation
Qr code presentation
 
Node in Production at Aviary
Node in Production at AviaryNode in Production at Aviary
Node in Production at Aviary
 
QR Codes for CAD/GIS Professionals
QR Codes for CAD/GIS ProfessionalsQR Codes for CAD/GIS Professionals
QR Codes for CAD/GIS Professionals
 
Qr codes to support learning christine
Qr codes to support learning   christineQr codes to support learning   christine
Qr codes to support learning christine
 
125 고성능 web view-deview 2013 발표 자료_공유용
125 고성능 web view-deview 2013 발표 자료_공유용125 고성능 web view-deview 2013 발표 자료_공유용
125 고성능 web view-deview 2013 발표 자료_공유용
 
QR Code Demo
QR Code DemoQR Code Demo
QR Code Demo
 
Philipp Nagele (CTO, Wikitude) An Insider Deep-Dive into the Wikitude SDK
Philipp Nagele (CTO, Wikitude) An Insider Deep-Dive into the Wikitude SDK Philipp Nagele (CTO, Wikitude) An Insider Deep-Dive into the Wikitude SDK
Philipp Nagele (CTO, Wikitude) An Insider Deep-Dive into the Wikitude SDK
 
Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28
Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28
Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28
 
What the Heck are QR codes and can I use it?
What the Heck are QR codes and can I use it?What the Heck are QR codes and can I use it?
What the Heck are QR codes and can I use it?
 
How We Built a Mobile Electronic Health Record App Using Xamarin, Angular, an...
How We Built a Mobile Electronic Health Record App Using Xamarin, Angular, an...How We Built a Mobile Electronic Health Record App Using Xamarin, Angular, an...
How We Built a Mobile Electronic Health Record App Using Xamarin, Angular, an...
 
OWASP SF - Reviewing Modern JavaScript Applications
OWASP SF - Reviewing Modern JavaScript ApplicationsOWASP SF - Reviewing Modern JavaScript Applications
OWASP SF - Reviewing Modern JavaScript Applications
 
Mobile Vue.js – From PWA to Native
Mobile Vue.js – From PWA to NativeMobile Vue.js – From PWA to Native
Mobile Vue.js – From PWA to Native
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 

Qr codesinjava gg_london

  • 1. Creating QR codes in Java Alison McGreavy
  • 2. What’s a QR code? QR is short for “Quick Response”. Developed in Japan by Denso Wave http://www.denso- wave.com/qrcode/index-e.html Is a 2D barcode. Can hold •URL •VCard •Text •meCard http://en.wikipedia.org/wiki/QR_code
  • 3. How can I decode a QR code? Use an app on your smartphone to scan in the QR code Use an online tool to enter the URL of an image or upload an image
  • 4. How can I create a QR code? Use a smartphone app Or an online tool such as http://zxing.appspot.com/generator
  • 5. Yes, but how can I create a QR code? (preferably in Java)
  • 6. ZXing • Download the ZXing-2.0.zip files from http://code.google.com/p/zxing/ • Unzip this on your local machine i.e. C:/zxing-2.0 • Install the ‘core’ package C:/zxing-2.0/core • Install the ‘javase’ package C:/zxing-2.0/javase
  • 7. Java • Java download – use JDK for development http://www.oracle.com/technetwork/java/javase/downloads/index.html • Set JAVA_HOME environment variable to location of JDK ie C:Program FilesJavajdk1.6.0_32bin • Add JAVA_HOME variable to PATH variable • Create Java project • Add code.jar and jse.jar to your project classpath • Start to code
  • 8. Java class to create & decode QR codes package uk.co.girlgeek; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import javax.imageio.ImageIO; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.Reader; public class QRCreator { private static String IMAGEDIR = "C:/Alison/images"; private static int IMAGE_WIDTH = 400; private static int IMAGE_HEIGHT = 400;
  • 9. Main method public static void main(String[] args) { try { QRCreator qrTest = new QRCreator(); // Create a QR code from a text message String filenameOfQrFromText = IMAGEDIR + "/qr_fromtext.png"; String messageToEncode = "Hi there my name is Alison and my email is almcgreavy@yahoo.co.uk"; qrTest.createQRCode(messageToEncode, filenameOfQrFromText); // Create a QR code from a URL String filenameofQrfromURL = IMAGEDIR + "/qr_fromurl.png"; String urlToEncode = "http://www.geekgirlmeetup.co.uk"; qrTest.createQRCode(urlToEncode, filenameofQrfromURL); // Read a text QR code qrTest.readQRCode(filenameOfQrFromText); // Read a URL QR code qrTest.readQRCode(filenameofQrfromURL); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
  • 10. Method to Create QR code private void createQRCode(String message, String filename) throws Exception { File file = new File(filename); BitMatrix bitMatrix = new QRCodeWriter().encode(message, BarcodeFormat.QR_CODE, IMAGE_WIDTH, IMAGE_HEIGHT); MatrixToImageWriter.writeToStream(bitMatrix, "PNG", new FileOutputStream(file)); System.out.println("Creating QR Image: " + file.getAbsolutePath()); }
  • 11. Method to Decode a QR code private void readQRCode(String imageFilename) throws Exception { InputStream barCodeInputStream = new FileInputStream(imageFilename); BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream); LuminanceSource source = new BufferedImageLuminanceSource( barCodeBufferedImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new MultiFormatReader(); com.google.zxing.Result result = reader.decode(bitmap); System.out.println("QR text is: " + result.getText()); }
  • 12. Java Tools & Tutorials IDE : Eclipse http://www.eclipse.org/ Build Tools : Ant / Maven http://maven.apache.org/ Version Control: CVS http://en.wikipedia.org/wiki/Concurrent_Versions_System Continuous Integration: Jenkins http://en.wikipedia.org/wiki/Jenkins_(software) Unit testing: Junit http://en.wikipedia.org/wiki/JUnit Jmock http://www.jmock.org/ Quality Metrics: Checkstyle http://checkstyle.sourceforge.net/ PMD http://pmd.sourceforge.net/pmd-5.0.0/ Issue Management: Bugzilla http://www.bugzilla.org/about/ Java Tutorials : http://docs.oracle.com/javase/tutorial/ http://www.javaranch.com/ http://www.coderanch.com/how-to/java/how-to-create-java-program