I'm trying to filter out the contours in an image. My code has no syntax errors but I just get a lot of red text when I run my program. I have located the point of failure to be cvInRangeS.
cvInRangeS(imghsv,minc,maxc,imgbin);
You can see this with the println statments only making it to "thourgh3"
square.jpg is in the project directory so that shouldn't be the problem if that helps.
The console returnes
Through 1
Through 2
Through 3
OpenCV Error: Assertion failed (src1.size == dst.size && dst.type() == CV_8U) in cvInRangeS, file ..\..\..\..\opencv\modules\core\src\arithm.cpp, line 2972
Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\core\src\arithm.cpp:2972: error: (-215) src1.size == dst.size && dst.type() == CV_8U in function cvInRangeS
at com.googlecode.javacv.cpp.opencv_core.cvInRangeS(Native Method)
at opencv2.OpenCV2.main(OpenCV2.java:50)
Java Result: 1
The full code is as follows
package opencv2;
/*There are import statments here but for the sake of space I have left them out :D*/
public class OpenCV2 {
public static void main(String[] args) {
IplImage img1;
IplImage imghsv;
IplImage imgbin;
CvScalar minc = cvScalar(95,150,75,0), maxc = cvScalar(145,255,255,0);
CvSeq contour1 = new CvSeq(), contour2;
CvMemStorage storage = CvMemStorage.create();
double areaMax = 1000, areaC = 0;
System.out.println("Through 1");
img1 = cvLoadImage("square.jpg");
imghsv = cvCreateImage(cvGetSize(img1),8,3);
imgbin = cvCreateImage(cvGetSize(img1),8,3);
System.out.println("Through 2");
cvCvtColor(img1,imghsv,CV_BGR2HSV);
System.out.println("Through 3");
cvInRangeS(imghsv,minc,maxc,imgbin);
System.out.println("Through 4");
cvFindContours(imgbin,storage,contour1,Loader.sizeof(CvContour.class),
CV_RETR_LIST, CV_LINK_RUNS,cvPoint(0,0));
contour2 = contour1;
System.out.println("Through 5");
while(contour1 != null && !contour1.isNull()){
areaC = cvContourArea(contour1,CV_WHOLE_SEQ,1);
if(areaC > areaMax){
areaMax = areaC;
}
contour1 = contour1.h_next();
}//end of while
while(contour2 != null && !contour2.isNull()){
areaC = cvContourArea(contour2,CV_WHOLE_SEQ,1);
System.out.println("Through 6");
if(areaC < areaMax){
cvDrawContours(imgbin,contour2,CV_RGB(0,0,0),CV_RGB(0,0,0),
0,CV_FILLED,8,cvPoint(0,0));
}//end of if
System.out.println("Through 7");
contour2 = contour2.h_next();
}//end of while2
System.out.println("Through 8");
cvShowImage("Color",img1);
cvShowImage("CF",img1);
cvWaitKey();
cvReleaseImage(img1);
cvReleaseImage(imghsv);
cvReleaseImage(imgbin);
cvReleaseMemStorage(storage);
}//end of main
}//end of class
cvInRangeS() assumes the type of the input image to be CV_8U, so you have to convert it first.
...
cvtColor(imghsv, grayscale, CV_BGR2GRAY );
cvInRangeS(grayscale,minc,maxc,imgbin);
...
Thanks for your help. the problem was in this line I have it set to a 3 channel image "3"'
imgbin = cvCreateImage(cvGetSize(img1),8,3);
It should be a binary image.
imgbin = cvCreateImage(cvGetSize(img1),8,1);
Related
I am running template matching using openCV 3.4.7 Android SDK (java).
The code work almost perfectly; when the template is match, it draws a rectangle on the matching area. The problem is that even when there is no match, it draws a random rectangle. I think that happens because the threshold is not set correctly. If so, can someone please help me out?
Here's the code:
public static void run(String inFile, String templateFile, String outFile,
int match_method) {
Mat img = Imgcodecs.imread(inFile);
Mat templ = Imgcodecs.imread(templateFile);
// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
// / Localizing the best match with minMaxLoc
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF
|| match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
// / Show me what you got
Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 0, 128));
// Save the visualized detection.
System.out.println("Writing " + outFile);
Imgcodecs.imwrite(outFile, img);
}
You can use Imgproc.TM_CCOEFF_NORMED or Imgproc.TM_CCORR_NORMED and mmr.maxVal >= 0.8. It should take care of most of your false positives.
Sample Code:
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.io.File;
import java.nio.file.Files;
public class templateMatchingTester {
private static String str = null;
static {
if (str == null) {
str = "initialised";
nu.pattern.OpenCV.loadShared();
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
private static Mat createMatrixFromImage(String imagePath) {
Mat imageMatrix = Imgcodecs.imread(imagePath);
Mat greyImage = new Mat();
Imgproc.cvtColor(imageMatrix, greyImage, Imgproc.COLOR_BGR2GRAY);
return greyImage;
}
private static boolean matchTemplate(String pathToInputImage,String pathToTemplate){
Mat inputImage = createMatrixFromImage(pathToInputImage);
Mat templateImage = createMatrixFromImage(pathToTemplate);
// Create the result matrix
int result_cols = inputImage.cols() - templateImage.cols() + 1;
int result_rows = inputImage.rows() - templateImage.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_8UC1);
int match_method;
match_method = Imgproc.TM_CCOEFF_NORMED;//Imgproc.TM_CCORR_NORMED;
Imgproc.matchTemplate(inputImage, templateImage, result, match_method);
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
double minMatchQuality = 0.85;
System.out.println(mmr.maxVal);
if (mmr.maxVal >= minMatchQuality){
return true;
} else
return false;
}
public static void main(String args[]) {
String template = "path/to/your/templateImage";
final File folder = new File("path/to/your/testImagesFolder/");
int matchCount = 0;
for (final File fileEntry : folder.listFiles()){
if (matchTemplate(fileEntry.getPath(),template)){
matchCount+=1;
}else
System.out.println(fileEntry.getPath());
}
System.out.println(matchCount);
}
}
Use a normed match method to ensure your match value is [0..1].
Replace this line
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
with a thresholding operation. Otherwise a best match of 0.9 would become 1 by the second normalization and you would lose the actual match "quality" information.
Normalizing the result of the template matching will always result in your best match being 1 making it impossible to discard a bad match.
i wrote an app that would take a screenshot of the game overwatch and attempt to tell who is on each team. using template matching and open cv. project need to iterate over the result image and check values.
OpenCVUtils.getPointsFromMatAboveThreshold(result,
0.90f)
public static void scaleAndCheckAll(String guid){
Mat source = imread(IMG_PROC_PATH + guid); //load the source image
Mat scaledSrc = new Mat(defaultScreenshotSize, source.type());
resize(source, scaledSrc, defaultScreenshotSize);
Mat sourceGrey = new Mat(scaledSrc.size(), CV_8UC1);
cvtColor(scaledSrc, sourceGrey, COLOR_BGR2GRAY);
for (String hero : getCharacters()) {
Mat template = OpenCVUtils.matFromJar(TEMPLATES_FOLDER + hero + ".png", 0); //load a template
Size size = new Size(sourceGrey.cols()-template.cols()+1, sourceGrey.rows()-template.rows()+1);
Mat result = new Mat(size, CV_32FC1);
matchTemplate(sourceGrey, template, result, TM_CCORR_NORMED);// get results
Scalar color = OpenCVUtils.randColor();
List<Point> points = OpenCVUtils.getPointsFromMatAboveThreshold(result,
0.90f);
for (Point point : points) {
//rectangle(scaledSrc, new Rect(point.x(),point.y(),template.cols(),template.rows()), color, -2, 0, 0);
putText(scaledSrc, hero, point, FONT_HERSHEY_PLAIN, 2, color);
}
}
String withExt = IMG_PROC_PATH + guid +".png";
imwrite(withExt, scaledSrc);
File noExt = new File(IMG_PROC_PATH + guid);
File ext = new File(withExt);
noExt.delete();
ext.renameTo(noExt);
}
the other method.
public static List<Point> getPointsFromMatAboveThreshold(Mat m, float t){
List<Point> matches = new ArrayList<Point>();
FloatIndexer indexer = m.createIndexer();
for (int y = 0; y < m.rows(); y++) {
for (int x = 0; x < m.cols(); x++) {
if (indexer.get(y,x)>t) {
System.out.println("(" + x + "," + y +") = "+ indexer.get(y,x));
matches.add(new Point(x, y));
}
}
}
return matches;
}
you can just get the first from the list or see how close they are if you expect multiple matches.
I'm working on a Java assignment for school. The assignment is to deal with 2 files reading the first, using the second to make adjustments on the first, and finally, outputing into a new file.
Scanner inputRecords = new Scanner(new File("Records.txt"));
Scanner inputTransactions = new Scanner(new File("Transactions.txt"));
BufferedWriter outputFile = (new BufferedWriter(new FileWriter("NewRecords.txt", true)));
char code; // transaction code
String lineTran = "";
String lineRecord = "";
String midS = "";
String tidS = "";
int tid = 0, mid= 0;
for (int x = 0; x < 6; x++)
{
lineTran = inputTransactions.nextLine();
code = lineTran.charAt(0);
System.out.println(code);
tidS = lineTran.substring(2,11);
tid = Integer.parseInt(tidS);
lineRecord = inputRecords.nextLine();
midS = lineRecord.substring(0,9);
mid = Integer.parseInt(midS);
if (mid < tid) // add a new record lineTran.substring(2,lineTran.length()
{
outputFile.write(lineRecord);
outputFile.newLine();
lineRecord = inputRecords.nextLine();
}
else if (mid == tid )
{
if (code == 'C') //change record
{
outputFile.write(lineTran.substring(2,lineTran.length()));
outputFile.newLine();
lineTran = inputTransactions.nextLine();
}
else if (code == 'D') //delete record
{
lineTran = inputTransactions.nextLine();
lineRecord = inputRecords.nextLine();
}
else // add error
{
System.out.println(lineRecord + " Already Exists...add error");
lineTran = inputTransactions.nextLine();
}
}
else
{
if (code == 'A') // add record
{
outputFile.write(lineTran.substring(2,lineTran.length()));
outputFile.newLine();
lineTran = inputTransactions.nextLine();
}
else if (code == 'C') // change error
{
System.out.println(lineRecord + " Record already exist...Change Error");
lineTran = inputTransactions.nextLine();
}
else // delete error
{
System.out.println(lineRecord + " Record does not exist...delete error");
lineTran = inputTransactions.nextLine();
}
}
Note that:
Records.txt has 10 lines of information (example: ######### lastname firstname occupation)
Transactions.txt has 6 lines of information (example: 'A,D,or C' ######### lastname firstname occupation)
The issue I'm having is no matter the type of loop i run i reach one of 2 deadends.
1) in the case of the for loop above
D
A
C
A
C
386326383 Slim Kan personalTrainer No changes were found...Change Error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at fileHandling.main(fileHandling.java:26)
is the outcome and nothing writen to file.
2) If I run for loop through x<5 program runs fine and, however, skips the last transaction.
I tried "do while" and "while" loops but only got similar results. any suggestions?
On the if condition you have assign lineTran = inputTransactions.nextLine(); then on the top of for loop you assign it again. Means that you are dismiss 1 line every loop. So that, in transaction file you have 6 lines, then you loop will run more loop than the number of line in the file because of you have read .nextLine() more than the number of lines.
please i try to make a new application in java for matching picture and video, the matching of template in picture working fine , but when i try to do it for video i always have this error message :
OpenCV Error: Assertion failed ((depth == CV_8U || depth == CV_32F) &&
type == _templ.type() && _img.dims() <= 2) in cv::matchTemplate, file
C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\templmatch.cpp,
line 1062 Exception in thread "main" CvException
[org.opencv.core.CvException: cv::Exception:
C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\templmatch.cpp:1062:
error: (-215) (depth == CV_8U || depth == CV_32F) && type ==
_templ.type() && _img.dims() <= 2 in function cv::matchTemplate ]
This is my function for matching video with picture , someone can help please .
public int runVedio(String inFile, String templateFile, int match_method) {
int nbr = 0;
Mat templ = Imgcodecs.imread(templateFile);
VideoCapture capture=new VideoCapture(inFile);
Mat frame = new Mat();
Mat result = new Mat();
capture.read(frame);
// / Do the Matching and Normalize
Imgproc.matchTemplate(frame,templ, result, match_method);
Imgproc.threshold(result, result,0.9,1,Imgproc.THRESH_TOZERO);
//Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
while(true)
{
// / Localizing the best match with minMaxLoc
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
if(mmr.maxVal > 0.98)
{
// / Show me what you got
Imgproc.rectangle(frame, matchLoc,
new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()),
new Scalar(0,255,0),2);
Imgproc.rectangle(result, matchLoc,
new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()),
new Scalar(0,255,0),-1);
nbr++;
}
else
{
return nbr;
}
}
}
make sure you are accessing the video correctly for that you can use :
while(camera.read(frame))
Since, it's a video you need to access all the frames in it so use while.
And also your result image i.e
Mat result = new Mat();
must go like below, so that both the images are of same size and are of same color code.
So change it to this ,
new Mat(frame.rows(), frame.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
Run the code and tell me weather it works..
okay guys im really really confused and all or any help is appropriated to the MAX!
EDIT: I am working on an assignment that deals with reading data from a text file, and parsing that data to various arrays. i am stuck on parsing, keep receiving a floating decimal error.
This is my first time ever using fileread. im trying to extract the hours and wage from employees and calculate their gross and net pay. Then i wanted to display, in alphabetical order, a managers report of all their information plus my calculated information...
i have efficiently wrote a program that displays their information. i cannot, for the life of me, find a way to parse this information to start applying my calculations. first let me show you what i have, then i will show you what i am doing and the results.
here is what i have:
import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class T2 {
public static void main(String[] args)
{
String list = "";//creates a list for output GUI
System.out.println("Reading File ......");
//Name of the file with precise directory
//String fileName="data.txt"
String fileName="G:\\cs215\\assign 2\\FileRead\\READTHIS.txt"; //turns a file into string
//tring fileName="C:\\cs130\\Max_names.txt"; //turns a file into string on my PC
//Arrays to store variables for each person
String [] extra = new String [11];
String [] Name = new String [11];
String [] familyName = new String [11];
String [] FName = new String [11];
String [] credit = new String [11];
String [] hr = new String [11];
String [] wg = new String [11];
Double [] wage = new Double [11];
Double [] hrworked = new Double [11];
try{//Try tests the code inside of it for errors.
//Create object of FileReader
FileReader inputFile = new FileReader(fileName);
//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);
//Variable to hold the one line data
String line = null;
// Read file line by line and print on the console
int row = 0;
while ((line = bufferReader.readLine()) != null) {
extra[row]= line;
row++;
}
//Close the buffer reader
bufferReader.close();
//This loop contains and seperates each line into individual words
for (int y = 0;y<=9;y++)
{
//seperates the first word in each line from the other ones.
int u = 0;
while(u < extra[y].length())
{
if(extra[y].charAt(u) == ',')//Determines the point at which the words are seperated
{
//Splits one word and the remaining words into two variables
Name[y] = extra[y].substring(0, u);
familyName[y] = extra[y].substring(u + 1, extra[y].length());
u = extra[y].length();
}
++u;
}
//seperates the second word in each line from the other ones.
int a = 0;
while(a < familyName[y].length())
{
if(familyName[y].charAt(a) == ',')//Determines the point at which the words are seperated
{
//Splits one word and the remaining words into two variables
FName[y] = familyName[y].substring(0, a);
credit[y] = familyName[y].substring(a + 1, familyName[y].length());
a = familyName[y].length();
}
++a;
}
//Puts the words back together in re-sorted order
extra[y] = ("" +FName[y] +", "+Name[y]+", " +credit[y]+ "");
}//end of sorting and resorting loop
//places each name in string.
String[] arr = {extra[0], extra[1], extra[2], extra[3],extra[4], extra[5], extra[6], extra[7], extra[8], extra[9]};
//rearanges the string in alphabetical order from top to bottom based on last name
String tmp;
for (int i = 0;i < arr.length;i++)
{
tmp = arr[i];
for (int j = 0;j < arr.length;j++)
{
if (i == j) continue;
int x = tmp.compareTo(arr[j]);
if (x < 0)
{
tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
//acquires the current date
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
for (int r=0;r<10;r++)
{
int pl = 0;
while(pl < arr[r].length())
{
if(arr[r].charAt(pl) == ',')//Determines the point at which the words are seperated
{
//Splits one word and the remaining words into two variables
familyName[r] = arr[r].substring(0, pl);
Name[r] = arr[r].substring(pl + 1, arr[r].length());
pl = arr[r].length();
}
++pl;
}
//seperates the second word in each line from the other ones.
int pr = 0;
while(pr < Name[r].length())
{
if(Name[r].charAt(pr) == ',')//Determines the point at which the words are seperated
{
//Splits one word and the remaining words into two variables
FName[r] = Name[r].substring(0, pr);
credit[r] = Name[r].substring(pr + 1, Name[r].length());
pr = Name[r].length();
}
++pr;
}
//Seperates the last two words in each line
int pz = 0;
while(pz < credit[r].length())
{
if(credit[r].charAt(pz) == ',')//Determines the point at which the words are seperated
{
//Splits one word and the remaining words into two variables
hr[r] = credit[r].substring(0, pz);
wg[r] = credit[r].substring(pz + 1, credit[r].length());
pz = credit[r].length();
}
++pz;
}}
//HERE IS WHERE I AM TRYING TO PARSE THE HOURS AND WAGE FROM MY DOC
//Prompts the JPanel
JLabel tran = new JLabel("Date: " + dateFormat.format(cal.getTime()) + "");
JFrame app = new JFrame("Manager Report");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Places all the variables in their correct spots
tran.setBounds(0, 10, 200, 20); // x, y, width, height x across, y down
JLabel tlast = new JLabel("Last Name:");
tlast.setBounds(0, 30, 200, 20);
JLabel tfirst = new JLabel("First Name:");
tfirst.setBounds(200, 30, 200, 20);
JLabel thour = new JLabel("Work hours:");
thour.setBounds(400, 30, 200, 20);
JLabel twage = new JLabel("Wage:");
twage.setBounds(600, 30, 200, 20);
JLabel Fix = new JLabel("");//This was added to make JPanel stop glitching
Fix.setBounds(1400, 30, 200, 20);
/*Goes throughthe list of people and places all the variables in their
correct spots. Note that each line output is lower than before.
*/
for (int t=0; t<=9;t++)
{
JLabel displaylname = new JLabel(""+familyName[t] +"");
displaylname.setBounds(0, 50+(20*t), 200, 20);
JLabel displayfname = new JLabel(""+FName[t]+"");
displayfname.setBounds(200, 50+(20*t), 200, 20);
JLabel displayhours = new JLabel(""+hr[t]+"hrs");
displayhours.setBounds(400, 50+(20*t), 200, 20);
JLabel displaywage = new JLabel(""+wg[t]+"$ per hr");
displaywage.setBounds(600, 50+(20*t), 200, 20);
//The required windows that allow output in the loop
app.add(displaylname);
app.add(displayfname);
app.add(displayhours);
app.add(displaywage);
}
//The required windows that allow output
app.add(tran);
app.add(tlast);
app.add(tfirst);
app.add(thour);
app.add(twage);
app.add(Fix);
app.setSize(800, 300);
app.setVisible(true);
//window.setLayout(null);
app.setResizable(false);
}//End of try
//catches the program, terminates it, and sends an error message if an error is encountered
catch(IOException | HeadlessException e){
System.out.println("Error while reading file line by line:" + e.getMessage());
}
}
}
the text doc im extracting from looks like this, firstname,last,hours,wage:
Sukken,Dey,45.0,75
Will,Duke,40,80
Adam,Kauffman,43,72
Shaun,Milson,40,65
Daniel,Jamestone,35,60
Matthew,Olberg,40,72
Andrew,Johnson,45,75
Arron,Winsen,42,70
Eric,Winsen,40,65
Mark,Dinger,45,75
im trying to parse the information into the double arrays i have defined at the top. I'm implementing these lines of code right before my promps for jpanel. i commented where.
for(int m=0; m<11; m++)
{
hrworked[m]=Double.parseDouble(hr[m]);
wage[m]=Double.parseDouble(wg[m]);
System.out.println(wage[m]);
}
i even placed a print line statement to see if the parses are working and it seems like they are until they hit an error. the output results in
run:
Reading File ......
75.0
Exception in thread "main" java.lang.NullPointerException
75.0
80.0
60.0
75.0
72.0
65.0
72.0
70.0
65.0
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at t2.T2.main(T2.java:147)
C:\Users\Adamska\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
it appears to be parsing because it prints out values for each line but it results in an error and im not sure what the cause is. is this an appropriate approach or should i be using something else to parse my values? if someone could explain what is going on and point me in what i need to do id greatly appreciate it
I have Java code that converts CSV to xlsx. It works fine with a small file size. Now I have a CSV file with 2 lakh records (200,000) and on conversion I am getting an out of memory error.
I tried changing the workbook to SXSSFWorkbook and increasing the heap size and Java memory size to -Xms8G -Xmx10G. Even this did not work. I tried it on a UNIX box.
On searching, I got some code about using BigGridDemo. Can anyone help me in customizing that to reading a .csv file and then using its logic to write to xlsx, or any other solution?
try { FileReader input = new FileReader(strFileToConvert);
BufferedReader bufIn = new BufferedReader(input);
if(strExcel_Typ.equals("-xls"))
{
wbAll = new HSSFWorkbook();
}
else
{
wbAll = new SXSSFWorkbook(100);
wbAll.setCompressTempFiles(true);
}
sheetAll = wbAll.createSheet();
rowAll = null;
cellAll = null;
shoRowNumAll = 0;
// do buffered reading from a file
while ((line = bufIn.readLine()) != null)
{
intCntr++;
//if there is any data in the line
if (line.length() > 0)
{
System.out.println(shoRowNumAll);
//create a new row on the spreadsheet
rowAll = sheetAll.createRow((int)shoRowNumAll);
shoRowNumAll++;
if (line.indexOf("\"", 0) > 0)
{
if (intCntr == 1)
{
//only issue the message the first time quotes are found
System.out.println("Double quotes found. Stripping double quotes from file");
}
line = line.replaceAll("\"", "");
}
//if its the first row and no delimiters found, there is a problem
if (line.indexOf(strDelim, 0) == -1 && intCntr == 1)
{
System.exit(1);
}
processLine(line);
((SXSSFSheet) sheetAll).flushRows(100);
}
}
bufIn.close();
/write the excel file
try
{ String file = strOutPutFile;
ExcelOutAll = new FileOutputStream(file);
wbAll.write(ExcelOutAll);
}
catch (IOException e)
{
System.err.println(e.toString());
}
ExcelOutAll.close();
Processline Method:
processLine(String line)
{
//find the first next delimiter starting in position 0
intNxtComma = line.indexOf(strDelim, 0);
while (intCurPosInLine < line.trim().replaceAll(",","").length())
{
strCellContent = line.substring((intCurPosInLine), intNxtComma);
//create a new cell on the new row
cellAll = rowAll.createCell(intCellNum);
//set the font defaults
Font font_couriernew_10 = wbAll.createFont();
font_couriernew_10.setFontHeightInPoints((short)10);
font_couriernew_10.setFontName("Courier New");
CellStyle cellStyle = wbAll.createCellStyle();
//if its the first row, center the text
if (shoRowNumAll == 1)
{
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
font_couriernew_10.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
}
cellStyle.setFont(font_couriernew_10);
// if the col. needs to be numeric, set the cell format to number
if ((strNumericCols.indexOf(Integer.toString(intCellNum), 0) > -1) && (intCntr > 1))
{
DataFormat datafrmt = wbAll.createDataFormat();
cellStyle.setDataFormat(datafrmt.getFormat("$#,##0.00"));
}
cellAll.setCellStyle(cellStyle);
//populate the cell
if ((strNumericCols.indexOf(Integer.toString(intCellNum), 0) > -1) && (intCntr > 1))
{
//if the col. needs to be numeric populate with a number
if(strCellContent != null && !"".equals(strCellContent.trim())){
douCellContent = Double.parseDouble(strCellContent.replaceAll(",",""));
cellAll.setCellValue(douCellContent);
}
}
else
{
cellAll.setCellValue(strCellContent.trim());
}
intCellNum++;
intCurPosInLine = intNxtComma + 1;
//if we dont find anymore delimiters, set the variable to the line length
if (line.indexOf(strDelim, intCurPosInLine) == -1)
{
intNxtComma = line.trim().length();
}
else
{
intNxtComma = line.indexOf(strDelim, intNxtComma + 1);
}
}
}