6

We are using the Eclipse SWT WebBrowser control to render HTML pages for our Java application. If the page contains an image, we want to get the image content. We can access the DOM to get the IMG element, but there doesn't seem to be a way to get the actual content (i.e. the bytes of the image) other than re-fetching the image using the image URL. (We can get the image URL via the 'src' attribute.) Is there any way to get the actual bytes of the image from the DOM?

Sorceror
  • 4,835
  • 2
  • 21
  • 35
ghirschhorn
  • 311
  • 1
  • 4
  • 7
  • You can try `browser.evaluate(..)` method, see [Snippet query DOM node value (SWT 3.5 and greater)](http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet308.java), but it's just a guess ;].. – Sorceror Aug 10 '11 at 23:10
  • Yes, we use browser.evaluate(..) already for DOM querying and manipulation. However, the question is what Javascipt/DOM methods do we need to send to evaluate to get the image content? – ghirschhorn Aug 11 '11 at 00:26
  • 8
    I think this might already have an answer: [Get image data in Javascript?][1] [1]: http://stackoverflow.com/questions/934012/get-image-data-in-javascript – kvn Aug 11 '11 at 01:38
  • I balieve ghirschorn wants the bytes in Java, not javascript. So its not a duplicate. – TJHeuvel Aug 11 '11 at 11:20
  • Any written code available? Please post it with your question. – Zorayr Aug 17 '13 at 01:52

2 Answers2

1

I am not sure if this is what you are looking for, but basically you can just make a typed XHR (such as ArrayBuffer) to the image source (it should be cached, so no real hit by doing this). I am assume you are using an HTML5 compliant browser (or such that supports ArrayBuffer or the type you need). I am assuming the document has at least one image with a proper source, see Fiddle for working demo.

var img = document.querySelector('img'), xhr = new XMLHttpRequest();
xhr.open('GET', img.src, true);
xhr.responseType = 'arraybuffer';
xhr.addEventListener('load', handleBuffer, false);
xhr.send();
// Your image data ArrayBuffer, feel free to change the type.
function handleBuffer (data) {
   var arryBuffer = data.target.response;
}

Sample Fiddle

joseeight
  • 904
  • 7
  • 10
  • PS: You can then send the arrayBuffer via another XHR to your backend using the same 'responseType'. Java should handle receiving the typed data with no problem. – joseeight Sep 16 '13 at 21:00
-2

Check out https://developer.mozilla.org/en-US/docs/Web/API/FileReader it will let you create base64 data urls, may not work depending on your browser versions.

Rummy
  • 105
  • 5