import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import login.Login;
import autoitx4java.AutoItX;
import com.jacob.com.LibraryLoader;
class LoginChildB extends Login{
}
public class SeleniumDesktopAppTest {
public static void main(String[] args) throws InterruptedException,AWTException {
final String[] argsCopy = args;
String jacobDllVersionToUse;
if (jvmBitVersion().contains("32")){
jacobDllVersionToUse = "jacob-1.18-x86.dll";
}
else {
jacobDllVersionToUse = "jacob-1.18-x64.dll";
}
Thread workerThread = new Thread(){
public void run(){
LoginChildB.main(argsCopy);
}
};workerThread.start();
File file = new File("lib", jacobDllVersionToUse);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
AutoItX x = new AutoItX();
if(x.winWaitActive("JavaFX Welcome")){
Thread.sleep(1000*1);
x.send("partha"+"\t");
Thread.sleep(1000*1);
x.send("sarathi"+"\t");
Thread.sleep(1000*1);
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}
}
/**
*
* Returns if the JVM is 32 or 64 bit version
*/
public
static String jvmBitVersion(){
return System.getProperty("sun.arch.data.model");
}
}
In the above code I am trying to open a JavaFX form(imported through external jar) and give the inputs to the text fields after that pressing the submit button. When I am running this code in Java 7 I don't have any issues. But when I am running this code using Java 8 the submit button was not getting pressed. Seems java.awt.Robot was not compatible with Java 8. Can any one please tell how to resolve this problem?
Related
Ideally in Java, but perhaps in C#, I'm looking to programmatically copy a directory of files & folders into the clipboard, and allow the user to manually paste these through Windows Explorer (Ctr+V etc) onto their Android device, via Windows Explorer.
So the code below is from the following question and is what I've tried so far:-
https://stackoverflow.com/a/31798747/6120066
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CopyFile {
public static void main(String[] args) throws InterruptedException {
File file = new File("C:\\blabla.txt");
List listOfFiles = new ArrayList();
listOfFiles.add(file);
FileTransferable ft = new FileTransferable(listOfFiles);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ft, new ClipboardOwner() {
#Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("Lost ownership");
}
});
System.out.println("WAITING");
Thread.sleep(2 * 60 * 1000);
}
public static class FileTransferable implements Transferable {
private List listOfFiles;
public FileTransferable(List listOfFiles) {
this.listOfFiles = listOfFiles;
}
#Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.javaFileListFlavor};
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.javaFileListFlavor.equals(flavor);
}
#Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return listOfFiles;
}
}
}
I've managed to copy files and paste them manually to another location on my laptop's disk, but it doesn't work when I try pasting to the Android device. However if I manually copy a file/folder in Explorer, I can paste it onto the Android device fine.
Any ideas? Do I need to use more native stuff? That is why I tagged with C# also.
I'm trying to extract establishments data from GooglePlaces API. It used to work initially, but after making a specific method for extracting the places (instead of in the main method), the program crushes. When debbuged it, it gets stuck in a java method called park (under LockSupport class from java). Reading about it, it says that this happens when there is more than 1 thread and there are sync problems. I'm very new at this and I don't know how to solve this in my code. In my mind, there is only 1 thread in this code, but I'm pretty sure I'm wrong. Please help. It crashes in a "for" commented below. Thanks so much!
package laundry;
import java.util.ArrayList;
import java.util.List;
import se.walkercrou.places.GooglePlaces;
import se.walkercrou.places.Param;
import se.walkercrou.places.Place;
import java.io.FileWriter; //add to import list
import java.io.IOException;
import java.io.Writer;
import static java.lang.Math.sqrt;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.location.Coordinates;
import se.walkercrou.places.exception.GooglePlacesException;
import se.walkercrou.places.exception.RequestDeniedException;
public class Laundry {
public static void main(String[] args) throws IOException {
List<Place> detailedPlaces = new ArrayList<>();
List<double[]> circlesPoint = new ArrayList<>();
double radio = 10;
Coordinates startingPoint = new Coordinates (-38.182476,144.552079,0);//geelong south west corner of the grid
Coordinates finalPoint = new Coordinates(-37.574381,145.415879,0); //north east of melbourne
GooglePlaces cliente = new GooglePlaces("keyof googlplaces");
MyResult result1=exploreGrid(startingPoint,finalPoint, radio, detailedPlaces, circlesPoint,cliente);
writeResultsCircles(result1.getPoints(),"c:\\outputCircles.txt" );
writeResultsPlaces(result1.getPlaces(), "c:\\outputPlaces.txt");
}
private static MyResult exploreGrid(Coordinates SWpoint,Coordinates NEpoint,double rad, List<Place> lugares, List<double[]> points,GooglePlaces client){
int iterationRow=0;
Coordinates workingPoint = new Coordinates(SWpoint.getLatitude(),SWpoint.getLongitude(),(float) 0.0);
List<Place> places = new ArrayList<>();
while (workingPoint.getLatitude()<NEpoint.getLatitude()){
while (workingPoint.getLongitude()<NEpoint.getLongitude()){
try {
places = client.getNearbyPlaces(workingPoint.getLatitude(), workingPoint.getLongitude(), rad*1000,GooglePlaces.MAXIMUM_RESULTS ,Param.name("types").value("laundry"));
if (places.size()==60){//si se llega al tope de resultados de getNearbyPlaces
iterationRow=1;
}
for (Place place : places) {
lugares.add(place.getDetails());//here is where it crashes
}
}catch (GooglePlacesException ex) {
System.out.println(ex.getCause());
}
double[] prePoint = {workingPoint.getLatitude(),workingPoint.getLongitude(),rad};
points.add(prePoint);
workingPoint.setLongitude(workingPoint.getLongitude()+rad*sqrt(3)*0.01134787);
}
iterationRow++;
if (isEven(iterationRow)){
workingPoint.setLongitude(SWpoint.getLongitude());
} else {
workingPoint.setLongitude(SWpoint.getLongitude()+rad*sqrt(3)*0.01134787/2);
}
workingPoint.setLatitude(workingPoint.getLatitude()+rad*3/2*0.00899416);
}
return new MyResult(lugares,points);
}
}
I'm trying to use java.awt.FileDialog in an ImageJ plugin but for some reason I am getting an error that Java cannot find the getFiles method:
C:\File_Opener3.java:50: cannot find symbol symbol : method
getFiles() location: class java.awt.FileDialog fd.getFiles();
^ 1 error
I get a similar error when trying setMultipleMode, but other methods like setVisible and getFile work fine. Can some one tell me what I am doing wrong?
import ij.plugin.*;
import ij.*;
import ij.io.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import ij.gui.*;
import ij.plugin.frame.Recorder;
import ij.util.Java2;
import ij.macro.Interpreter;
import java.awt.*;
import java.awt.FileDialog;
import java.awt.Frame;
// Try to figure out why this only allows list veiw
public class File_Opener3 implements PlugIn {
//static File dir;
private static Frame sharedFrame;
private String dir;
private String name;
public void run(String arg) {
openFiles();
IJ.register( File_Opener .class);
}
public void openFiles() {
Frame parent = IJ.getInstance();
if (parent==null) {
if (sharedFrame==null) sharedFrame = new Frame();
parent = sharedFrame;
}
FileDialog fd = new FileDialog(parent, "title"); // From Java.awt.FileDialog
fd.setVisible(true);
//fd.setMultipleMode(true);
name = fd.getFile();
if (name==null) {
if (IJ.isMacOSX())
System.setProperty("apple.awt.fileDialogForDirectories", "false");
Macro.abort();
} else
dir = fd.getDirectory();
//File[] files = fd.getFiles();
fd.getFiles();
//IJ.log("48 fd.getFilenameFilter(): "+fd.getFilenameFilter());
Opener opener = new Opener();
//opener.openMultiple();
/* for (int i=0; i<files.length; i++) {
ImagePlus img = opener.openImage(path, files[i].getName());
if (img!=null)
img.show();
} */
}
}
FileDialog.getFiles() and FileDialog.setMultipleMode() were introduced in Java 1.7. You are probably compiling against an earlier version of Java. If you're using an IDE, check the source level that's set for your project.
I want to do some image analysis on a video that's stored in .mp4 format. Therefore I need a way to just get the images of this movie in Java.
I goolged a lot and found some libraries like jcodec and jaad. BUT I wasn't able to get the things running with these libraries. And as I found out, there were examples (at least I found none) that showed my usecase.
Can you help me? Do you know any library that can do what I need and is running at least on Win7 64 bit.
Or do you know how to accomplish this with jcodec?
edit:
As I wrote, I tried it with jcodec. I found out how to get the data of a frame, but not how I can get it into something like a BufferedImage or so. I expect that these data isn't in a simple RGB format but in any compressed format or so. (Am I right with that?) I don't know to to decode this data.
You can get the data of a frame with jcodec as follows (at least as far as I understand this):
public static void main(String[] args) throws IOException, MP4DemuxerException {
String path = "videos/video-2011-09-21-20-07-21.mp4";
MP4Demuxer demuxer1 = new MP4Demuxer(new FileInput(new File(path)));
DemuxerTrack videoTrack = demuxer1.getVideoTrack();
Packet firstFrame = videoTrack.getFrames(1);
byte[] data = firstFrame.getData();
}
I also found the following:
http://code.google.com/p/jcodec/source/browse/trunk/src/test/java/org/jcodec/containers/mp4/DitherTest.java?r=70
But this isn't working (has compile errors) with the downloadable jar-package.
you could use jcodec(https://github.com/jcodec/jcodec) in the followinf program i am extracting frames from a video.
/*
* To extract frames from a mp4(avc) video
*
*/
package avc_frame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jcodec.api.FrameGrab;
import org.jcodec.api.JCodecException;
public class Avc_frame {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException, JCodecException {
long time = System.currentTimeMillis();
for (int i = 50; i < 57; i++) {
BufferedImage frame = FrameGrab.getFrame(new File("/Users/jovi/Movies/test.mp4"), i);
ImageIO.write(frame, "bmp", new File("/Users/jovi/Desktop/frames/frame_"+i+".bmp"));
}
System.out.println("Time Used:" + (System.currentTimeMillis() - time)+" Milliseconds");
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;
public class Read{
public static void main(String []args) throws IOException, Exception
{
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("C:/Users/Digilog/Downloads/Test.mp4");
frameGrabber.start();
IplImage i;
try {
i = frameGrabber.grab();
BufferedImage bi = i.getBufferedImage();
ImageIO.write(bi,"png", new File("D:/Img.png"));
frameGrabber.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm developing a Eclipse plugin that reads from and writes to the console. I expect the following code to flash an alert window with "Hello World" on it.
public void run(IAction action) {
ConsoleCommands.writeToConsole("Hello World!");
Alert(ConsoleCommands.readConsole());
}
However, the alert simply shows blank. Some investigation showed that the read was happening before the write (the display on the console was fine, just the alert was showing the previous state of the console) so I tried,
public void run(IAction action) {
ConsoleCommands.writeToConsole("Hello Wolrd!");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Alert(ConsoleCommands.readConsole());
}
in case there was a threading issue, but this simply delays the writing to the console as well. Any ideas what is happening?
----EDIT-----
In case it's useful, here's the code for the methods...
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
public class ConsoleCommands {
private static MessageConsole findConsole(String name) {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++)
if (name.equals(existing[i].getName()))
return (MessageConsole) existing[i];
// no console found, so create a new one
MessageConsole myConsole = new MessageConsole(name, null);
conMan.addConsoles(new IConsole[] { myConsole });
return myConsole;
}
public static String readConsole() {
MessageConsole myConsole = findConsole("Joe's Console");
IDocument doc = myConsole.getDocument();
return doc.get();
}
public static MessageConsole writeToConsole(String output) {
MessageConsole myConsole = findConsole("Joe's Console");
MessageConsoleStream out = myConsole.newMessageStream();
out.println(output);
return myConsole;
}
}
Try writing to the console using myConsole.getDocument().set(output) instead of using the stream.
I don't know if this is recommended practice but it solved the problem for us...