Is it possible to crop/create thumbnails using apache POI - java

I wanted o know whether it's possible to compress a given image to a predefined size in apache POI.
Say I have an input image of 500X500, and I want to convert to a thumbnail size (40X40), can I do it in apache POI?
if not, does java library provide any image processing capabilities?
Right now, I was able to embed images using apache POI, but I have no control over forcing the image to be with in a certain cell size.
Thanks.

The Picture interface in POI has resize methods as in the Javadocs
void resize(double scale)
Parameters:
scale - the amount by which image dimensions are multiplied relative
to the original size. resize(1.0) sets the original size, resize(0.5)
resize to 50% of the original, resize(2.0) resizes to 200% of the
original.
Use either the HSSFPicture or XSSFPicture implementations

Since Apache POI is designed to deal with Microsoft Documents, I wouldn't say it isn't possible.
A quick look through the Java Doc and nothing jumps out at me.
Image resizing is a fun exercise with lots of trade offs and options.

Related

javaxt.io.Image doubles in size when using saveAs()

In an application, I need to use javaxt.io.Image to rotate the image and then save it into a new file. This is the code:
Image image = new Image(input);
image.rotate();
image.saveAs(output);
While the image does lose the EXIF rotation, which is the reason I needed this functionality, the image almost doubles in size. input.jpg is at 2.8MB, while output.jpg becomes 4.3MB. I couldn't find a parameter or function for compression in javaxt.io.Image.
Did I miss a step or is this behaviour wanted by the library? What would be the most efficient way to re-reduce the size?
As Thomas commented, I missed the function setOutputQuality(float quality) in the library documentation. Using that enables you to compress your images.

Java Drawing shapes to file of 2D object

I have a play framework application which I want to be able to produce a product label from. I have the label design in illustrator. It consists of a black circle, white writing with a QR code in the middle, also has curved text.
I want to create a high resolution PDF and/or image file of this design on the fly. All most all of the drawing stuff I find for java relates to swing.
Anyone done this?
The basic class which allows creating an image programatically is BufferedImage and the corresponding Graphics2D class. You are not forced to use it with Swing. You can easily convert it to common graphic formats like PNG. Then you can save it as an image file or place it in a generated(e.g. with iText) PDF.
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
In other words - yes, it can be done.
But if I ware you I would consider exporting the design from Illustrator to a file and use it as a resource in your application. But if you need to scale it programatically you ought to consider using SVG format to avoid loosing quality. Java does not have build-in support for vector images so you should look at
Apache Batik

Scalable clipping mask

I need to to clip variablesized images into puzzle shaped pices like this(not squares): http://www.fernando.com.ar/jquery-puzzle/
I have considered the posibility of doing this with a php library like Cairo or GD, but have little to no experience with these librays, and see no immidiate soulution for creating a clipping mask dynamicaly scalable for different sized images.
I'm looking for guidance/tips on which serverside programing language to use to accomplish this task, and preferably an approach to this problem.
You can create an image using GD with the size of the puzzle piece. and then copy the full image on that image with the right cropping to get the right part of the image.
Then you can just dynamically color in every part of the piece you want to remove with a distinct color (eg #0f0) and then use imagecolorallocatealpha to make that color transparent. Do it for each piece and you have your server side image pieces.
However, if I where you I would create the clipping mask of each puzzle peace in advance in the distinct color. That would make two images per connection (one with the "circle" connecter sticking out and one where this circle connector fits into). That way you can just copy these masks onto the image to create nice edges quickly.
GD is quite complicated, I've heard very good things about Image Magick for which there is a PHP version and lots of documentation on php.net. However, not all web servers would have this installed by default.
http://www.php.net/manual/en/book.imagick.php
If you choose to do it using PHP with GD then the code here may help:
http://php.amnuts.com/index.php?do=view&id=15&file=class.imagemask.php
Essentially what you need to do with GD is to start with a mask at a particular size and then use the imagecopyresampled function to copy the mask image resource to a larger or smaller size. To see what I mean, check out the _getMaskImage method class shown at the url above. A working example of the output can be seen at:
http://php.amnuts.com/demos/image-mask/
The problem with doing it via GD, as far as I can tell, is that you need to do it a pixel at a time if you want to achieve varying opacity levels, so processing a large image could take a few seconds. With ImageMagick this may not be the case.

Java image import

What's the best Java library to use if I want to import graphics in some custom format (basically an array of bytes with a specified height, width, bytes per pixel and depth) and then work on it? Image resizing and cutting functionality would be useful if it was included. Should support saving to some lossless format too.
Ones I've found so far are too high level to import the raw data (or maybe it was hidden too deep...)
I've found SWT more than adequate. It allows images to be manipulated
at the pixel level. See this article and the Image Analyzer example.
For more take a look at SWT Home

Auto scale and rotate images

Given:
two images of the same subject matter;
the images have the same resolution, colour depth, and file format;
the images differ in size and rotation; and
two lists of (x, y) co-ordinates that correlate the images.
I would like to know:
How do you transform the larger image so that it visually aligns to the second image?
(Optional.) What are the minimum number of points needed to get an accurate transformation?
(Optional.) How far apart do the points need to be to get an accurate transformation?
The transformation would need to rotate, scale, and possibly shear the larger image. Essentially, I want to create (or find) a program that does the following:
Input two images (e.g., TIFFs).
Click several anchor points on the small image.
Click the several corresponding anchor points on the large image.
Transform the large image such that it maps to the small image by aligning the anchor points.
This would help align pictures of the same stellar object. (For example, a hand-drawn picture from 1855 mapped to a photograph taken by Hubble in 2000.)
Many thanks in advance for any algorithms (preferably Java or similar pseudo-code), ideas or links to related open-source software packages.
This is called Image Registration.
Mathworks discusses this, Matlab has this ability, and more information is in the Elastix Manual.
Consider:
Open source Matlab equivalents
IRTK
IRAF
Hugin
you can use the javax.imageio or Java Advanced Imaging api's for rotating, shearing and scaling the images once you found out what you want to do with them.
For a C++ implementation (without GUI), try the old KLT (Kanade-Lucas-Tomasi) tracker.
http://www.ces.clemson.edu/~stb/klt/

Categories

Resources