BarcodeCanvas.java

Navigation:  Tutorials > Sample Java Application >

BarcodeCanvas.java

Previous pageReturn to chapter overviewNext page

import java.awt.*;

import java.io.*;

 

public class BarcodeCanvas extends Canvas {

 

   private com.mw6.barcode.Barcode myBC = new com.mw6.barcode.Barcode();

   private com.mw6.barcode.PDF417 myPDF417= new com.mw6.barcode.PDF417();

   private com.mw6.barcode.DataMatrix myDM = new com.mw6.barcode.DataMatrix();

   private com.mw6.barcode.Aztec myAztec = new com.mw6.barcode.Aztec();

   private com.mw6.barcode.QRCode myQRCode = new com.mw6.barcode.QRCode();

   private com.mw6.barcode.MaxiCode myMC = new com.mw6.barcode.MaxiCode();

 

   private byte[] imageData = new byte[1000];

   private int[] sizeArray = new int[2];

   private int bCategory;

 

   public BarcodeCanvas(int barcodeCategory)

   {

       super();

     

       bCategory = barcodeCategory;

 

       try {

           if (barcodeCategory == 1)

           {

               // General Barcode

               myBC.setData("012349");

               myBC.setNarrowBarWidth(0.04);

               myBC.setBarHeight(2.0);

               myBC.setTextFont(new java.awt.Font("Helvetica", java.awt.Font.PLAIN, 16));

               myBC.setRotation(com.mw6.barcode.Barcode.ORIENTATION_0);

               myBC.setResolution(96);

               myBC.setCheckDigit(false);

               myBC.setCheckDigitToText(true);

               myBC.setShowText(true);

               myBC.setBorderWidth(0.0);

 

               imageData = myBC.exportImageStream("png");

           }

           else if (barcodeCategory == 2)

           {

               // PDF417

               myPDF417.setNarrowBarWidth(0.08);

               myPDF417.setData("Your String");

               myPDF417.setRotation(com.mw6.barcode.PDF417.ORIENTATION_0);

               myPDF417.setTruncateSymbol(false);

               myPDF417.setColumns((short)3);

               myPDF417.setHandleTilde(true);

               myPDF417.getActualSize(sizeArray);

 

               imageData = myPDF417.exportImageStream("png");

           }

           else if (barcodeCategory == 3)

           {

               // DataMatrix

               myDM.setModuleSize(0.07);

               myDM.setData("Your String");

               myDM.setResolution(96);

               myDM.setMode(com.mw6.barcode.DataMatrix.MODE_ASCII);

               myDM.setPreferredFormat(com.mw6.barcode.DataMatrix.FORMAT_48X48);

               myDM.setRotation(com.mw6.barcode.DataMatrix.ORIENTATION_0);

               myDM.setHandleTilde(true);

               myDM.getActualSize(sizeArray);

 

               imageData = myDM.exportImageStream("png");

           }

           else if (barcodeCategory == 4)

           {

               // Aztec

               myAztec.setPreferredFormat(com.mw6.barcode.Aztec.FORMAT_67X67);

               myAztec.setModuleSize(0.12);

               myAztec.setData("Your String");

               myAztec.setResolution(96);

               myAztec.setRotation(com.mw6.barcode.Aztec.ORIENTATION_0);

               myAztec.setHandleTilde(true);

               myAztec.getActualSize(sizeArray);

 

               imageData = myAztec.exportImageStream("png");

           }

           else if (barcodeCategory == 5)

           {

               // QRCode

               myQRCode.setVersion(com.mw6.barcode.QRCode.VERSION_2);

               myQRCode.setModuleSize(0.10);

               myQRCode.setData("Your String");

               myQRCode.setResolution(96);

               myQRCode.setRotation(com.mw6.barcode.QRCode.ORIENTATION_0);

               myQRCode.getActualSize(sizeArray);

 

               imageData = myQRCode.exportImageStream("png");

           }

           else if (barcodeCategory == 6)

           {

               // MaxiCode

               myMC.setMode(com.mw6.barcode.MaxiCode.MODE_2);

               myMC.setData("[)>~d03001~d0299615238~d029840~d029001~d029AIM, Inc~d029634 Alpha Drive~d029Pittsburgh~d029PA~d030~d004");

               myMC.setHandleTilde(true);

               myMC.setResolution(96);

               myMC.setRotation(com.mw6.barcode.MaxiCode.ORIENTATION_0);

               myMC.setSize(400, 400);

               myMC.getActualSize(sizeArray);

 

               imageData = myMC.exportImageStream("png");

           }

       } catch (IOException e) {}

 

       // Set up canvas's size

       this.setSize(sizeArray[0], sizeArray[1]);

   }

 

   public void paint(Graphics g) {

 

       if (bCategory == 1)

           myBC.render(g, 0, 0);

       else if(bCategory == 2)

           myPDF417.render(g, 0, 0);

       else if(bCategory == 3)

           myDM.render(g, 0, 0);

       else if(bCategory == 4)

           myAztec.render(g, 0, 0);

       else if(bCategory == 5)

           myQRCode.render(g, 0, 0);

       else if(bCategory == 6)

           myMC.render(g, 0, 0);

   }

 

   public void saveImage(String fileName, String imgFormat) throws IOException {

 

       if (bCategory == 1)

           myBC.saveAsImage(fileName, imgFormat);

       else if(bCategory == 2)

           myPDF417.saveAsImage(fileName, imgFormat);

       else if(bCategory == 3)

           myDM.saveAsImage(fileName, imgFormat);

       else if(bCategory == 4)

           myAztec.saveAsImage(fileName, imgFormat);

       else if(bCategory == 5)

           myQRCode.saveAsImage(fileName, imgFormat);

       else if(bCategory == 6)

           myMC.saveAsImage(fileName, imgFormat);

   }

}