I'm programming a desktop application using SWT and I use the browser in parts of the interface because of the flexibility.
I easily can introduce external images. An image in the file system:
<img src="/home/user/image.jpg" />
Or an image on the web:
<img src="http://some.cl/image.jpg" />
Can I obtain the images from a stream? In some place of my code I want to program something like this:
OutputSteam getExternaResource(String resourcePath)
I want to arbitrarily control the origin of the request.
I don't know of a direct way to do this, all I can think of is using javascript to set the image data as base64 string into the src of the image.
Using org.eclipse.swt.browser.Browser.execute(String) or maybe use org.eclipse.swt.browser.BrowserFunction.
The images should have an id which can be used in javascript:
<img id="image1" />
Edit: on the other hand, maybe it's easier to just parse the HTML previously and set the image base64 string there.
Depending on how you get the HTML you could do:
if you create the HTML yourself, just use <img src="data:image/png;base64.... convert the image to base64 and put it in the src attribute
if you read the HTML from an external source, you could use JSoup to parse the HTML and replace the image src attribute with a base64 string. afterwards use Browser.setText(String) to set the HTML of the browser, be aware that in that case relative paths (in links or images) don't work.
String html = "html";
Document doc = Jsoup.parse(html);
Elements img = doc.getElementsByTag("img");
for (Element element : img) {
String src = element.attr("src");
// READ image using the existing src, convert to base64 (using java.util.Base64)
String base64 = "";
element.attr("src", "data:image/png;base64,"+);
}
String newHtml = doc.html();
browser.setText(newHtml);
If you have control over the HTML page, i.e. it is generated by your code, possibly from a template, then you can embed the image.
The bytes of the image need to be base64 encoded and appended to the src attribute of the image tag like described here: http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/
Related
Im trying to get the all the images of a website but why does Jsoup not only get the images of the page but also a create document named like the link after the slash?
Elements imageElements = document.select("img[src$=.png], img[src$=.jpg], img[src$=.jpeg]");
for(Element imageElement : imageElements){
String strImageURL = imageElement.attr("abs:src");
Here is the full code
I found a way to fix it it is probably a prnt.sc issue:
Instead selecting the everything with the "img" tag like that:
Elements img = doc.getElementsByTag("img");
I selected everything with the file ending png, jpg and jpeg. Here the code:
Elements imageElements = document.select("img[src$=.png], img[src$=.jpg], img[src$=.jpeg]");
I have String which contains some html tags and it is coming from database, i want to write that in PDF file with same styling present in the String in the form of HTML tag. I tried to use XMLWorkerHelper like this
String html = What is the equation of the line passing through the
point (2,-3) and making an angle of -45<sup>2</sup> with the positive
X-axis?
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new
StringReader(html));
but it only reads the data which is inside the html tag(in this case only 2) other string it simply ignores. But i want the entire String with HTML formating.
With HTMLWorker it works perfectly but that is deprecated so please let me know how to achieve this.
I am using iText 5 lib
I have java.awt.Image img, And want to add it to myjTextEditor as HTML tag, the known way to imbed image by img src=.... , didnt work:
String Html="<img src=\""+thumb+"\"/><br/>";// thumb is an Image Object
myjTextEditor.setText(Html);
That code will work fine if thumb is a file... is it possible to do that? (i.e imbed Image object)??
Well if you can base64 encode thumb data you can use like this:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
The section after image/png;base64, is base64 encoded binary image data.
I have just started working on a content extraction project. First I am trying to the Image URLs in a webpage. In some cases, the "src" attribute of "img" has relative URL. But I need to get the complete URL.
I was looking for some Java library to achieve this and thought Jsoup will be useful. Is there any other library to achieve this easily?
If you just need to get the complete URL from a relative one, the solution is simple in Java:
URL pageUrl = base_url_of_the_html_page;
String src = src_attribute_value; //relative or absolute URL
URL imgUrl = new URL(pageUrl, src);
The base URL of the HTML page is usually just the URL you have obtained the HTML code from. However, a <base> tag used in the document header, may be used for specifying a different base URL (but it's not used very frequently).
You may use Jsoup or just a DOM parser for obtaining the src attribute values and for finding the eventual base tag.
I am retrieving a captcha image from the Java based package "SimpleCaptcha"
On the front end I just put the following in my page and I get a captcha image:
<img src="stickyImg" />
I want to reload this captcha image onclick using javascript.
I tried:
$("#theclickhandler").click(function(){
$("#stickyImg").load('stickyImg', function(response){
$("#stickyImg").attr('src', response);
});
return false;
});
This gets the image but outputs something like this (greatly shortened obviously):
<img src="captcha image�PNG �ݚ��L�23U�݆�}$�J����Dy����IEND�B`� ">
That looks like raw, binary data to me. How do I get this to work?
The src attribute of an image tag specifies to the browser where the image to load is, not the content of the image. So you're stuffing the content of the image into the place where you want the location to be, and that's giving you garbage because it simply doesn't make sense.
So, to reload the image, you need to tell the browser that the address of the image has changed. It's not sufficient to simply rewrite the location in the image's src attribute to the same address -- that won't tell the browser to change anything. You can overcome this by stuffing some random data in the query string, say, the time of the request. Like #mikerobi suggests, you can just rewrite the src tag, here with the modification of putting a timestamp in the query string (which your servlet will almost surely ignore):
$('#stickyImg').attr('src', 'stickyImg?' + (new Date().getTime()));
You can't use that approach to load images, you need to set the image url attribute to the path of the image generator.
$('#stickImg').attr('src', 'path/to/image/generator');
I don't know how many browsers support it, but it's possible to base64 encode an image, prefix it with (depending on the image format) "data:image/png; base64,", and use that as a URI for the image.