Frame extractor - java

So, I've been trying to create a frame extractor but, obviously, it doesn't work. Here's what it look likes
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Scanner;
import java.sql.Time;
import javax.imageio.ImageIO;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
public class FrameExtract {
public static void main(String []args) throws Exception
{
System.out.println("Please enter the file name and path");
Scanner filepath = new Scanner(System.in);
String filename = filepath.nextLine();
File myObj = new File(filename);
System.out.println("Please enter the precise time of the frame you want to extract");
Scanner timeex = new Scanner(System.in);
Long timetoex = timeex.nextLong();
Time timestamp = new Time(timetoex);
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(myObj.getAbsoluteFile());
frameGrabber.start();
Frame f;
try {
Java2DFrameConverter c = new Java2DFrameConverter();
f = frameGrabber.grab();
BufferedImage bi = c.convert(f);
ImageIO.write(bi,"png", new File("D:\\img.png"));
frameGrabber.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
and the exceptions show this:
java.util.Scanner.throwFor(Scanner.java:864)
java.util.Scanner.next(Scanner.java:1485)
java.util.Scanner.nextLong(Scanner.java:2222)
java.util.Scanner.nextLong(Scanner.java:2182)
Eng.F.FrameExtract.main(FrameExtract.java:22)
I know there is problems like the code doesn't use the time properly, or even at all

Related

a program has an access on a file to read the current water level and give a warning when the water level is more than 15 meter

I don't know how to read the last line from the file so it will also be saved in waterlevel.
I have to make in addition to the code a strukogram.
import java.util.Scanner;
import java.io.FileWriter;
import java.ioException;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Dam{
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
FileWriter fw;
FileReader fr;
int waterLevel;
String text;
do {
System.out.println("give the current water level");
try {
fw = new FileWriter("waterLevel.txt");
text = sc.nextInt()+ "\n";
fw.write(text,0,text.length());
fw.flush();
fw.close;
} catch (IOException e) {
e.printStackTrace();
}
} while (waterLevel < 15);
System.out.println("warning");
}
}

Can i take a screenshot and paste it in a word file in appium using Java?

I'm currently using the below method to take screenshots and store them in a folder called 'Screenshots'. But what i want is, to take these screenshots and paste them in a word document according to the test cases to which they belong.
Is it possible? If so could somebody please guide me?
public String FailureScreenshotAndroid(String name) {
try {
Date d = new Date();
String date = d.toString().replace(":", "_").replace(" ", "_");
TakesScreenshot t = (TakesScreenshot)driver;
File f1 = t.getScreenshotAs(OutputType.FILE);//Temporary Location
String permanentLocation =System.getProperty("user.dir")+ "\\Screenshots\\"+name+date+".png";
File f2 = new File(permanentLocation);
FileUtils.copyFile(f1, f2);
return permanentLocation;
}catch (Exception e) {
String msg = e.getMessage();
return msg;
}
}
Try below:
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class TakeScreenshots {
public static void main(String[] args) {
try {
XWPFDocument docx = new XWPFDocument();
XWPFRun run = docx.createParagraph().createRun();
FileOutputStream out = new FileOutputStream("d:/xyz/doc1.docx");
for (int counter = 1; counter <= 5; counter++) {
captureScreenShot(docx, run, out);
TimeUnit.SECONDS.sleep(1);
}
docx.write(out);
out.flush();
out.close();
docx.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void captureScreenShot(XWPFDocument docx, XWPFRun run, FileOutputStream out) throws Exception {
String screenshot_name = System.currentTimeMillis() + ".png";
BufferedImage image = new Robot()
.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
File file = new File("d:/xyz/" + screenshot_name);
ImageIO.write(image, "png", file);
InputStream pic = new FileInputStream("d:/xyz/" + screenshot_name);
run.addBreak();
run.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, screenshot_name, Units.toEMU(350), Units.toEMU(350));
pic.close();
file.delete();
}
}

Java issue with reading a text file into an ArrayList

I have created a program that is supposed to read a text file for Integers and put them into an Arraylist, and then there are a bunch of methods to act on it. but, after some trouble shooting I am noticing that my program won't pull the integers from the text file in. Could anyone point me in the right direction?
package project1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.System;
public class Main {
public static void main(String[] Args) {
Main mainObject = new Main();
mainObject.run();
}
public void run() {
**ArrayList<Integer> list = new ArrayList<>();
String fileName = "p01-in.txt";
Scanner in = new Scanner(fileName);
while (in.hasNextInt()) {
list.add(in.nextInt());
int line = in.nextInt();
System.out.println("%s %n" + line);
}
in.close();**
ArrayList<Integer> listRunsUpCount = new ArrayList<>();
ArrayList<Integer> listRunsDnCount = new ArrayList<>();
Main findRuns = new Main();
listRunsUpCount = findRuns.FindRuns(list, 0);
listRunsDnCount = findRuns.FindRuns(list, 1);
ArrayList<Integer> listRunsCount = new ArrayList<>();
Main mergeRuns = new Main();
listRunsCount = mergeRuns.MergeRuns(listRunsUpCount,
listRunsDnCount);
Main Output = new Main();
Output.Output("p01-runs.txt", listRunsCount);
}
You can use BufferedReader to read file content line by line.
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line=reader.readLine();
while (line != null) {
line = reader.readLine();
list.add(Integer.parseInt(line.trim()));
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}

using Java arraylist for storing data from scan from file

I am new to java, but not coding. I am trying to figure out java because it's part of my class this term and I am having a really hard problem grasping the idea of it and implementing things in java.
my problem Is that I am not sure if I am correctly using the arraylist to grab data from the scan of the file and input it into a arraylist to sort and print at a later time. I am just having issues picking up on java any help would be great since I am new to java.
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.*;
public class MissionCount
{
private static ArrayList<String> list = new ArrayList<String>();
// returns an InputStream that gets data from the named file
private static InputStream getFileInputStream(String fileName) throws Exception {
InputStream inputStream;
try {
inputStream = new FileInputStream(new File(fileName));
}
catch (FileNotFoundException e) { // no file with this name exists
inputStream = null;
throw new Exception("unable to open the file -- " + e.getMessage());
}
return inputStream;
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("USage: MissionCount <datafile>");
//System.exit(1);
}
try {
System.out.printf("CS261 - MissionCount - Chad Dreher%n%n");
int crewcount = 0;
int misscount = 0;
InputStream log = getFileInputStream(args[0]);
Scanner sc = new Scanner(log);
sc.useDelimiter(Pattern.compile(",|\n"));
while (sc.hasNext()) {
String crewMember = sc.next();
list.add(crewMember);
String mission = sc.next();
list.add(mission);
}
sc.close();
// Add code to print the report here
}catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
InputStream log = getFileInputStream(args[0]);
Change that line to as follows :-
File log = new File(args[0])
that should work!

Trying to append a text file using java printwriters

So I have a few other classes like this one, I call the method in using an object in the run file. I want to write every output of every class into the same text file. However at the moment only one output is being saved to the text file, as it is overwriting each time, how do I do this using a print writer seen below?
Any guidance is much appreciated!
Class:
package cw;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class LineCounter {
public static void TotalLines() throws IOException {
Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\Sam\\Desktop\\Report.txt"));
int linetotal = 0;
while (sc.hasNextLine()) {
sc.nextLine();
linetotal++;
}
out.println("The total number of lines in the file = " + linetotal);
out.close();
System.out.println("The total number of lines in the file = " + linetotal);
}
}
Run File:
package cw;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class TextAnalyser {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
LineCounter Lineobject = new LineCounter();
WordCounter Wordobject = new WordCounter();
NumberCounter Numberobject = new NumberCounter();
DigitCounter Digitobject = new DigitCounter();
SpaceCounter Spaceobject = new SpaceCounter();
NumberAverage Noavgobject = new NumberAverage();
WordAverage Wordavgobject = new WordAverage();
Palindromes Palindromeobject = new Palindromes();
VowelCounter Vowelobject = new VowelCounter();
ConsonantCounter Consonantobject = new ConsonantCounter();
WordOccurenceTotal RepeatsObject = new WordOccurenceTotal();
Lineobject.TotalLines();
Wordobject.TotalWords();
Numberobject.TotalNumbers();
Digitobject.TotalDigits();
Spaceobject.TotalSpaces();
Noavgobject.NumberAverage();
Wordavgobject.WordAverage();
Vowelobject.TotalVowels();
Consonantobject.TotalConsonant();
Palindromeobject.TotalPalindromes();
//RepeatsObject.TotalRepeats();
}
}
You want to use the second argument of the FileWriter constructor to set the append mode:
new FileWriter("name_of_your_file.txt", true);
instead of:
new FileWriter("name_of_your_file.txt");

Categories

Resources