Home/Parse data from scanned image

Parse data from scanned image

Published On: 20 April 2017.By .
  • Mobile

 

Sometimes, Scanning the files can be core requirements of Android platform.Scanning of files involves reading the data from a file, processing the data for some other purposes, perform operations on data, It can useful for other useful operations also like to send emails, to send a message, to make calls if the data contains emails, phone etc.

The article is based on reading the excel file which contains contact no of people and to make calls with just single click.Follow the below steps to perform the same.

  1. First Download the Apache POI – Java API To Access Microsoft Format Files from the following URL

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.poi/poi/3.7

  1. Copy-paste the jar file into the libs directory of the project folder.
  1. Add the dependencies into build.gradle to compile copied jar file.
  compile fileTree(dir: 'libs', include: ['*.jar'])
  1. Access the file using the following code
File selectedFile = new File("selectedFilePath");
  1. Convert the excel file into HSSFSheet for next processing
    FileInputStream myInput = new FileInputStream(selectedFile);
    
    // Create a POIFSFileSystem object
    POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
    
    // Create a workbook using the File System
    HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
    
    // Get the first sheet from workbook
    HSSFSheet mySheet = myWorkBook.getSheetAt(0);
    
    /** We now need something to iterate through the cells.**/
    Iterator<Row> rowIter = mySheet.rowIterator();
    
    

    6.Read values from each row and each cell using the following code

ArrayList<ExcelData> excelDataRows = new ArrayList<>(); // it is used to denote all the data of excel
ExcelData excelData = new ExcelData(); // it is used to denote all the data of single row of excel
ArrayList<CellValueObject> cellValueObjects = new ArrayList<>(); // it is used to denote data of all cells of single row
CellValueObject cellValueObject; // it is used to denote data of single cell

 

int i = 0;     int j = 0;
    while (rowIter.hasNext()) {
        HSSFRow myRow = (HSSFRow) rowIter.next();
        Iterator<Cell> cellIter = myRow.cellIterator();
        cellValueObjects = new ArrayList<>();
        while (cellIter.hasNext()) {

            HSSFCell myCell = (HSSFCell) cellIter.next();
            String str = myCell.toString();
            cellValueObject = new CellValueObject(); 
            cellValueObject.setValue(str);
            cellValueObjects.add(cellValueObject);
        }
        excelData = new ExcelData();
        excelData.setCellValueObjects(cellValueObjects);
        //   cellValueObjects.clear();
        excelDataRows.add(excelData);
    }
} catch (Exception e) {
    e.printStackTrace();
}


ExcelData class and CellValueObject class are as  follows

public class ExcelData {

    ArrayList<CellValueObject> cellValueObjects = new ArrayList<>();

    public ArrayList<CellValueObject> getCellValueObjects() {
        return cellValueObjects;
    }

    public void setCellValueObjects(ArrayList<CellValueObject> cellValueObjects) {
        this.cellValueObjects = cellValueObjects;
    }
}

 

public class CellValueObject {

    String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

 

  1. Now, these data can be arranged into listview of Layout with the help of Custom Adapter (Which extends BaseAdapter) of ListView and on Item Click, any type of events( like phone call, email, message if excel contains phone no/email etc.) can be achieved according to requirement.

 

Related content

We Love Conversations

Say Hello
Go to Top