Parse data from scanned image
- Mobile
Parse data from scanned image
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.
- 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
- Copy-paste the jar file into the libs directory of the project folder.
- Add the dependencies into build.gradle to compile copied jar file.
1 |
compile fileTree(dir: 'libs', include: ['*.jar']) |
- Access the file using the following code
1 |
File selectedFile = new File("selectedFilePath"); |
- Convert the excel file into HSSFSheet for next processing
12345678910111213FileInputStream myInput = new FileInputStream(selectedFile);// Create a POIFSFileSystem objectPOIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);// Create a workbook using the File SystemHSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);// Get the first sheet from workbookHSSFSheet mySheet = myWorkBook.getSheetAt(0);/** We now need something to iterate through the cells.**/Iterator<Row> rowIter = mySheet.rowIterator();
1 2 3 4 |
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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(); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public class ExcelData { ArrayList<CellValueObject> cellValueObjects = new ArrayList<>(); public ArrayList<CellValueObject> getCellValueObjects() { return cellValueObjects; } public void setCellValueObjects(ArrayList<CellValueObject> cellValueObjects) { this.cellValueObjects = cellValueObjects; } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public class CellValueObject { String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } } |
- 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
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s