Macro recorder/player - java

I am making a sort of Macro recorder/player
I have done the player part with utils such as java.awt.Robot() which emulates basic human mouse/keyboard output commands, reading an XML file.
I am stuck at the part where I have to record that XML file.
I have no idea of which Class I can use to do the opposite of Robot() If yo have any FemaleRobot() for me I would be very happy :D
The only thing in this direction I have so far is :
while (true) {
Point pos = MouseInfo.getPointerInfo().getLocation();
System.out.println(pos.x+" x "+pos.y);
}
which is not much and not really what I want ^_^, I don't know how to use a Mouse/KeyListener since it would require a Component. If it is the only way, what Compoment do I use as I don't want any graphical java implementation? Should I create a phony Component anyway? Which one?
e.g. I want my recorder to write in the XML how I click on my ubuntu desktop or press enter on firefox.
I guess it's clear, if not I will be checking the answers a lot. Have a nice day and thanks for reading this.

Try jnativehook lib:
http://code.google.com/p/jnativehook/wiki/examples
It is very easy to use and may meet your needs.

I regret to inform you it is completely impossible to monitor mouse clicks and keystrokes outside of your form. Java events simply do not fire outside the scope of your form.
The reason java behaves this way is to eliminate the possibility of java based malware attempting to steal sensitive data.

You can find examples of mouse listeners here: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
to Write to a file you can do something like:
FileWriter fileStream = new FileWriter("myfile.extention");
BufferedWriter out = new BufferedWriter(fileStream);
out.write("pos.x+" x "+pos.y");
If you are doing this:
while (true)
{
Point pos = MouseInfo.getPointerInfo().getLocation();
System.out.println(pos.x+" x "+pos.y);
}
You will want to add a Thread.Sleep call in the loop.
Then you can read the file back by doing:
FileInputStream fileStream = new FileInputStream("myfile.extention");
DataInputStream in = new DataInputStream(fileStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in))
String position = br.readLine()
Then you can parse that string to get the values.
So you might do something like:
FileWriter filewrite = new FileWriter("myfile.txt");
BufferedWriter out = new BufferedWriter(filewrite);
while (Recording)
{
Point pos = MouseInfo.getPointerInfo().getLocation();
out.write(pos.x + " " + pos.y);
Thread.sleep(50);
}
FileInputStream fileStream = new FileInputStream("myfile.txt");
DataInputStream in = new DataInputStream(fileStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in))
String position;
while(position = br.readLine() != null)
{
String[] positions = position.split(" ");
int x = Integer.parseInt(positions[0]);
int y = Integer.parseInt(positions[1]);
}
You will have to use the mouse events to write click positions to the file.

You can first get the resolution of screen.Second thing make a frame that is a size of your screen and make its transparency to 0% .When you click to a position on a screen put down your jframe and use mouse robot to click a position.You can get the coordinates of the entire screen.This way you can record the mouse clicks,But the problem comes is when you need to type in the text

Related

How can I get my Stream Key through YouTube's Live API?

I use my own encoder to stream the video. When i stream i have to keep going back onto YouTube to change the selected stream key to use the first one that was created. How can i change or make sure that the YoutTube API uses the original stream key and not generate a new one.
I have tried using list, update and transition but none of there response give the stream key or allows me to change the stream key being used
title = getStreamTitle();
System.out.println("You chose " + title + " for stream title.");
// Create a snippet with the video stream's title.
LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
streamSnippet.setTitle(title);
// Define the content distribution network settings for the
// video stream. The settings specify the stream's format and
// ingestion type. See:
// https://developers.google.com/youtube/v3/live/docs/liveStreams#cdn
CdnSettings cdnSettings = new CdnSettings();
cdnSettings.setFormat("1080p");
cdnSettings.setIngestionType("rtmp");
cdnSettings.getIngestionInfo();
LiveStream stream = new LiveStream();
stream.setKind("youtube#liveStream");
stream.setSnippet(streamSnippet);
stream.setCdn(cdnSettings);
// Construct and execute the API request to insert the stream.
YouTube.LiveStreams.Insert liveStreamInsert =
youtube.liveStreams().insert("snippet,cdn", stream);
LiveStream returnedStream = liveStreamInsert.execute();
This is what I am currently doing to, but this create a new stream for the YouTube broadcast event. I do not want it to make a new stream but I need to return the key.
private static String getStreamTitle() throws IOException {
String title = "";
System.out.print("Please enter a stream title: ");
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
title = bReader.readLine();
if (title.length() < 1) {
// Use "New Stream" as the default title.
title = "New Stream";
}
return title;
}
I either expect the output to give me a stream key which I can add to my encoder or be able to directly make it use the previous stream so the reusable stream key stays the same.
For those who haven't seen OP's newer post, where they posted their answer, you can get the stream key using:
returnedStream.getCdn().getIngestionInfo().getStreamName();
How can I change the stream my event uses via the YouTube live api?

smartXLS - sheetRangeToImage not working

I have this problem using smartXLS library for Java
I try to export the sheets of a workbook as .png images using the method
workbook.sheetRangeToImage(row1,col1,row2,col2,file)
The code I use is as follows:
private static void takeValuePics() throws Exception {
WorkBook w = new WorkBook();
w.read(XLS_PATH + DIF_FILE_NAME);
int numSheets = w.getNumSheets();
String out;
for(int i=0;i<numSheets;i++) {
w.setSheet(i);
System.out.println(w.getNumber(1,1));
w.setPrintGridLines(true);
out = VAL_PATH + "values_" + w.getSheetName(i) + ".png";
w.sheetRangeToImage(0,0,LOOPS,3,out);
}
The constants are configured correctly and the file is read correctly.
(The println() prints correct values)
The .png files are created but they are completely empty! Just white rectangles.
Does anybody know what's wrong?
Problem solved by the fantastic SmartXLS support team!
I forward their answer:
You need set the print scale,it use the print scale value when exporting range to image
workBook.setPrintScale(100);
That was all!
Hope this helps people in the future.

LibGdx android accessing .txt files and turning them to Strings

This is a libgdx project using gradle if that matters. I have a class that implements screen where I want to draw a string to the display. on Desktop the following code works.
try {
string = readFile("bin/001.txt",Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
And then I can use that string in a Label for a stage. However on the android version I use "assets/001.txt" and get the error VFY: unable to resove static method 11542: Ljava/nio/file/Paths;.g
I then researched it and found a suggestion here
FileInputStream fis;
fis = openFileInput("test.txt");
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while ((n = fis.read(buffer)) != -1)
{
fileContent.append(new String(buffer, 0, n));
}
This did not work either because openFileInput is undefined because "Those are methods defined on the Context class" however
"If you are using LibGDX, you need to forego the concept of Activities and Views, since your entire game will now just be a single Activity. To have a main menu when you game starts up doesn't switch the Activity, but just presents a different set of objects to be rendered to the same Activity."
And now I'm lost
To read a file in LibGdx you need to abstract from the underlying. This is because the same application will work in several systems (Desktop, Android, Web, ...).
So to read a file you need to use:
FileHandle file = Gdx.files.internal("myfile.txt");
String text = file.readString();
The path and location of the files can be summarized in the documentation

Writing multiple files with Spring Batch

I'm a newbie in Spring Batch, and I would appreciate some help to resolve this situation: I read some files with a MultiResourceItemReader, make some marshalling work, in the ItemProcessor I receive a String and return a Map<String, List<String>>, so my problem is that in the ItemWriter I should iterate the keys of the Map and for each one of them generate a new file containing the value associated with that key, can someone point me out in the right direction in order to create the files?
I'm also using a MultiResourceItemWriter because I need to generates files with a maximum of lines.
Thanks in advance
Well, finaly got a solution, I'm not really excited about it but it's working and I don't have much more time, so I've extended the MultiResourceItemWriter and redefined the "write" method, processing the map's elements and writing the files by myself.
In case anyone out there needs it, here it is.
#Override
public void write(List items) throws Exception {
for (Object o : items) {
//do some processing here
writeFile(anotherObject);
}
private void writeFile (AnotherObject anotherObject) throws IOException {
File file = new File("name.xml");
boolean restarted = file.exists();
FileUtils.setUpOutputFile(file, restarted, true, true);
StringBuffer sb = new StringBuffer();
sb.append(xStream.toXML(anotherObject));
FileOutputStream os = new FileOutputStream(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, Charset.forName("UTF-8")));
bufferedWriter.write(sb.toString());
bufferedWriter.close();
}
And that's it, I want to believe that there is a better option that I don't know, but for the moment this is my solution. If anyone knows how can I enhance my implementation, I'd like to know it.

Parsing a text file on BlackBerry takes forever

I was originally using RIM's native xml parser methods to parse a 150k text file, approximately 5000 lines of xml, however it was taking about 2 minutes to complete, so I tried a line based format:
Title: Book Title Line 1 Line
2 Line 3
I should be able to read the file in less time than it takes to blink, but it is still slow.
Identifier books is a Vector of Book objects and lines are stored in a vector of strings in the Book object.
class classs = Class.forName("com.Gui.FileLoader");
InputStream is = classs.getResourceAsStream( fileName );
int totalFileSize = IOUtilities.streamToBytes( is ).length;
int totalRead = 0;
//Thought that maybe a shared input stream would be faster, in this case it't not.
SharedInputStream sis = SharedInputStream.getSharedInputStream( classs.getResourceAsStream( fileName ) );
LineReader lr = new LineReader( sis );
String strLine = new String( lr.readLine() );
totalRead += strLine.length();
Book book = null;
//Loop over the file until EOF is reached, catch EOF error move on with life after that.
while(1 == 1){
//If Line = Title: then we've got a new book add the old book to our books vector.
if (strLine.startsWith("Title:")){
if (book != null){
books.addElement( book );
}
book = new Book();
book.setTitle( strLine.substring( strLine.indexOf(':') + 1).trim() );
strLine = new String( lr.readLine() );
totalRead += strLine.length();
continue;
}
int totalComplete = (int) ( ( (double) totalRead / (double) totalFileSize ) * 100.00);
_observer.processStatusUpdate( totalComplete , book.getTitle() );
book.addLine( strLine );
strLine = new String( lr.readLine(), "ascii" );
totalRead += strLine.length();
}
For one thing, you're reading in the file twice - once for determining the size and then again for parsing it. Since you're already reading it into a byte array for determining the size, why not pass that byte array into a ByteArrayInputStream constructor? For example:
//Used to determine file size and then show in progress bar, app is threaded.
byte[] fileBytes = IOUtilities.streamToBytes( is );
int totalFileSize = fileBytes.length;
int totalRead = 0;
ByteArrayInputStream bais = new ByteArrayInputStream( fileBytes );
LineReader lr = new LineReader( bais);
This way it won't matter if the rest of the classes reading from the stream are reading a byte at a time - it's all in-memory.
It is easy to assume that all the operations you've elided from the code sample finish in constant time. I am guessing that one of them is doing something inefficiently, such as book.addLine( strLine ); or perhaps _observer.processStatusUpdate( totalComplete , book.getTitle() ); If those operations are not able to complete in constant time, then you could easily have a quadratic parsing algorithm.
Just thinking about the operations is the best way to figure it out, but if you're stumped, try using the BlackBerry profiler. Run your program in the Eclipse debugger and get it to stop at a breakpoint just before parsing. Then, in Eclipse, select 'window .. show view .. other .. BlackBerry .. BlackBerry Profiler View'
Select the 'setup options' button from the profiler view toolbar. It has a blue triangle in the icon. Set 'method attribution' to cumulative, and 'what to profile' to 'time including native methods'
then continue your program. once parsing is finished, you'll need to pause program execution, then click on the 'method' tab of the profiler view. You should be able to determine your pain point from there.
Where does the profiler say you spend your time?
If you do not have a preferred profiler there is jvisualvm in the Java 6 JDK.
(My guess is that you will find all the time being spent on the way down to "read a character from the file". If so, you need to buffer)
Try using new BufferedInputStream(classs.getResourceAsStream(fileName));
EDIT:
Apparently the documentation that says they have BufferedInputStream is wrong.
I am going to leave this wrong answer here just so people have that info (doc being wrong).

Categories

Resources