Convert a bitmap image to an uncompressed tif image in Java - java

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

Related

Compress image size using Java

I want to compress(reduce) image size using Java. We can upload images in jpg/jpeg/png formats. The general format of images is PNG. So, after image uploaded to the server, we need to compress(reduce file size) and convert it to PNG.
I have the next code for the compress image:
BufferedImage bufferedImage = ImageIO.read(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
switch (imageType) {
case PNG:
case JPG:
// need Java 9+ for PNG writer support
ImageWriter writer = ImageIO.getImageWritersByFormatName(imageType.getExtension()).next();
ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.3f);
}
writer.write(null, new IIOImage(bufferedImage, null, null), param);
writer.dispose();
return new ByteArrayInputStream(outputStream.toByteArray());
default:
log.warn("Image type unknown");
return null;
}
The problem is - after processing the image, I got the result - file size increased instead of reducing. The original image has a lower size than compressed.
Any suggestions on how to solve this issue?
Unfortunately the lossy JPEG compression compresses far better than the lossless PNG compression. You could restrict width and height of the image and scale proportionally.
So I would switch to JPEG and restrict the size.
As a solution for this problem I can recommend the API of TinyPNG.
You can use it for compressing as well as resizing the image.
It works for both .jpeg and .png.
Documentation: tinypng.com/developers/reference/java

JPG to PBM image conversion

I'm trying to convert a ".jpg" file to ".pbm" in java, but I've not been lucky, I tried with the following code: javaxt.io.Image image = new javaxt.io.Image("image.jpg");
image.saveAs("imagepbm.pbm");
But it didn't work, Could someone help me please! Thanks in advance!
In standard Java 8 PBM format is not supported. The list of supported formats as returned by
String[] f2 = ImageIO.getWriterFormatNames();
Arrays.stream(f2).forEach(t -> System.out.println(t));
is:
JPG
jpg
bmp
BMP
gif
GIF
WBMP
png
PNG
wbmp
jpeg
JPEG

The Thumbnailator library converts image to cmyk

I'm reading an image at full size with correct orientation with the Thumbnailator library which honors EXIF orientation flags.
InputStream is = new BufferedInputStream( new FileInputStream(filepath) );
BufferedImage image = Thumbnails.of(is).scale(1).asBufferedImage();
ImageIO.write(image, "jpg", new File(filepath));
But after converting the colorls are vrong so I checked the color model with XnView and it shows jpeg cmyk.
The original image was rgb. So why colors are wrong after using Thumbnailator library?
I was facing the same problem. I resolved the issue by saving it as PNG. Thumbnailator has got several issues and like the version (0.x.x) suggests there is no stable version out yet.
You can find the detailed solution at this link

PDFBOX JPG image is working but PNG not working

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

How to read Bytes of an Image in Java?

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

Categories