Text to Captcha Image using java - java

How to convert text to captcha image.
EDIT
i looked at jcaptcha. It creates a random text for a id. But i need
a image with the given text.

Jcaptcha provides classes to convert a text into image.
sample code:
WordToImage image = ComposedWordToImage(fontGenerator, background,textPaster);
BufferedImage image = image.getImage("text");
api doc of WordToImage interface

Related

Convert image data InputStream to string and from this string back to image does not gives the image

Cq5 image component has the binary image data, I require this data to be exposed in an api.
To do this I tried to get the input stream and read it as String.
InputStream is = jcrnode.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
String imageData = jcrnode.getProperty(JcrConstants.JCR_DATA).getString();
When trying to write the string to FileOutputStream I dont get the image.
While trying to download the jcr:data manually and open the file image viewer gives the image.
But when converting this into string and back doesn't work for me. Any suggestions on this.
Thanks #i.net was able to solve as per ur suggestions above.
StringBuilder dataUri = New StringBuilder();
dataUri.append("data:image/jpeg;base64");
dataUri.append(StringUtils.newString Utf8(Base64.encodeBase64(imageByteArray,false)));
dataUri.toString() is the string which can be used as embedded images.
Sample html below to render base64 image
<div>
<p>refer wiki</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>

Control the origin of images in an SWT browser

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/

eclipse plugin: add image to browser

I have created an Eclipse plugin which displays a browser with a URL as follows:
browser.setBounds(0, 0, 100, 100);
browser.setUrl("https://www.stackoverflow.com/");
Also I am able to get the animated image from the bundle as follows:
Image image = ImageDescriptor.createFromURL(FileLocator.find(bundle, new Path("icons/sample.gif"), null)).createImage();
How could I add the Image to the browser on run time execution?
Since you're setting the Browser widget to display a URL, the widget will display the contents of that URL.
If you want to display your own content, you'll have to use the setText() method.
Perhaps you can create html that includes the URL you want in an IFRAME and compose the rest of the html with your own content?
As for the image, I'm sure there are ways to include the image in the constructed HTML ... or you can base64 encode the data and include it in the constructed HTML. See Can I embed a .png image into an html page?

Is it possible to show Image object in a jTextEditor as HTML tag?

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.

Java code to display an Image in Google App Engine

I have an image in the blobstore of my GAE . I need to retrieve it and make some trasformations and finally display it in my jsp page.
Currently I used,
BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
blobstoreService.serve(blobKey, res);
This shows the picture but I want to retrieve it as an 'Image' type and resize it using the code below
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image oldImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform resize = ImagesServiceFactory.makeResize(200, 300);
Image newImage = imagesService.applyTransform(resize, oldImage);
byte[] newImageData = newImage.getImageData();
How will I display my 'newImage' in a jsp page? It would be very helpful if I could see an example code. I would also like to know if there is any way i can get the blob-key of the images I presently have in my blobviewer.
You should not process the image in the request for your JSP page.
You have to take two steps:
1.
Render a JSP page that contains an image tag like
<img src="mydomain.com/getImage?blob-key=123435"/>
2.
Have a separate servelt mapped to mydomain.com/getImage that outputs the image with the given id.
So all the code you presented above will go in the servlet that delivers the image and not in the JSP delivering code. And then the image can be delivered using the HTTPResponses OutputStream. And don't forget to set the correct content type and length for the response.

Categories