Adding a picture to Word Document using Apache POI - java

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();

Related

Docx4j scale image to parent element

I want to scale an image in docx4j to match the width of it's parent if it's too large to fit on the page. For example maybe it's inside a table cell which has a width or bigger than the page.
I'm currently playing with the int width = box.getContainingBlock().getWidth(); and int imagewidth = imagePart.getImageInfo().getSize().getWidthMpt(); but the values don't seem to be correct. Is there a better way of doing this?
https://github.com/plutext/docx4j/commit/81fecde856e80602a168cee8d3df70269668a9dc (in 3.3.0) addressed this use case, adding:
public Inline createImageInline(String filenameHint, String altText,
int id1, int id2, boolean link, int maxWidth) throws Exception
but you do need to know the width of the parent.
Example of usage https://github.com/plutext/docx4j/commit/279b5bcc91ff65d4dc5b88f08b8f6e9815b12563

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.

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;
}

How to calculate "scanLegth" param from Bitmap.getRGB565(...)

I'm trying get bytes from bitmap in blackberry using the next method in Bitmap:
getRGB565(byte[] rgbData, int offset, int scanLength, int x, int y, int width, int height)
But i have read the params and i don't know how must i calculate scanLength:
scanLength - Width of a scanline (in bytes) within the data array.
Any idea?
Here scanLength is the full width of the original image, while width is the width of the rectangle you are copying from.
If you are copying the whole image it is the same, but if you are copying only a part of the image you'll have scanLength > width.
See also the Bitmap#getRGB565 javadoc
To get byte[] from Bitmap I used this: http://blackberry-digger.blogspot.com/2009/05/code-convert-bitmap-to-png-and-then.html and it worked fine.
Sorry it was too easy . In getARG is another example , it must be used usually with the same int width param

Insert image to excel file using JXL without stretching it

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);

Categories

Resources