I have com.lowagie.text.Image Object and I want to save it to a file as PNG image.
I wonder whether it is possible or not to save it as a image file.I googled it but no luck. Anybody know how to save itext Image object to file?
P.S: I am not bothered about writing the Image object to PDF. I just want to save it as a image file(like png,jpg etc).
If you know that your image was originally a PNG, you can just save the result of getOriginalData() to a file. Otherwise, you can build a BufferedImage first:
byte[] data = image.getOriginalData();
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(data));
ImageIO.write(bi, "PNG", file);
Related
I had print an image into 'PDF' using the following code:
InputStream in = new FileInputStream(new File("C:/"+imageName));
PDJpeg img = new PDJpeg(doc, in);
contentStream.drawXObject(img, 20, pageYaxis-120, 80, 80);
Here when imagName="a.jpg" its working fine, In case of imagName="b.png" its not working. In jpg images its working but in png its not. Why it is so? Please help me. How can I make print both the formats, I mean format in depended?
In Apache PDFBox 1.8, use PDPixelMap for PNG images:
BufferedImage awtImage = ImageIO.read(new File(image));
ximage = new PDPixelMap(doc, awtImage);
In the source code of PDFBox, see the ImageToPDF.java example. This will work with all files that can be read with ImageIO. However it is still useful to keep using PDJpeg for JPG images, because there the JPEG files are directly put into the PDF files without being converted into a lossless format.
Bitmap alphaImage = BitmapFactory.decodeStream(in);
PDImageXObject alphaXimage = LosslessFactory.createFromImage(document, alphaImage);
I am currently working with Image processing in Java. Initially I used ImageIO class to write images
ImageIO.write(image,"jpg",os);
the problem with this method is am lossing the actual image size and quality. Then I preferred ByteStream
Files.readAllBytes(fi.toPath());
to read and
fos.write(fileContent);
to write Images. This works perfectly. The issue I am facing here is I can read only files but not Images(ie, BuffreredImage image). Is it possible to read a Image rather than files here or should I move to someother IO?
Code Snippet is here,
try {
File fnew=new File("d:\\3\\IMG1.jpg");
java.io.FileOutputStream fos = new java.io.FileOutputStream(new File("d:\\3\\Test1\\4.jpg"));
File fi = new File("d:\\3\\7.jpg");
byte[] fileContent = Files.readAllBytes(fi.toPath());
fos.write(fileContent);
} catch (Exception e) {
System.out.println("Exception");
}
Any Kind of suggestions or help will be appreciated. Thanks in Advance.
the problem with this method is am lossing the actual image size and quality. Then I preferred ByteStream
When you read a JPEG with with ImageIO, it is converting the JPEG to a Bitmap automatically. Then when you write it, it is encoding to a JPEG again (which loses quality).
Just replace ImageIO.write(image,"jpg",os) with ImageIO.write(image,"png",os) and you are done. A lossless format such as PNG will not lose any data when you write the image.
BufferedImage getRGB() will get you all the actual pixel data for the image. There will be no compression or anything like JPEG. It will be the raw image.
Edited to add an example based on my comments...
BufferedImage image = ImageIO.read(new File("google.jpg"));
ImageWriter w = ImageIO.getImageWritersBySuffix("jpg").next();
ImageOutputStream out = ImageIO.createImageOutputStream(new File("output.jpg"));
w.setOutput(out);
ImageWriteParam param = new JPEGImageWriteParam(Locale.getDefault());
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1);
w.write(null, new IIOImage(image, null, null), param);
out.close();
I have a web page that allows user to upload images and save them into a blob field of a MySQL DB. I need to resize those images, and I would like to know what the steps are.
Now, I get the image this way:
InputStream content = request.getPart("immagine").getInputStream();
What should I do to resize the image? Should I turn the InputStream into a BufferedImage, then resize it and then turn it into an InputStream again before saving it into the DB?
We had similar requirement, to crop and re-size image. we used imgscalr library for this.
BufferedImage img = //Convert you input image to BufferedImage, we used ImageIO.read for this.
BufferedImage scaledImg = Scalr.resize(img, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.AUTOMATIC, 400, 300); //400,300 was the size we expected
Hope this helps!
Using Spring MVC & JSP
My scenario-
User uploads image file (gif,jpg,png) and if the file doesnt match dimensions then file needs to be scaled and disaply on jsp as preview.
I have MultipartFile(which is uploaded file), I convert that into BufferedImage then I resize the BufferedImage using Graphics2D. I want to convert this buffered image into multipart file again to show it on jsp.
How Can i convert buffered image into MultipartFile?
Thanks
I doubt this statement is correct: I want to convert this buffered image into multipart file again to show it on jsp
To my knowledge you need to either:
Write the converted image into a disk and display it using html <img..> tag, or
Create a spring mvc handler method that writes the image into the response body directly, something like this
.
#RequestMapping(..)
public void convertedImg(HttpServletResponse resp) {
// set response Content-Type..
OutputStream os = resp.getOutputStream();
//.. write your converted image into os
}
You can probably use ImageIO to write your BufferedImage to some file format.
Like:
BufferedImage image; // your image
OutputStream stream; // your output
try {
ImageIO.write(image, "png", stream);
}
finally {
stream.flush();
}
Where stream can be a FileOutputStream to the server's file system, the OutputStream of a database blob, or the servlet response's OutputStream, depending on where/if you want to store it.
I'm trying to convert a bitmap image into an uncompressed tif file for use with the Tesseract OCR engine.
I can use this method to produce a compressed tif file...
final BufferedImage bmp = ImageIO.read(new File("input.bmp"));
ImageIO.write(bmp, "jpg", new File("output.tif"));
This produces an empty tif file when the "jpg" is changed to tif as these files are dealt with in Java Advanced Imaging (JAI).
How can I create an uncompressed tif image? Should I decompress the tif image produced from the above code or is there another way to handle the conversion process?
Any examples provided would be much appreciated.
Thanks
kingh32
You can use ImageWriteParam to disable compression:
TIFFImageWriterSpi spi = new TIFFImageWriterSpi();
ImageWriter writer = spi.createWriterInstance();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_DISABLED);
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("output.tif"));
writer.setOutput(ios);
writer.write(null, new IIOImage(bmp, null, null), param);
Some time before i was facing the problems with tiff images reading and conversion with jai.
I found that it need to install support for working with tiff images in jai, then it works fine for me u can also get it form here:
https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jaiio-1.0_01-oth-JPR#CDS-CDS_Developer
and install over a jvm then it will also work for you.
you can also have a look here
Java / JAI - save an image gray-scaled