I'm brand new to Java Advanced Imaging, and the first stumbling block I've reached is the ability to read in a 12 bit, single band, greyscale JPEG file. I've seen references to it being possible with JAI, but no code or even suggestions about how it should be done. Could someone please help me out with either a helpful link or a short code snippet?
I've been using this tutorial so far, but it hasn't helped me on this issue.
Thanks.
JAI-ImageIO will register itself into Java's ImageIO api, so you should be able to use it just by having the jai-imageio jars in your classpath an calling the normal ImageIO methods, such as ImageIO.read(file).
The problem with jpeg might be that Java has a default jpeg reader in the IIORegistry, and you may have to select the right one manually using something like ImageIO.getImageReadersForFormatName().
Another thing with the more esoteric formats is that JAI ImageIO usually has two implementations - one pure Java and the other using native binary libraries, so make sure that you include the *lib-wrapper.dll (or whatever suits your particular OS) in the LD_LiBRARY_PATH or have the in the working directory of your program. The native implementation usually supports mode format variants than the pure-java one.
we usually read JPEG 12bit grayscale images using JAI + JAI Image I/O.
Additionally to previous answers, you need the native binary libraries to read JPEG 12bit, so mediaLib library is required.
You only need the "ImageRead" operation from Image I/O:
byte[] imageBytes = ...
RenderedOp readImage = JAI.create("ImageRead", new MemoryImageInputStream(imageBytes));
Whe usually read the image files from FTP, so get the byte[] and need the custom MemoryImageInputStream that wraps a byte[] into a ImageInputStream by subclassing ImageInputStreamImpl.
Related
I have TIFF files that contain a single image.
I need to be able to convert them to PNG inside our Java app.
Almost every search says to use JAI - which doesn't seem to exist anymore.
We currently have the itextpdf library in our system, and it looks like it can read a TIFF and write a PNG.
Anyone know how? Or can point me to the correct section of documentation?
I see that there is a TIFFImage class that looks like it can read a TIFF, and a PNGWriter that can write a PNG - but I haven't been able to figure out how to take the result of the TIFFImage (an Image object) and pass that data to the PNGWriter.
You can use standard ImageIO and my TwelveMonkeys ImageIO TIFF plugin to read the TIFF and write it back as PNG. The plugin can in most cases be used as a direct replacement for the JAI ImageIO TIFF plugin.
When the plugin is installed, your TIFF is single page, and all you care about is the pixel data, the code could simply be:
BufferedImage image = ImageIO.read(tiffFile);
if (!ImageIO.write(image, "PNG", pngFile)) {
// Handle file not written
}
I would not recommend using iText for this (and neither would Bruno Lowagie, it seems). :-)
I was trying to open jpeg files in a Java program and noticed that neither ImageIO nor the Apache commons imaging library tools could open the images. The commons library showed me this error:
"Only sequential, baseline JPEGs are supported at the moment"
So, my image files are compressed in a way both libraries aren't able to read. I could make an ImageJ macro and transform all the images first but I would like to just use my program and not something extra.
Is there a way to find the compression mode in jpeg or even a java library that can read jpegs in several modes?
Thanks in advance
Suddenly it works with the ImageIO standard Class. Don't ask me why. Thanks for your help guys.
You need to get some tool that will allow you to dump a JPEG stream. There are a number of them out there.
What you are looking for is the start of frame marker.
FFC0 indicates baseline sequential.
FFC1 indicates extended sequential. It doesn't take much more code to do extended sequential than baseline. It is puzzling why a decoder would limit itself to baseline these day.
FFC2 is progressive.
There are others but those are the only ones you are likely to encounter and are widely supported.
You just need to find a tool that will save in baseline format. Finding one to read the other two formats is easy.
I have to associate a couple of text parameters (a UUID and a couple of strings representing integers) to a PNG image in a way they can follow the image when the PNG file is passed from an Android device to another through the Net. This is the typical situation in which I would use a couple of custom auxiliary chuncks (similar to EXIF fields ) to store my data inside the PNG image itself.
Maybe it is just me but the only info I was able to find about reading and writing PNG custom metadata from Java code on Android was this SO post:
Writing image metadata in Java, preferably PNG
that even offers some code (quite verbose, as usual with Java).
Those same SO post refers also to PNGJ: http://code.google.com/p/pngj/
To be honest, I would be happy to not use yet another library in this project.
Does anybody know of others ways to write and read text metadata in a PNG file in Android? Maybe a less verbose way... Maybe a way that does not require a separated library....
Any other source of information? Any tutorial? Any example?
I'm open to use a different (but equivalent) image file format, if needed (JPEG, whatever).
A solution working also on iOS and Windows 8 Phone would be a plus but it is not actually required.
I had to do something similar lately, so I had to study the subject.
Android offers you no option of manipulating png metadata. You will have to use an external library (PNGJ seems like a good option).
In my case, since I am making changes to the Android frameworks, I didn't use an external lib, but made changes to skia (the Android graphics library instead).
If you need more info on that I can give you, but I think it's irrelevant in your case.
In server side image process, should I use AWT or SWT?
I think AWT maybe too abstract away from the actual image data bits, so I guess SWT maybe slightly faster.
Or do you suggest another open source image process library? (BSD or Apache licensed)
The server is running Ubuntu 11.04.
Requirements:
Read/write image from/to stream instead of files.
The image type is given as parameter rather then guess from extension.
(This is optional if it can determine the image type from the header bytes.)
Support of JPG, PNG, GIF.
(Bonus) Support of Animated GIF.
Use cases:
Generate thumbnails.
Add some banner texts.
Add cryptic image watermark and validation.
SWT has very little in terms of image processing. The standard JDK image stuff has the fancy ImageIO package, and Java2D which can do pretty much everything. I'd call this a no-brainer: use Java's built-in stuff, and don't try to use SWT for something it's not intended for.
I can second Ernest's notion about SWT for this task, forget it.
But while Swing is fine for image manipulation and output, Swing's ImageIO fails too often during input: many images which you'll meet in the wild, and which work fine in the browser, produce Exceptions.
The best Java option I know of is JAI, which unfortunately is a pain to use. So do yourself a favor, and use JAI for input, and Swing for the rest, like this:
RenderedImage renderedImage = JAI.create("fileload", imageFile.getAbsolutePath());
RenderedImageAdapter planarImage = new RenderedImageAdapter(renderedImage);
BufferedImage image planarImage.getAsBufferedImage();
JAI will also fail in rare cases (JPGs with custom color space, as written by Photoshop, are an example). If you want to do even better, use ImageMagick, which is an ultra-powerful command line tool. There are Java interfaces available, which either provide an API for the command line, or use JNI to call the native library, see here.
I have a database which contains picture data stored as a binary blob. The documentation says the data is encoded using LZW. I thought that I could decode it using the Zip or GZip input streams found in the Java library, but it didn't work - I got an exception that said the format of the data is not correct.
From what I've read, the library uses DEFLATE, which is not LZW. Also, I've read about some licensing problems for using the LZW algorithm.
What can I use to decode the data? Is there a library? Do I have to implement it myself? What about the licensing problems?
I know the question is old, but I just wanted to add a great resource about LZW:
http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp
It's more specifically about the use of LZW in GIF images, but it explains the compression and decompression algorithms pretty well.
Here are a couple of links:
http://www.cs.sfu.ca/CC/365/li/squeeze/LZW.html
http://u.cs.biu.ac.il/~freskom1/AlgProg1/Progs/LZW.java
http://www.codeproject.com/KB/java/lzw.aspx
And there are others.
Indeed if the images are LZW compressed TIFF files, The Java Advanced Imaging API apparently supports decoding directly (though not encoding it seems).
You can also try with 7-Zip JBinding which uses the 7zip library internally. It's quite easy to use.
I went through a surprising amount of LZW implementations before finding one that worked for my case.
UncompressedInputStream from the BioJava project worked for me, when I needed to unpack a .pax file.