How to input data into external GUI using Java runtime exec - java

I am trying to write java code to access and use the DullRazor software.
Please refer to this image of the DullRazor application:
I had an idea of creating a Java runtime program that could loop through all images I need to process(the software only allows 1 image at a time) and complete the necessary steps required for the DullRazor software to successfully alter an image for every image I have.
The DullRazor software works as follows:
-Source File: Requires the path to an image(jpg in my case) to be altered i.e c://Isic-Images//image0000.jpg.
-Target File: Requires the location for the new image with a new image name i.e c://finalLocation//newImage.jpg
-Start: Run the program after giving the inputs in the correct format as described above.
My thinking is iterating through all my images, creating new ones and incrementing the name(img00, img001 etc..).
I have never attempted anything like this in Java and I have had some trouble accessing the Input fields of the software as well as the application's start button.
The code below is just very basic for opening the application, but I am unsure how to access the various items in the DullRazor application and being able to input Strings in those aforementioned fields(again, refer to the DullRazor picture).
private String trainingPath = "C:\\Users\\user\\AppData\\Local\\Temp\\ISIC-Images\\Training\\0";
private String finalPath = "C:\\Users\\user\\finalLocation\\";
public static void main(String[] args) {
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("C:\\Users\\user\\Desktop\\dullrazor.exe");
System.out.println("Opening DullRazor");
OutputStream output = process.getOutputStream();
InputStream input = process.getInputStream();
Thread.sleep(2000);
process.destroy();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException s) {
s.printStackTrace();
} finally {
System.out.println("Closing Dullrazor");
}
}
I have just been testing a bit with the code above, but I am unsure on how to proceed.
Tell me if there is anything that needs clarifying.
Any help is greatly appreciated, thanks.

You can use Java's java.awt.Robot class to control mouse and keyboard on the screen.
This is a simple example entering "test1" and "test2" into two input fields:
Robot r = new Robot();
r.mouseMove(22, 125);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
r.keyPress('T');
r.keyRelease('T');
r.keyPress('E');
r.keyRelease('E');
r.keyPress('S');
r.keyRelease('S');
r.keyPress('T');
r.keyRelease('T');
r.keyPress('1');
r.keyRelease('1');
r.mouseMove(200, 125);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
r.keyPress('T');
r.keyRelease('T');
r.keyPress('E');
r.keyRelease('E');
r.keyPress('S');
r.keyRelease('S');
r.keyPress('T');
r.keyRelease('T');
r.keyPress('2');
r.keyRelease('2');
The above code in action:
If the position of the new application window does not change with each start, and the tool is not about to be deployed to users, this might already suffice. However, if it changes the position with each start, the challenge is to find the window position to add the relative input element positions from there. There are Windows (platform) specific approaches facilitating the Win32 API through JNA, though I'm not familiar with it and whether it is still available in current Microsoft Windows versions.
See these related questions on determining other windows positions:
Windows: how to get a list of all visible windows?
How to get the x and y of a program window in Java?

Using robot works perfectly in order to input into the targeted fields and clicking start/clear button on the application.
In order to find the x & y positions of the application I used runtime exec to open dullrazor and then take a screenshot of the screen with the application up where mouse clicks reveals the x and y position of the current click. Below is the code for finding x & y which I found at this Stackoverflow thread:
Robot robot = new Robot();
final Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
final BufferedImage screen = robot.createScreenCapture(
new Rectangle(screenSize));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel screenLabel = new JLabel(new ImageIcon(screen));
JScrollPane screenScroll = new JScrollPane(screenLabel);
screenScroll.setPreferredSize(new Dimension(
(int)(screenSize.getWidth()/2),
(int)(screenSize.getHeight()/2)));
final Point pointOfInterest = new Point();
JPanel panel = new JPanel(new BorderLayout());
panel.add(screenScroll, BorderLayout.CENTER);
final JLabel pointLabel = new JLabel(
"Click on any point in the screen shot!");
panel.add(pointLabel, BorderLayout.SOUTH);
screenLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
pointOfInterest.setLocation(me.getPoint());
pointLabel.setText(
"Point: " +
pointOfInterest.getX() +
"x" +
pointOfInterest.getY());
}
});
JOptionPane.showMessageDialog(null, panel);
System.out.println("Point of interest: " + pointOfInterest);
}
});
Thank you try-catch-finally for a great answer.

Related

Selenium WebDriver : Verify Print Window dialog displayed on the page

I have a scenario to verify Print Properties dialog (Windows component) opening up correctly after clicking on Print link. Aware of Robot utility class in Java which can emulate keyboard events like Escape/Enter etc. to operate on that window.
Is there any way we can verify the new dialog opened up is a Print dialog - something to verify dialog title i.e. Print or retrieve text from that windows dialog or something else which will confirm dialog to be a Print dialog.
The print dialog comes from the os, which selenium can't handle (yet). Therefore you won't be able to check for existence. The only way to I can think of is using a java.awt.Robot, send VK_ESCAPE and assert that the test continues.
As a starter you could try out this:
Runnable r = new Runnable() {
#Override
public void run() {
try {
Robot r = new Robot();
r.delay(1000);
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
Actions actions = new Actions(getDriver());
actions.sendKeys(Keys.CONTROL).sendKeys("p");
Thread t = new Thread(r);
t.start();
actions.perform();
//some stupid asserts that we reached here
If you are operating in windows (which I am going to assume you are) you can use the inspect.exe tool that comes along with visual studio. It will allow you to interact with the dialogue box and even send any information that you want accurately including selecting elements from the drop down or any other interaction needed. This even works if you wish to save files using selenium, but to answer your question, you can even use it to detect if that window is indeed there. How you want to proceed from there is your call.
//using System.Windows.Automation;
//using System.Windows.Forms;
AutomationElement desktop = AutomationElement.RootElement;
AutomationElement Firefox = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass"));
AutomationElement PrinterComboBox = PrintForm1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "1139"));
SelectionPattern selectPrinterComboBox = (SelectionPattern)PrinterComboBox.GetCurrentPattern(SelectionPattern.Pattern);
AutomationElement ItemInDropdown = PrinterComboBox.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "SelectPrintMethod"));
SelectionItemPattern ItemInDropdownSelectItem = (SelectionItemPattern)ItemInDropdown.GetCurrentPattern(SelectionItemPattern.Pattern);
ItemInDropdownSelectItem.Select();
AutomationElement OKButton = PrintForm1.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "1"));
InvokePattern ClickOK = (InvokePattern)OKButton.GetCurrentPattern(InvokePattern.Pattern);
ClickOK.Invoke();

Java VLCJ player, set audio track is not working

I am currently working on java media player that can play mkv format. I am using VLCJ, everything is working except when I try to change audio track which is not working.
here is the code
public class mediaplayer {
private static JFileChooser filechooser = new JFileChooser();
public mediaplayer() {
}
public static void main(String[] args) {
String vlcPath = "", mediaPath = "";
File ourfile;
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
filechooser.showSaveDialog(null);
ourfile = filechooser.getSelectedFile();
mediaPath = ourfile.getAbsolutePath();
EmbeddedMediaPlayerComponent mediacom = new EmbeddedMediaPlayerComponent();
JFrame frame = new JFrame();
frame.setContentPane(mediacom);
frame.add(canvas);
frame.setLocation(100, 100);
frame.setSize(1050, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
MediaPlayer mplayer = mediacom.getMediaPlayer();
mplayer.playMedia(mediaPath);
mplayer.setAudioTrack(1);
}
}
In libVLC versions before vlc 2.0.5 the native API call to set the audio track was bugged.
With the fix in libVLC 2.0.5, setting the audio track works reliably but you can not just assume a simple index from 0..N and you can not assume sequential track numbers - you must enumerate the audio tracks by calling mediaPlayer.getAudioDescriptions(). The returned TrackDescription objects contain an audio track identifier that should be used with mediaPlayer.setAudioTrack().
To disable audio, you can select the audio track identifier of the track with a a description of "Disable".
Also be aware that you might not be able to set the audio track immediately after calling mediaPlayer.playMedia(). Media is started asynchronously and you may need to wait until the media has actually started and/or has been parsed before the track information is available.
6 years later
The API have grown a lot
If you want to stop or choose an audio track you can use this code snippet and adapt it, the idea is to wait for the player to start using a process then switching to what you need, in my case is the audio disabling
new Thread(
() -> {
try {
while(!empc.mediaPlayer().status().isPlaying())Thread.sleep(500);
empc.mediaPlayer().audio().trackDescriptions().stream()
.filter(td -> td.description().equals("Disable"))
.forEach(t -> empc.mediaPlayer().audio().setTrack(t.id()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
).start();
empc is my media player instance {the embed one for more precision in the player component}

FontMetrics return garbage font descent

I am facing a weird problem in Eclipse, and even after lot of search, haven't found any Bugs also in this regard. My Problem is with the
handle field in FontMetrics Class. Since the API says its Platform Dependent, there is not much I can do about it. The problem is like this:
I have to export some diagrams, made of draw2d widgets and connections, to Word and PDF. Till now, the export feature was available as an Action to the toolbar of the Editor, in which Diagrams are drawn. It has been working fine. All I do is paint the FigureCanvas to an SWT Image, and save it to a File. There are APIs available with me, which then insert it to Word/PDF. Now, I need to that offline, i.e. without actually drawing the diagram on Screen. I did something like this to achieve this:
Job job = new Job("Making DFD for " + data.getName()) {
#SuppressWarnings("unchecked")
#Override
protected IStatus run(IProgressMonitor monitor) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NONE);
try {
imageFolder = new File(tempFolder + IOUtils.FILE_SEPARATOR +
"dfd-" + (new Date()).getTime());
composite.setLayout(new FillLayout());
final FigureCanvas canvas = new FigureCanvas(composite);
ArrayList<DFDFigureData> figureData = renderer.getDfdFigureDatas();
final DFDMaker3 dfdMaker;
dfdMaker = new DFDMaker3(canvas, "", figureData, null, false);
Logger.println("Shell Size:", shell.computeSize(-1, -1, true));
display.syncExec(new Runnable(){
public void run() {
dfdMaker.makeDFD();
shell.setSize(-1, -1);
dfdMaker.selectNode(-1, display);
}
});
Logger.println("Shell Size:", shell.computeSize(-1, -1, true));
/* Image Export Stuff Goes here */
return Status.OK_STATUS;
} catch (Exception e) {
Logger.println("Error in DFD Creation Job", e.toString());
return Status.CANCEL_STATUS;
} finally {
composite.dispose();
shell.dispose();
display.dispose();
}
}
};
job.setPriority(Job.SHORT);
job.schedule();
When I run this for the first time, both the Log statements tell me a good story:
Shell Size::: Point {72, 98}
Shell Size::: Point {1216, 524}
But when I run the same code 2nd time, without closing the application, I get:
Shell Size::: Point {72, 98}
Shell Size::: Point {1216, 1945541082}
The large Height value of the shell spoils everything. After intense debugging, I found that the second time, a FlowPage, that I am using, get a wrong value for Font's descent. The method FontMetrics.getDescent() returns a Random large value.
I am not sure how exactly to proceed in this. I've disposed all the resources that I used the first time. The Display, Shell, Composite, Canvas, and even the GC and SWTGraphics. Can anyone tell me if its a bug? If not, any idea how can I find the problem here?

Correct using VLCj

I try to use VLCj to get access to web-cameras. I am using this code:
public static void main(String[] args) {
// Create player.
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
DirectMediaPlayer mediaPlayer = mediaPlayerFactory.newDirectMediaPlayer(
320, 240,
new RenderCallback() {
#Override
public void display(Memory arg0) {
// Do nothing.
}
});
// Options setup.
String[] options = new String[]{};
String mrl = "v4l2:///dev/video0"; // Linux
// Start preocessing.
mediaPlayer.startMedia(mrl, options);
BufferedImage bufImg;
for (int i = 0; i < 1000; ++i) {
bufImg = mediaPlayer.getSnapshot();
// Do something with BufferedImage...
// ...
}
// Stop precessing.
mediaPlayer.stop();
mediaPlayer = null;
System.out.println("Finish!");
}
And this code partially works -- I can get and work with BufferedImage, but:
I got an error in to output: [0x7f0a4c001268] main vout display error: Failed to set on top
When main loop is finished and camera was disabled program don't finished! I see Finish! message, but program not return control into IDE or console.
UPD:
I am using openSUSE 12.2 x64, VLC 2.0.3 installed and working properly for all video files, library VLCj 2.1.0.
This code working properly:
public static void main(String[] args) {
// Configure player factory.
String[] VLC_ARGS = {
"--intf", "dummy", // no interface
"--vout", "dummy", // we don't want video (output)
"--no-audio", // we don't want audio (decoding)
"--no-video-title-show", // nor the filename displayed
"--no-stats", // no stats
"--no-sub-autodetect-file", // we don't want subtitles
"--no-inhibit", // we don't want interfaces
"--no-disable-screensaver", // we don't want interfaces
"--no-snapshot-preview", // no blending in dummy vout
};
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(VLC_ARGS);
// Create player.
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
// Select input device.
String mrl = "v4l2:///dev/video0"; // Linux
// Start processing.
mediaPlayer.startMedia(mrl);
BufferedImage bufImg;
for (int i = 0; i < 1000; ++i) {
bufImg = mediaPlayer.getSnapshot();
// Do something with BufferedImage...
// ...
}
// Stop processing.
mediaPlayer.stop();
// Finish program.
mediaPlayer.release();
mediaPlayerFactory.release();
}
Re your native window: VLCj opens a shared instance to the VLC library.
A headless media palyer is NOT intended to have a video or audio output!
In fact, if you need anything to play (and not to stream to anywhere else) you need to create either an output window or use a direct media player (may be much more complicated)
So, if a headless player needs to play something it opens a native window to perform the playback!
Source: http://www.capricasoftware.co.uk/wiki/index.php?title=Vlcj_Media_Players
Re the error: the video display component MUST be the top component of the panel, window or whereever it is added to. Otherwise it will throw the error
main vout display error: Failed to set on top
Furthermore, if you put anything over the component it will destroy the video output which won't work anymore!
Anyway, I don't know how the DirectMediaPlayer works in detail but VLCj has some weird behaviour... Maybe getSnapshot() needs a video display component but I'm not sure.
Re your not finishing program: you join to finish your own thread. This can't work because your thread "sleeps" until the other thread who is waited for has been terminated but as this is your own thread it "sleeps" and won't terminate.
You can test this behaviour with this short code in a main method:
System.out.println("Test start");
Thread.currentThread().join();
System.out.println("Test stop");
You will NEVER reach the "Test stop" statement.

java - how to make an xml into a hyperlink? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to add hyperlink in JLabel
In my program, I am searching through an index using Lucene and I am retrieving files. I have created XML files for the retrieved docs from the Lucene's search. Now, I want to make these XML files as hyperlinks and display to the user as the search results. That is I want the XML files to be open when the user clicks on this hyperlink. Any help appreciated!?
for(int i=0;i<file_count;i++)
{
file=str+index[i]+".xml";
JLabel label = new JLabel(file,JLabel.CENTER );
label.setOpaque(true);
label.setBackground(Color.RED);
panel.add(label) ;
label.addMouseListener(new java.awt.event.MouseAdapter() {
#Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() > 0)
{
Runtime r= Runtime.getRuntime();
try {
System.out.println("testing : Inside mouseclicked");
Process p = r.exec("cmd.exe /c start "+file);
System.out.println("opened the file");
} catch (IOException ex) {
System.out.println(ex.getMessage());
System.out.println();
}
}
}
});
}
Here is the code that I have made. In this, I am suppose to get output on the screen "file_count" no of times. I am getting that but what is happening is all the links are showing the same file when clicked. Help?
If I do understand your question correctly, you want to allow the user to open a file. The Desktop class (available as of JDK1.6) allows this
File fileToOpen = ...;
Desktop desktop = Desktop.getDesktop();
desktop.open( fileToOpen )
Depending on how you want to present this to the user, you can opt for your JLabel code with the listener but it is probably easier to use a JButton with an ActionListener. Both approaches are discussed in detail in the answer Marko Topolnik already suggested in his comment. The only difference is that they wanted to open an URL, while you want to open a file (so that answer uses the browse method instead of the open method of the Desktop class).

Categories