Insert image to excel file using JXL without stretching it - java

I can insert image to my excel file using jxl usingsheet.addImage(WritableImage obj). My problem is that, it stretches based on the args of WritableImage. I'm wondering if there is a way so that the image that I insert will not stretch like if I insert a 200x200 sized image it will appear to the sheet as 200x200.

As much as this has bugged me about jxl, I've never found a way to insert an image without associating the aspect ratio to cells instead of pixels/inches/any standard unit of measurement, and I've done decent research in the past on doing so.
The best you can do is to adapt the images to the height/width of the cells you are inserting it into, or even better, set the cell width/height for the cells you are putting the image in.
From the JExcel FAQ- http://jexcelapi.sourceforge.net/resources/faq/
private static final double CELL_DEFAULT_HEIGHT = 17;
private static final double CELL_DEFAULT_WIDTH = 64;
File imageFile = new File(GIF_OR_JPG_IMAGE_FILE);
BufferedImage input = ImageIO.read(imageFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(input, "PNG", baos);
sheet.addImage(new WritableImage(1,1,input.getWidth() / CELL_DEFAULT_WIDTH,
input.getHeight() / CELL_DEFAULT_HEIGHT,baos.toByteArray()));
To keep the aspect ratio, you'll also want to set the WritableImage to not re-size if the user changes the row height or column width. Do this with either of the below (your preference based on if you want the image anchor locked or to move with resizing):
WritableImage.MOVE_WITH_CELLS;
WritableImage.NO_MOVE_OR_SIZE_WITH_CELLS;

Actually, this is possible. Assume that the width of the picture that you want to include is 4000. Then you do the following:
CellView cv = excelSheetTemp.getColumnView(0);
//get the width of the column where you want to insert the picture
int width = forLogo.getSize();
//if the width is less than the size you want, set the column width to
//the width. This will ensure that your image does not shrink
if (width < 4000) {
forLogo.setSize(4000);
excelSheetTemp.setColumnView(0, cv);
width = 4000;
}
double c = 4000/width;
WritableImage im = new WritableImage(0, 1, c, 3, the image file);
excelSheetTemp.addImage(im);

Related

resize image from a height [duplicate]

This question already has answers here:
Java: maintaining aspect ratio of JPanel background image
(4 answers)
Closed 7 years ago.
I have a program that sends the image from frontend (angularjs) to java controller. In controller I am geting a byte array. I can save this image but I would like to resize this image before I saveing. The problem is that I want to set fixed height of the picture, and the change of width should take place proportionately to the height. This procedure should be universal so that it can be applicable to different photos.
Below is my code:
#RequestMapping(value = "/rest/bookImage", method = RequestMethod.POST)
public #ResponseBody MessageDTO UploadFile(
MultipartHttpServletRequest request, HttpServletResponse response) {
Iterator<String> itr = request.getFileNames();
MultipartFile file = request.getFile(itr.next());
FileOutputStream fos;
fos = new FileOutputStream(urlImage);
fos.write(file.getBytes());
fos.close();
}
Since you have the image already, you can use the getScaledInstance function:
yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
You say you want the width to be proportionate to the height, by choosing if you want it to be double or whatever (just set the appropriate height and width respectively!)
See more info here : http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html
public Image getScaledInstance(int width,
int height,
int hints)
Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default.
If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.
Parameters:
width - the width to which to scale the image.
height - the height to which to scale the image.
hints - flags to indicate the type of algorithm to use for image resampling.
Returns: a scaled version of the image.

Adding a picture to Word Document using Apache POI

I know it is already possible to add a picture to Word document using the
XWPFRun: addPicture(java.io.InputStream pictureData, int pictureType, java.lang.String filename, int width, int height) throws InvalidFormatException, java.io.IOException
method. However, I don't want my Picture to be resized. The resizing is not scaling the new images: it is always stretching them, making them useless.
Is there a way to insert image with original size, or scale them proportionally???
I had used Apache POI for quite some time and I don't think it's possible to add picture to a WORD doc without specifying height and width.
I always use following code to retrieve the size of a picture and scale them accordingly if needed.
BufferedImage bi = ImageIO.read(new File(filename));
int width = bi.getWidth();
int height = bi.getHeight();
You can use Apache POI ImageUtils as following:
Dimension dimension = ImageUtils.getImageDimension(imageInputStream, XWPFDocument.PICTURE_TYPE_JPEG);
double width = dimension.getWidth();
double height = dimension.getHeight();

Android add a watermark logo to very large jpg file (say 10000 x 150000) [duplicate]

This question already has an answer here:
Load large picture from file and add watermark
(1 answer)
Closed 8 years ago.
I have a large jpeg file say 10000 x 150000 px. I want to add a small logo to the bottom of the image without re sizing.
I am able to do this If i down sample the original image and draw the logo using canvas.But when i finally save it to file, the image original size will be reduced as I am sampling it.
If i load the original image into bitmap without down sampling, it exceeds the VM.
Below code work for me :-
public static Bitmap mark(Bitmap src, String watermark, Point location, Color color, int alpha, int size, boolean underline) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(watermark, location.x, location.y, paint);
return result;
}
For large image editing you'll need to use native tools like imagemagick. Because there seem to be a lack of advanced image processing libraries in android supported Java.
If you can compile Composite tool's binaries for android. Then you can use them with --limit option to work with limited memory.
Also, you can try OpenCV as an alternative.
You can use BitmapRegionDecoder when deal with large image file. From the official document.
BitmapRegionDecoder can be used to decode a rectangle region from an image. BitmapRegionDecoder is particularly useful when an original image is large and you only need parts of the image.
To create a BitmapRegionDecoder, call newInstance(...). Given a BitmapRegionDecoder, users can call decodeRegion() repeatedly to get a decoded Bitmap of the specified region.
Just decode the part of your image that you need to add watermark, then use Canva to draw text on it.
try {
BitmapRegionDecoder regionDecoder = BitmapRegionDecoder.newInstance("/sdcard/test.png", true);
Bitmap bitmap = regionDecoder.decodeRegion(rect, options);
} catch (IOException e) {
e.printStackTrace();
}

Prepending an image to the top of another image

I've been looking for a solution for the last several days.
I've seen an example of composite images with Java Advanced Imaging. But that seems to be restricted by the smallest width and height of either image files. So it outputs a file with the height and width of the header file.
Preferably, I'd like to have the header not covering any part of the body image. But it's not a requirement. Sometimes the body image's width is smaller than the header and that's fine as the main content of the header file will be in the middle.
Using JDK 1.6.0_41, I need to take the first two images:
And have the result be:
Whether it is using Java or Javscript is fine. The entire process is as follows:
I take a canvas object of a map using OpenLayers, then use a POST to send it to a Java Servlet to be processed and stored. Then later retrieved the image if the user desires.
The long blue header needs to be at the top of an image or just above it. The header image will have content from the user that created it, etc. That I can do. But manipulating multiple images is not something I am familiar with.
In Java, you can do this:
public BufferedImage prependImage(BufferedImage image1, BufferedImage image2) {
Dimension d1 = new Dimension(image1.getWidth(null),
image1.getHeight(null));
Dimension d2 = new Dimension(image2.getWidth(null),
image2.getHeight(null));
Dimension dt = new Dimension(d1.width, d1.height + d2.height);
BufferedImage image = new BufferedImage(dt.width, dt.height,
BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
int x = 0;
int y = 0;
g.drawImage(image1, x, y, d1.width, d1.height, null);
y += d1.height;
g.drawImage(image2, x, y, d2.width, d2.height, null);
g.dispose();
return image;
}

Can I save a huge PNG without the whole thing being in memory?

I'm saving a very large PNG (25 MB or so) with Java. The problem is that while it's being generated, it's using 3+ gigabytes of memory, which is not ideal since it severely slows down systems with low memory.
The code I'm working with needs to combine a set of tiled images into a single image; in other words, I have nine images (PNG):
A1 A2 A3
B1 B2 B3
C1 C2 C3
which need to be combined into a single image.
The code I'm using is this:
image = new BufferedImage(width, height, height, BufferedImage.TYPE_INT_ARGB_PRE);
g2d = image.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
// draw the 9 images on here at their proper positions...
// save image
g2d.dispose();
File file = getOutputFile();
ImageIO.write(image, "png", file);
Is there a way to make and save an image without having the entire image in memory?
Edit:
To draw the images, I'm doing this in a loop:
BufferedImage tile = ImageIO.read(new File("file.png"));
g2d.drawImage(tile, x, y, w, h);
This is being repeated many times (it's usually about 25x25, but sometimes more), so if there is even a small memory leak here, that could be causing the problem.
You can also take a look at this PNGJ library (disclaimer: I coded it), it allows to save a PNG image line by line.
ImageIO.write(image, "png", file); is internally using com.sun.imageio.plugins.png.PNGImageWriter. That method and that writer expect image to be a rendered image but PNG writting is done by 'bands' so you can make a subclass of RenderedImage that generates the requested bands of the composed large image as the writer ask for that bands to the image.
From PNGImageWriter class:
private void encodePass(ImageOutputStream os,
RenderedImage image,
int xOffset, int yOffset,
int xSkip, int ySkip) throws IOException {
// (...)
for (int row = minY + yOffset; row < minY + height; row += ySkip) {
Rectangle rect = new Rectangle(minX, row, width, 1); // <--- *1
Raster ras = image.getData(rect); // <--- *2
*2 I think this is the only place where the writer reads pixels from you image. You should make a getData(rect) method that computes that rect joining 3 bands from 3 images into one.
*1 As you see it reads bands with a height of 1 pixel.
If the things are as I think you should only need to compose 3 images at a time. There would be no need for the other 6 to be in memory.
I know it is not an easy solution but it might help you if you don't find anything easier.
Would using an external tool be an option? I remember using ImageMagick for similar purpose, you would need to save your smaller images first.

Categories

Resources