Matlab drag and drop file from windows explorer to figure (gui) - java

I would like to know it there is a way to drag a file from Windows explorer and drop it in my GUI.
The goal should be to get the file path (or folder path) and be able to load it with my own loading function.
I precise that I am using Matlab 2015b in Windows 10 64bits.
I edit my post to give an code example of what I am trying to do (based on Yair Altman solution and other found in Internet) :
function demo
% Set-up a figure droppable axis
hFig = figure('name','DND example','numbertitle','off');
hAx1 = axes('position',[.1,.1,.8,.8]);
% Enable drop on the figure axis
dnd = handle(java.awt.dnd.DropTarget(),'callbackProperties');
jFrame = get(hFig,'JavaFrame');
jAxis = jFrame.getAxisComponent;
jAxis.setDropTarget(dnd);
set(dnd,'DropCallback',{#dndCallbackFcn,hFig, hAx1});
set(dnd,'DragOverCallback',#dndCallbackFcn);
end
function dndCallbackFcn(varargin)
persistent transferable
eventData = varargin{2};
if eventData.isa('java.awt.dnd.DropTargetDropEvent') %nargin>2
hFig = varargin{3}; % my figure is passed as the third argument
try
eventData.acceptDrop(eventData.getDropAction);
transferable = eventData.getTransferable;
catch
end
dataFlavorList = java.awt.datatransfer.DataFlavor.javaFileListFlavor;
fileList = transferable.getTransferData(dataFlavorList);
%{
I want here to get back the file path and then call my loading function
%}
end
end
I always get an error in the line :
fileList = transferable.getTransferData(dataFlavorList);
The error is the following :
Java exception occurred:
java.awt.dnd.InvalidDnDOperationException: No drop current
at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(Unknown Source)
at sun.awt.datatransfer.TransferableProxy.getTransferData(Unknown Source)
at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(Unknown Source)

I tried to implement same functionality as yours and fall into the same exceptions when trying to get transferable data.
It is rather unclear if getTransferable fails because of default FlavorMap instantiated in %matlabroot%\sys\java\jre\...\lib\flavormap.properties (as pointed in Yair Altman's book
in drag and drop section) or for some other strange reason. Anyway I came accross this dndcontrol object on file exchange which works like a charm for our purpose by managing transferable data on java side directly.
I get inspired from this to wrote my own matlab proxy on top of java.awt.dnd.DropTarget that is more generic and closer to its java implementation peer (i.e. it works exactly the same way as java DropTarget object except that all data types have been converted to more standard and convenient matlab types).
You can download my implementation from here:
GitHub: DropListener
FileExchange: DropListener
And here is some usage example for doing what you need (drop in matlab axis from file explorer):
%
% PURPOSE:
%
% Show how to add drop support from file explorer to some matlab axis
%
% SYNTAX:
%
% [] = DropListenerDemo();
%
% USAGE:
%
% Simply drop files from file explorer into displayed axis.
%
%%
function [] = DropListenerDemo()
%[
% Create a figure with some axis inside
fig = figure(666); clf;
axes('Parent', fig);
% Get back the java component associated to the axis
% NB1: See ยง3.7.2 of Undocumented Secrets of Matlab Java Programming
% NB2: or use findjobj, or javaObjectEDT for drop support onto other component types
jFrame = get(handle(fig), 'JavaFrame');
jAxis = jFrame.getAxisComponent();
% Add listener for drop operations
DropListener(jAxis, ... % The component to be observed
'DropFcn', #(s, e)onDrop(fig, s, e)); % Function to call on drop operation
%]
end
function [] = onDrop(fig, listener, evtArg) %#ok<INUSL>
%[
% Get back the dropped data
data = evtArg.GetTransferableData();
% Is it transferable as a list of files
if (data.IsTransferableAsFileList)
% Do whatever you need with this list of files
msg = sprintf('%s\n', data.TransferAsFileList{:});
msg = sprintf('Do whatever you need with:\n\n%s', msg);
uiwait(msgbox(msg));
% Indicate to the source that drop has completed
evtArg.DropComplete(true);
elseif (data.IsTransferableAsString)
% Not interested
evtArg.DropComplete(false);
else
% Not interested
evtArg.DropComplete(false);
end
%]
end
The object also support for catching DragEnter, DragOver, DropActionChanged, DragExit event so you can tune every aspects of dragging operation. With little effort, it can also be extended to support for image dragging or other data types dragging.
Hope you'll like it and you'll find it generic enough to think of other usages.

There is a post at Matlab Central which uses a compiled java class. A stackoverflow answer included the code. With that solution, your demo may look like this:
function demo
% Set-up a figure droppable axis
hFig = figure('name','DND example','numbertitle','off');
warning('off', 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
jFrame = get(hFig,'JavaFrame');
jAxis = jFrame.getAxisComponent;
% dnccontrol class from above link
dndcontrol.initJava();
dndcontrol(jAxis, #dropCallbackFcn);
end
function dropCallbackFcn(~, evt)
fileparts(evt.Data{1}) % show dropped file's path
end

Related

Not able to reference Java class from my report in BIRT

I am very new to BIRT. I am wokring on a BIRT project where I am trying to reference Java class inside script 'open' section but am unable to do so.
I do not get any errors but I am not able to see any data in my dataset preview.
Script - open
count = 0;
// create instance of
// the GetStockHistory class
gsh = new Packages.de.vogella.birt.stocks.daomock.StockDaoMock(); //cause of error somehow
//Load the List
stock = gsh.getStockValues("Java");
Script-Fetch
if(count < stock.size()){
row["columnDate"] = stock.get(count).getDate();
row["columnOpen"] = stock.get(count).getOpen();
row["columnHigh"] = stock.get(count).getHigh();
row["columnLow"] = stock.get(count).getLow();
row["columnClose"] = stock.get(count).getClose();
row["columnVolume"] = stock.get(count).getVolume();
count++;
return true;
}
return false;
StockDaoMock is a class which returns a dummy list of values.
Referring this blog BIRT sample app
Can anyone please help me here and let me know what am I doing wrong ?
Why can't I see any data in preview dataset. Is there a specific way in which I need to make reference to java classes because I am sure the error is somewhere in that part only. If I remove the reference part and just hardcode a string, then it is working fine and I can see it in the preview. Things mess up as soon as I refer a java class by importing it.
BIRT-4.8
EDIT---
even this inside my Script 'open' doesn't work
importPackage(Packages.de.vogella.birt.stocks.daomock);
gsh = new StockDaoMock();
BIRT does not use the java sources directly. You have to generate a JAR from your classes and add that JAR to your BIRT class path (Window / Preferences / Report Design / Classpath).

Using training made with python API as input to LabelImage module in java API?

I have a problem with java tensorflow API. I have run the training using the python tensorflow API, generating the files output_graph.pb and output_labels.txt. Now for some reason I want to use those files as input to the LabelImage module in java tensorflow API. I thought everything would have worked fine since that module wants exactly one .pb and one .txt. Nevertheless, when I run the module, I get this error:
2017-04-26 10:12:56.711402: W tensorflow/core/framework/op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().
Exception in thread "main" java.lang.IllegalArgumentException: No Operation named [input] in the Graph
at org.tensorflow.Session$Runner.operationByName(Session.java:343)
at org.tensorflow.Session$Runner.feed(Session.java:137)
at org.tensorflow.Session$Runner.feed(Session.java:126)
at it.zero11.LabelImage.executeInceptionGraph(LabelImage.java:115)
at it.zero11.LabelImage.main(LabelImage.java:68)
I would be very grateful if you help me finding where the problem is. Furthermore I want to ask you if there is a way to run the training from java tensorflow API, because that would make things easier.
To be more precise:
As a matter of fact, I do not use self-written code, at least for the relevant steps. All I have done is doing the training with this module, https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/image_retraining/retrain.py, feeding it with the directory that contains the images divided among subdirectories according to their description. In particular, I think these are the lines that generate the outputs:
output_graph_def = graph_util.convert_variables_to_constants(
sess, graph.as_graph_def(), [FLAGS.final_tensor_name])
with gfile.FastGFile(FLAGS.output_graph, 'wb') as f:
f.write(output_graph_def.SerializeToString())
with gfile.FastGFile(FLAGS.output_labels, 'w') as f:
f.write('\n'.join(image_lists.keys()) + '\n')
Then, I give the outputs (one some_graph.pb and one some_labels.txt) as input to this java module: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java, replacing the default inputs. The error I get is the one reported above.
The model used by default in LabelImage.java is different that the model that is being retrained, so the names of inputs and output nodes do not align. Note that TensorFlow models are graphs and the arguments to feed() and fetch() are names of nodes in the graph. So you need to know the names appropriate for your model.
Looking at retrain.py, it seems that it has a node that takes the raw contents of a JPEG file as input (the node DecodeJpeg/contents) and produces the set of labels in the node final_result.
If that's the case, then you'd do something like the following in Java (and you don't need the bit that constructs a graph to normalize the image since that seems to be a part of the retrained model, so replace LabelImage.java:64 with something like:
try (Tensor image = Tensor.create(imageBytes);
Graph g = new Graph()) {
g.importGraphDef(graphDef);
try (Session s = new Session(g);
// Note the change to the name of the node and the fact
// that it is being provided the raw imageBytes as input
Tensor result = s.runner().feed("DecodeJpeg/contents", image).fetch("final_result").run().get(0)) {
final long[] rshape = result.shape();
if (result.numDimensions() != 2 || rshape[0] != 1) {
throw new RuntimeException(
String.format(
"Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s",
Arrays.toString(rshape)));
}
int nlabels = (int) rshape[1];
float[] probabilities = result.copyTo(new float[1][nlabels])[0];
// At this point nlabels = number of classes in your retrained model
DoSomethingWith(probabilities);
}
}
Hope that helps.
Regarding the "No operation" error, I was able to resolve that by using input and output layer names "Mul" and "final_result", respectively. See:
https://github.com/tensorflow/tensorflow/issues/2883

GStreamer-Java: RTSP-Source to UDP-Sink

I'm currently working on a project to forward (and later transcode) a RTP-Stream from a IP-Webcam to a SIP-User in a videocall.
I came up with the following gstreamer pipeline:
gst-launch -v rtspsrc location="rtsp://user:pw#ip:554/axis-media/media.amp?videocodec=h264" ! rtph264depay ! rtph264pay ! udpsink sync=false host=xxx.xxx.xx.xx port=xxxx
It works very fine. Now I want to create this pipeline using java. This is my code for creating the pipe:
Pipeline pipe = new Pipeline("IPCamStream");
// Source
Element source = ElementFactory.make("rtspsrc", "source");
source.set("location", ipcam);
//Elements
Element rtpdepay = ElementFactory.make("rtph264depay", "rtpdepay");
Element rtppay = ElementFactory.make("rtph264pay", "rtppay");
//Sink
Element udpsink = ElementFactory.make("udpsink", "udpsink");
udpsink.set("sync", "false");
udpsink.set("host", sinkurl);
udpsink.set("port", sinkport);
//Connect
pipe.addMany(source, rtpdepay, rtppay, udpsink);
Element.linkMany(source, rtpdepay, rtppay, udpsink);
return pipe;
When I start/set up the pipeline, I'm able to see the input of the camera using wireshark, but unfortunately there is no sending to the UDP-Sink. I have checked the code for mistakes a couple of times, I even set a pipeline for streaming from a file (filesrc) to the same udpsink, and it also works fine.
But why is the "forwarding" of the IP-Cam to the UDP-Sink not working with this Java-Pipeline?
I haven't used the Java version of GStreamer, but something you need to be aware of when linking is that sometimes the source pad of an element is not immediately available.
If you do gst-inspect rtspsrc, and look at the pads, you'll see this:
Pad Templates:
SRC template: 'stream_%u'
Availability: Sometimes
Capabilities:
application/x-rtp
application/x-rdt
That "Availability: Sometimes" means your initial link will fail. The source pad you want will only appear after some number of RTP packets have arrived.
For this case you need to either link the elements manually by waiting for the pad-added event, or what I like to do in C is use the gst_parse_bin_from_description function. There is probably something similar in Java. It automatically adds listeners for the pad-added events and links up the pipeline.
gst-launch uses these same parse_bin functions, I believe. That's why it always links things up fine.

Saving a web page as image

As a hobby project I am exploring the ways to save a web page (HTML) as image, mostly programatically using c/c++/javascript/java. Till now I have come across the following ways:
Get the IHTMLElement of page body and use it to query for IHTMLElementRender and then use its DrawToDC method (Ref: http://www.codeproject.com/KB/IP/htmlimagecapture.aspx ). But the problem is that it did not work for all the pages (mostly pages having embedded iframes).
Another way which i can think of is to use some web browser component and when the pages is fully loaded then capture it using BitBlt (Ref: http://msdn.microsoft.com/en-us/library/dd183370%28VS.85%29.aspx ). But the problem is that the page I have requested may be longer than my screen size and it will not fit into the web browser component.
Any direction/suggestion to resolve above issues or an alternative approach is greatly appreciated.
If you use Python, there's pywebshot and webkit2png. Both of them have some dependencies, though.
Edit: Oops, Python is not in your list of preferred languages. I'll leave this answer here anyway, because you said "mostly" and not "exclusively".
Another (somewhat roundabout) option would be to run a server like Tomcat and use Java to call a command-line tool to take a screenshot. Googling for "command line screenshot windows" comes up with some reasonable-looking possibilities. Besides running a server, though, I don't know a good way to run local executables from javascript. This method would make it cross-browser, though, which is a plus (just make an ajax call to the script when you want a screenshot).
Unfortunately I don't actually know how to deploy war files. It might be more trouble to use Tomcat; I mentioned it because Java was a preferred language. It would be fairly simple to run XAMPP and use this PHP snippet, and you wouldn't really have to learn php:
<?php
exec("/path/to/exec args");
?>
EDIT
You know, I'm not sure that really answers your question. It's one way, but it's coming at it from the JavaScript end rather than the scripting end. If you want to do it via scripting, you could always use Selenium. It supports capturing screenshots of an entire page, and can be controlled via Java.
Well finally able to crack it by going through these two articles:
http://www.codeproject.com/KB/GDI-plus/WebPageSnapshot.aspx [c# code - IE]
http://www.codeproject.com/KB/graphics/IECapture.aspx [c++ & GDI - IE]
Can't share the code, but the above two articles will give you the best possible solution.
Also have a look at:
https://addons.mozilla.org/en-US/firefox/addon/3408/ [firefox + javascript]
Above things are still ok. BUT not guaranteed to work always. Check the below link:
How do I render the scrollable regions of a canvas with IViewObject::Draw?
If you are OK using javascript for it, I suggest going with phantomjs
Example from http://fcargoet.evolix.net/
var page = new WebPage(),
address = 'http://dev.sencha.com/deploy/ext-4.0.7-gpl/examples/feed-viewer/feed-viewer.html';
page.viewportSize = {
width : 800,
height : 600
};
// define the components we want to capture
var components = [{
output : 'feed-viewer-left.png',
//ExtJS has a nice component query engine
selector : 'feedpanel'
},{
output : 'feed-viewer-preview-btn.png',
selector : 'feeddetail > feedgrid > toolbar > cycle'
},{
output : 'feed-viewer-collapsed.png',
//executed before the rendering
before : function(){
var panel = Ext.ComponentQuery.query('feedpanel')[0];
panel.animCollapse = false; // cancel animation, no need to wait before capture
panel.collapse();
},
selector : 'viewport'
}];
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
/*
* give some time to ExtJS to
* - render the application
* - load asynchronous data
*/
window.setTimeout(function () {
components.forEach(function(component){
//execute the before function
component.before && page.evaluate(component.before);
// get the rectangular area to capture
/*
* page.evaluate() is sandboxed
* so that 'component' is not defined.
*
* It should be possible to pass variables in phantomjs 1.5
* but for now, workaround!
*/
eval('function workaround(){ window.componentSelector = "' + component.selector + '";}')
page.evaluate(workaround);
var rect = page.evaluate(function(){
// find the component
var comp = Ext.ComponentQuery.query(window.componentSelector)[0];
// get its bounding box
var box = comp.el.getBox();
// box is {x, y, width, height}
// we want {top, left, width, height}
box.top = box.y;
box.left = box.x;
return box;
});
page.clipRect = rect;
page.render(component.output);
});
// job done, exit
phantom.exit();
}, 2000);
}
});

Java error in FireFox - myApplet.myFunction is not a function

I am getting an error that is only reproducible in Firefox.
In IE, it always works.
I have an ASP.NET application that targets an applet. For most functionality, it works great, but when I run one function (a function that does a lot of different things), the methods on the java applet seems to be unavailable! When I call any functions on the java applet from Javascript code, I get the error : myApplet.myFunction is not a function .
After this has happened, I get the same error for everything that calls functions on the applet, also functionality that worked prior to this state.
The applet is not unloaded or crashed, which is proved by that the interactive functions in the applet still works. But if the applet calls a javascript function on the page that calls back to the applet, I get the same error!
Sometimes it works when I repeat the action several times.
This only happens on the public version of the web application. Not in my local web app that runs on development machine. The main difference is that the public one use login access.
Can anyone give me hint of what can be causing this?
To summairize whats happens: When I run a particular bit of javascript code, the API against the java applet becomes unabailable (Error: myApplet.myFunction is not a function).
Here is the function that causes this buggy state of the applet:
function ParseAndZoomToAddress (objString) {
var mapFrame
eval("var objArray = new Array(" + objString + ")")
if (objArray.length > 0) {
mapFrame = window.frames[0].frames['mapFrame']
if(!mapFrame) alert('mapFrame is null!')
else window.status = 'mapFrame is OK!'
var center_x = 0
var center_y = 0
if(objArray.length == 1) {
var rExp1 = /[G-P]/g
var rExp2 = /[Q-Z]/g
eval("var coordArray = new Array(" + objArray[0].replace(rExp1, '0x').replace(rExp2, ',0x') + ")")
if(coordArray.length == 2){
center_x = coordArray[0]
center_y = coordArray[1]
}else{
alert("Invalid ID " + objArray[0])
}
}else{
var center_x = objArray[1]
var center_y = objArray[2]
}
var objZoomScale = '2000' // document.SearchForm.Scale.value
var scale = mapFrame.m_yur - mapFrame.m_yll
if (objZoomScale != "") {
scale = ScaleToMeter(parseInt(objZoomScale),mapFrame.m_image_width)
}
var xll = center_x - (scale / 2.0)
var yll = center_y - 10
var xur = xll + scale
var yur = center_y + 10
center_x = (xll + xur) / 2.0
center_y = (yll + yur) / 2.0
center_x = Math.round(center_x * 1000) / 1000
center_y = Math.round(center_y * 1000) / 1000
mapFrame.ResetClipPolygon() //This calls applet methods
var lCenterSymbol = '5024,40,85,x,y,5'
var rExp = /x/gi
lCenterSymbol = lCenterSymbol.replace(rExp,center_x)
rExp = /y/gi
lCenterSymbol = lCenterSymbol.replace(rExp,center_y)
mapFrame.parent.m_CenterSymbolString = lCenterSymbol
mapFrame.ResetClipPolygon() //This calls applet methods
mapFrame.previewVisible()
mapFrame.mapVisible()
mapFrame.setMove(false) //This calls applet methods
mapFrame.ZoomToArea(xll, yll, xur, yur) //This calls applet methods
mapFrame.parent.m_CenterSymbolString = ""
}
}
Additionaly I can say that the applet is defined by the <applet> tag.
The applet is located inside a <div> tag that is hidden in javascript with style.display='none' and shown again with style.display='block'.
This is done during the 2 lines:
mapFrame.previewVisible()
mapFrame.mapVisible()
I don't know if this is the problem, but these function are called elsewhere as well where it doesn't cause this, but it might be combination of things.
I am happy to get hint's that will help me solve this, not an absolute solution.
I will accept the best hint as the answer.
Perhaps this is a timing issue. It could happen that the Firefox engine tries to execute the javascript code before the java applet has been properly loaded. To verify this add a delay before accessing the applet in javascript code. Even better use a try-catch block for your call and wrap it in a for-loop. If the call is successful exit the loop, otherwise add a delay and retry.
Edit: Based on your edits it seems that there isn't a timing issue. Since your javascript function is quite big, I would try to locate, where the problem first appears. Create a dummy applet method and call it inside your javascript method at various places. Find out after which statement the problem first appears. Also, if you haven't done that already, install Firebug and watch out for javascript errors.
I was suddenly able to reproduce it in my development environment, so it was easier to locate the problem. The problem was related to hiding/showing as I suspected, but I'm still not sure why it was a problem. It was probably some sort of timing issues as noted by kgiannakakis, so will credit him with the bounty reward.
The problem disappeared when commenting out these two lines:
mapFrame.previewVisible(); // appletDiv.style.display="none"
mapFrame.mapVisible(); // appletDiv.style.display="block"
The comment at end is basically what is done inside those functions with the div that contains the applet.
So far, so good.
The intention with the Visible functions are originally switching 2 tabs where one of them displays the applet (mapVisible), and the other displays a static image (previewVisible). If I click the preview tab, that basically runs previewVisible(), the appletDiv is hidden. If I then run my big nasty function (ParseAndZoomToAddress) then I get the error again, but this time it is somewhat expected, since the applet is hidden (and obviously FireFox does not like to work with hidden applets..).
Then I remember why I included these calls inside the ParseAndZoomToAddress function: I have to make sure the applet is visible before accessing it! So I try to re-introduce the call to mapFrame.mapVisible(), and guess what - it works! Even if the applet was hidden, it is available after the call to mapFrame.mapVisible().
Anyone have a conclusion to this?
My theory is that it causes problems to unload and reload the applet so close together in the same javascript function.
I came here through a search, as I had exactly the same problem in Firefox (not MSIE), on web pages that I knew I had not changed for years and that I knew had worked fine before in Firefox.
myApplet.myFunction is not a function
As it turns out, in my case the culprit is just having the Google Translate Tools Javascript code described at
http://translate.google.com/translate_tools?hl=en&layout=1&eotf=1&sl=bg&tl=en
After I had removed that snippet from my included Javascript file (included at the bottom of my applet page), myApplet.myFunction worked fine again!
It also fixed the mysterious problem that my applet would not always load when loading the web page. Maybe Google Translate Tools under the hood also fiddles with style.display or similar, but I did not further dig into that.
I suspect that many others who use Google Translate Tools on a Java applet page with Javascript controls are similarly affected.
This post seems to have similar problem. They claim they fixed some HTML syntactic error and then it worked in Firefox.
I had a similar problem.
We fixed it by putting the <applet></applet> inside a <div> that has no display:none style.
JavaScript in Firefox cannot see functions in an applet that is inside a div that is not displayed.

Categories

Resources