Aspose Demos > Java Components > Aspose.BarCode for Java > Java > Barcode Reader > Linear, 2D and Postal Barcodes

Welcome to theAspose.BarCode for Java Featured Demos!

When this demo runs without a license in evaluation mode, only Code39 types of barcodes will be detected.

Upload an image file using the file upload control below. This online demo will try to recognize the barcode from the image and show the codetext and symbology type on screen.

For more details please see the following topics:



Browse and select an image file to upload


JAVA

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

//////////////////////////////////////////////////////////////////////////
// Copyright 2002-2010 Aspose Pty Ltd. All Rights Reserved.
//
// This file is part of Aspose.BarCode. The source code in this file
// is only intended as a supplement to the documentation, and is provided
// "as is", without warranty of any kind, either expressed or implied.
//////////////////////////////////////////////////////////////////////////
package com.aspose.barcode.demos;

import com.aspose.barcoderecognition.BarCodeReadType;
import com.aspose.barcoderecognition.BarCodeReader;
import com.icesoft.faces.component.inputfile.FileInfo;
import com.icesoft.faces.component.inputfile.InputFile;
import java.awt.Toolkit;
import javax.faces.el.ValueBinding;
import javax.faces.event.ActionEvent;

public class BarCodeRecognizer extends Demo
{
    private String _strCodetext = "";
    public String getStrCodetext() {
        return _strCodetext;
    }
    public void setStrCodetext(String v) {
        _strCodetext = v;
    }

    private FileInfo currentFile;

    public FileInfo getCurrentFile() {
        return currentFile;
    }

    public void uploadActionListener(ActionEvent actionEvent) {
        try
        {
            init();
            
            InputFile inputFile = (InputFile) actionEvent.getSource();
            currentFile = inputFile.getFileInfo();

            _strCodetext = "";
            BarCodeReader reader = new BarCodeReader(Toolkit.getDefaultToolkit().getImage(currentFile.getPhysicalPath()),
                    BarCodeReadType.AllSupportedTypes);
            
            while(reader.read())
            {
                _strCodetext += "Codetext: " + reader.getCodeText() + ", Symbology: " + reader.getReadType().toString() + "<br></br>";
            }
            reader.close();

            if (_strCodetext.length() == 0)
                _strCodetext = "Cound not recognize barcode.";
        }
        catch(Exception ex)
        {
            //System.out.println(ex.getMessage());
            _strCodetext = "Error: " + ex.getMessage();
        }
    }
    
}

JAVA

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 73 74 75 76 77 78 79 80 81

package com.aspose.barcode.demos;

import java.io.*;

public class AsposeContext implements Serializable {

    private String _codtextColor = "BLACK";
    public String getCodetextColor() {
        return _codtextColor;
    }
    public void setCodetextColor(String v) {
        _codtextColor = v;
    }

    private String _xdimension = "0.6";
    public String getXdimension() {
        return _xdimension;
    }
    public void setXdimension(String v) {
        _xdimension = v;
    }

    private String _rotationAngle = "0";
    public String getRotationAngle() {
        return _rotationAngle;
    }
    public void setRotationAngle(String v) {
        _rotationAngle = v;
    }

    private String _imageQuality = "DEFAULT";
    public String getImageQuality() {
        return _imageQuality;
    }
    public void setImageQuality(String v) {
        _imageQuality = v;
    }

    private String _readType = "CODE128";
    public String getReadtype() {
        return _readType;
    }
    public void setReadtype(String v) {
        _readType = v;
    }

    private String _symbologyType = "CODE128";
    public String getSymbologytype() {
        return _symbologyType;
    }
    public void setSymbologytype(String v) {
        _symbologyType = v;
    }

    private String _codetext = "codetext";
    public String getCodetext() {
        return _codetext;
    }
    public void setCodetext(String v) {
        _codetext = v;
    }

    private String _mimeType = "image/jpg";
    public String getMimetype() {
        return _mimeType;
    }

    private Boolean _openNew = false;

    public Boolean getOpennewwindow() {
        return _openNew;
    }
    public void setOpennewwindow(Boolean v) {
        _openNew = v;
    }

    // Default Constructor
    public AsposeContext() {
    }
}

JAVA

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

//////////////////////////////////////////////////////////////////////////
// Copyright 2002-2010 Aspose Pty Ltd. All Rights Reserved.
//
// This file is part of Aspose.BarCode. The source code in this file
// is only intended as a supplement to the documentation, and is provided
// "as is", without warranty of any kind, either expressed or implied.
//////////////////////////////////////////////////////////////////////////
package com.aspose.barcode.demos;

import java.io.File;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.faces.context.FacesContext;

///////////////////////
import java.text.SimpleDateFormat;
import com.icesoft.faces.context.ByteArrayResource;
import com.icesoft.faces.context.Resource;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.lang.reflect.*;

/**
 * Base class for Aspose.BarCode.Demos classes that execute specific demos.
 */
public abstract class Demo {

    private byte[] imgBytes = null;
    protected static final String GENERATED_DIR_SHORT_NAME = "generated";

    /**
     * Called before launching any concrete demo-classes.
     * We need application path so we can find the database and template documents.
     */
    private void initDemo() throws Exception {
        //calculate common project dirs and files used by all demos.
        mProjectDir = getProjectDir();
        mGeneratedDir = new File(mProjectDir, "generated");

        //wipe out the dir for generated files.
        if (mGeneratedDir.exists()) {
            File[] files = mGeneratedDir.listFiles();
            for (int i = 0; i < files.length; i++) {
                files[i].delete();
            }
        } else {
            mGeneratedDir.mkdir();
        }

        context = FacesContext.getCurrentInstance();
        request = (HttpServletRequest) context.getExternalContext().getRequest();
        response = (HttpServletResponse) context.getExternalContext().getResponse();

        initLicense(new com.aspose.barcode.License());
        initLicense(new aspose.pdf.License());
        initLicense(new com.aspose.words.License());
        initLicense(new com.aspose.cells.License());
        initLicense(new com.aspose.pdf.kit.License());

        //LicenseUtil.setLicenses();
    }

    /**
     * Calculates the root dir of the demo project.
     *
     * The demo project dir is "demos/Aspose.BarCode.Demos".
     * It is proposed that compiled classes will be placed by compiler into "classes" subdir
     * of the project dir, so Demo.class (this) will be placed into
     * "demos/Aspose.BarCode.Demos/classes/com/aspose/BarCode/demos/Demo.class".
     */
    private static File getProjectDir() {
        URL url = Demo.class.getResource("Demo.class");

        String path = new File(url.getPath()).getAbsolutePath();
        String subPath = "\\classes\\com\\aspose\\barcode\\demos\\Demo.class";

        return new File(path.substring(0, path.length() - subPath.length()));
    }

    /**
     * Called by concrete demo-class. Calculates paths to files used solely by this concrete demo.
     */
    protected void init() throws Exception {
        initDemo();
    }
    /**
     * Project dir. Something like: 'Aspose.BarCode.Java installation path'\\demos\\Aspose.BarCode.Demos
     */
    protected static File mProjectDir;
    /**
     * Directory for images generated by the demos: demos\\generated.
     */
    protected static File mGeneratedDir;

    /**
     * Sends the document to the client browser.
     * demoName = full path + filename - no extention
     * formatType = extention
     */
    protected void sendToBrowser(String demoName, String formatType, boolean openNewWindow)
            throws Exception {
        //System.out.println("Sending " + demoName + ", " + formatType);
        imgBytes = null;
        String fileName = demoName + formatType;

        // NOTE by Vit - IceFaces has a standard ice:outputResource control
        // to allow downloading resources. We don't have to use response for this purpose.

        // response.getOutputStream()
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        //Set Response content type
        //response.setContentType("application/msword");
//        setResptype("image/jpg");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
        int c;
        while ((= bis.read()) > -1) {
            out.write(c);
        }
        imgBytes = out.toByteArray();
        bis.close();
        //out.reset();

        setResponse(formatType, out);
        //context.responseComplete();
    }

    protected void setResponse(String formatType, ByteArrayOutputStream out) throws IOException {
        setFilename(formatType);
        resp = out.toByteArray();
        out.close();
        // outputStream.write(this.response);
    }

    public void setFilename(String formatType) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd_hh_mm_ss-", Locale.US);
        String curDate = formatter.format(new Date());

        this.fileName = curDate + FILE_NAME + formatType;

        //System.out.println("Response: " + this.fileName + " (" + v + ")");
    }
    
    protected FacesContext context;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    //////////////////////////////////////////////////////////
    protected final String FILE_NAME = "barcode";

    public Resource getGeneratedfile() {
        byte[] res = getResult();
        if (null == res || res.length < 1) {
            return null;
        }
        return new ByteArrayResource(res);
    }
    private String fileName = FILE_NAME;

    public String getFilename() {
        return fileName;
    }
    private String respType = "image/jpg";

    public String getResptype() {
        return respType;
    }

    public void setResptype(String v) {
        respType = v;
    }

    
    public byte[] getResult() {
        return resp;
    }

    protected byte[] resp;

    /**
     *
     * Initializes license (if demo license is available).
     * you may insert your licensing code here that would use obj's
     * setLicense based on InputStream rather than passing it on
     * to 3rd-party class.
     *
     */
    public static void initLicense(Object obj) {
        try {

            Class myclass = Class.forName("com.aspose.demos.Common");
            if (null == myclass) {
                System.out.println("initLicense not found");
                return;
            }

            //Use reflection to list methods and invoke them
            Method[] methods = myclass.getMethods();
            Object object = myclass.newInstance();

            Class partypes[] = new Class[1];
            partypes[0] = Object.class;

            Method meth = myclass.getMethod("initLicense", partypes);
            if (null != meth) {

                //System.out.println("Calling " + meth.getName());
                Object arglist[] = new Object[1];
                arglist[0] = obj;

                meth.invoke(object, arglist);
            }
        } catch (ClassNotFoundException cnfe) {
            // Ignore
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public byte[] getImgBytes() {
        return imgBytes;
    }
}

XHTML

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 73 74 75 76 77 78 79 80 81

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ice="http://www.icesoft.com/icefaces/component"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jstl/core"
      xmlns:jsf="http://www.aspose.com/jsf"
      >

    <ui:composition template="/WEB-INF/includes/templates/page-template.xhtml">

        <ui:define name="pageTitle">
            Recognize BarCode - Aspose.BarCode for Java Demos
        </ui:define>

        <ui:define name="page-content">
            <ui:decorate template="/WEB-INF/includes/templates/tabbed_container.xhtml">
                <ui:define name="example">

                    <p class="componentDescriptionTxt">Welcome to the <STRONG>Aspose.BarCode for Java</STRONG> Featured Demos!</p>
                    <p class="componentDescriptionTxt">When this demo runs without a license in evaluation mode,
                    only Code39 types of barcodes will be detected.</p>
                    
                    <p class="componentDescriptionTxt" align="left">
                        Upload an image file using the file upload control below.
                        This online demo will try to recognize the barcode from the image
                        and show the codetext and symbology type on screen.
                        <br></br><br></br>

                        For more details please see the following topics:
                        <ul>
                          <li style="text-align: left">
                            <a href="http://www.aspose.com/documentation/java-components/aspose.barcode-for-java/introducing-barcode-recognition.html">BarCode Recognition with Aspose.BarCode for Java</a>
                          </li>
                            <li style="text-align: left">
                            <a href="http://www.aspose.com/documentation/java-components/aspose.barcode-for-java/recognizing-specific-barcode-symbology.html">Specify Symbologies for BarCode</a>
                          </li>
                        </ul>
                    </p>

                    <br></br><br></br>
                    <table>
                        
                        <tr>
                            <td align="left">
                                Browse and select an image file to upload
                            </td>
                            <td width="10"></td>
                            <td>
                             <!--
                                <ui:include src="/WEB-INF/includes/templates/barcode-read-types.xhtml" />
                             -->
                            </td>
                        </tr>
                        
                        <tr>
                            <td colspan="3" align="left">
                                <ice:inputFile id="inputFileComponent"
                                       actionListener="#{recognize.uploadActionListener}" />
                                <ice:message for="inputFileComponent" />
                            </td>
                        </tr>
                        <tr>
                            <td align="left" colspan="3">
                                <ice:outputLabel value="#{recognize.strCodetext}" />
                            </td>
                        </tr>
                    </table>
                    <p align="left">
                        
                        <br></br>
                        
                    </p>

                </ui:define>
            </ui:decorate>
        </ui:define>
    </ui:composition>
</html>