//////////////////////////////////////////////////////////////////////////// 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;publicclassBarCodeRecognizerextendsDemo{privateString _strCodetext ="";publicString getStrCodetext(){return _strCodetext;}publicvoid setStrCodetext(String v){ _strCodetext = v;}privateFileInfo currentFile;publicFileInfo getCurrentFile(){return currentFile;}publicvoid uploadActionListener(ActionEvent actionEvent){try{ init();InputFile inputFile =(InputFile) actionEvent.getSource(); currentFile = inputFile.getFileInfo(); _strCodetext ="";BarCodeReader reader =newBarCodeReader(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();}}}
//////////////////////////////////////////////////////////////////////////// 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. */publicabstractclassDemo{privatebyte[] imgBytes =null;protectedstaticfinalString 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. */privatevoid initDemo()throwsException{//calculate common project dirs and files used by all demos. mProjectDir = getProjectDir(); mGeneratedDir =newFile(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". */privatestaticFile getProjectDir(){ URL url =Demo.class.getResource("Demo.class");String path =newFile(url.getPath()).getAbsolutePath();String subPath ="\\classes\\com\\aspose\\barcode\\demos\\Demo.class";returnnewFile(path.substring(0, path.length()- subPath.length()));}/** * Called by concrete demo-class. Calculates paths to files used solely by this concrete demo. */protectedvoid init()throwsException{ initDemo();}/** * Project dir. Something like: 'Aspose.BarCode.Java installation path'\\demos\\Aspose.BarCode.Demos */protectedstaticFile mProjectDir;/** * Directory for images generated by the demos: demos\\generated. */protectedstaticFile mGeneratedDir;/** * Sends the document to the client browser. * demoName = full path + filename - no extention * formatType = extention */protectedvoid sendToBrowser(String demoName,String formatType,boolean openNewWindow)throwsException{//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 =newByteArrayOutputStream();//Set Response content type//response.setContentType("application/msword");// setResptype("image/jpg");BufferedInputStream bis =newBufferedInputStream(newFileInputStream(fileName));int c;while((c = bis.read())>-1){ out.write(c);} imgBytes = out.toByteArray(); bis.close();//out.reset(); setResponse(formatType, out);//context.responseComplete();}protectedvoid setResponse(String formatType,ByteArrayOutputStream out)throwsIOException{ setFilename(formatType); resp = out.toByteArray(); out.close();// outputStream.write(this.response);}publicvoid setFilename(String formatType){SimpleDateFormat formatter =newSimpleDateFormat("yyyy-MMM-dd_hh_mm_ss-",Locale.US);String curDate = formatter.format(newDate());this.fileName = curDate + FILE_NAME + formatType;//System.out.println("Response: " + this.fileName + " (" + v + ")");}protectedFacesContext context;protectedHttpServletRequest request;protectedHttpServletResponse response;//////////////////////////////////////////////////////////protectedfinalString FILE_NAME ="barcode";publicResource getGeneratedfile(){byte[] res = getResult();if(null== res || res.length <1){returnnull;}returnnewByteArrayResource(res);}privateString fileName = FILE_NAME;publicString getFilename(){return fileName;}privateString respType ="image/jpg";publicString getResptype(){return respType;}publicvoid setResptype(String v){ respType = v;}publicbyte[] getResult(){return resp;}protectedbyte[] 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. * */publicstaticvoid 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 themMethod[] methods = myclass.getMethods();Object object = myclass.newInstance();Class partypes[]=newClass[1]; partypes[0]=Object.class;Method meth = myclass.getMethod("initLicense", partypes);if(null!= meth){//System.out.println("Calling " + meth.getName());Object arglist[]=newObject[1]; arglist[0]= obj; meth.invoke(object, arglist);}}catch(ClassNotFoundException cnfe){// Ignore}catch(Exception ex){ ex.printStackTrace();}}publicbyte[] getImgBytes(){return imgBytes;}}
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="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:compositiontemplate="/WEB-INF/includes/templates/page-template.xhtml"><ui:definename="pageTitle"> Recognize BarCode - Aspose.BarCode for Java Demos</ui:define><ui:definename="page-content"><ui:decoratetemplate="/WEB-INF/includes/templates/tabbed_container.xhtml"><ui:definename="example"><pclass="componentDescriptionTxt">Welcome to the <STRONG>Aspose.BarCode for Java</STRONG> Featured Demos!</p><pclass="componentDescriptionTxt">When this demo runs without a license in evaluation mode, only Code39 types of barcodes will be detected.</p><pclass="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><listyle="text-align: left"><ahref="http://www.aspose.com/documentation/java-components/aspose.barcode-for-java/introducing-barcode-recognition.html">BarCode Recognition with Aspose.BarCode for Java</a></li><listyle="text-align: left"><ahref="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><tdalign="left"> Browse and select an image file to upload</td><tdwidth="10"></td><td><!-- <ui:include src="/WEB-INF/includes/templates/barcode-read-types.xhtml" /> --></td></tr><tr><tdcolspan="3"align="left"><ice:inputFileid="inputFileComponent"actionListener="#{recognize.uploadActionListener}"/><ice:messagefor="inputFileComponent"/></td></tr><tr><tdalign="left"colspan="3"><ice:outputLabelvalue="#{recognize.strCodetext}"/></td></tr></table><palign="left"><br></br></p></ui:define></ui:decorate></ui:define></ui:composition></html>