Hello,
Can you let me know how to crop the image using
ImageConvertOptions options = new ImageConvertOptions();
The use case is that the html is getting converted into image without storing in the disc. Code snippet is below.
byte[] buffer = htmlBody.getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
Converter converter = new Converter(() → inputStream, () → loadOptions);
ImageConvertOptions options = new ImageConvertOptions();
converter.convert(() → imageOutputStream, options);
return ResponseEntity.ok(imageOutputStream.toByteArray());
@vladimir.litvinchik - Can you please let me know if you have any suggestion?
I’ve tried to follow How to use FileStreamFactory with PdfViewOptions - #3 by vladimir.litvinchik
But in my use case, can you help how to use the PngViewOptions as i have ImageConvertOptions. Basically, i’m looking for how to pass PngViewOptions when converting html to image. I’m not saving the file in the disc. Returning the ByteArray
@joshuaemerson
I’ve moved this topic into GroupDocs.Conversion category. The code you’ve linked is related to GroupDocs.Viewer. Let us take a look.
Thanks @vladimir.litvinchik . Can you let me know how to resize the generated image without saving the image
@joshuaemerson
To resize the image you can specify image width or heigh
ImageConvertOptions options = new ImageConvertOptions();
options.setFormat(ImageFileType.Png);
options.setWidth(200);
The sample app: resize-image.zip (10.5 KB)
See Convert to Image with advanced options for more details on ImageConvertOptions
.
@vladimir.litvinchik - Thank you. I’ve tried this, but if i set the width using ImageConvertOptions, then the generated image is shrunk or stretched and loose the picture quality. The same image looks good if do not set the width. To visualize, consider a rectangular image in a whole page. If i want to crop only the rectangular image without loosing the image quality. Can you let me know if we have options to crop the image like that?
@joshuaemerson
It is not supported. As as workaround you can use java.awt
and javax.imageio
to create a rectangular thumbnail.
public static BufferedImage resizeAndCropImage(ByteArrayOutputStream imageStream, int thumbnailWidth,
int thumbnailHeight) throws IOException {
// Convert ByteArrayOutputStream to BufferedImage
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageStream.toByteArray());
BufferedImage originalImage = ImageIO.read(inputStream);
// Calculate new dimensions to maintain aspect ratio
int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight();
double scale = Math.max(
(double) thumbnailWidth / originalWidth,
(double) thumbnailHeight / originalHeight);
int newWidth = (int) (scale * originalWidth);
int newHeight = (int) (scale * originalHeight);
// Resize the image
Image scaledImage = originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(scaledImage, 0, 0, null);
g.dispose();
// Crop the image to the center
int x = (newWidth - thumbnailWidth) / 2;
int y = (newHeight - thumbnailHeight) / 2;
BufferedImage croppedImage = resizedImage.getSubimage(x, y, thumbnailWidth, thumbnailHeight);
return croppedImage;
}
This is the output that I’ve got. Image is 400x400 pixels.
The sample app: crop-image.zip (13.0 KB)
I have logged this feature in our internal bug-tracker. The issue ID is CONVERSIONJAVA-2494.
Thank you @vladimir.litvinchik . But the image quality is greatly reduced and it’ll be blur if we use ImageIO.write.
@joshuaemerson
Yes, possibly there are some options that can improve the quality. We’ll check if we could add this feature to our public API.
Great. Thank you. Please update here when it’s available in the public API