I'm attempting to save a BufferedImage as JPEG using ImageIO. But even when saving using 100 quality, I am suffering quality loss due to Chroma SubSampling.
I have successfully fixed this issue by reverting to the older JAI libraries and explicitly setting the Horizontal and Vertical Subsampling to 1.
However a critical requirement is that I save the image as Progressive, which JAI doesn't seem to support.
Is there a way to disable sub-sampling using the newer ImageWriter?
Thanks in advance
If possible, I'd like to avoid ImageMagick
A reading of JPEG Metadata Format Specification and Usage Notes seems to suggest that this is possible by writing the image with a custom IIOMetadata.
A search on the internet brings up https://codereview.appspot.com/3082041/patch/204004/210007, which seems to use ImageIO in exactly this manner.
I haven't tried it, and have no idea if it actually works.
Related
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.
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'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.
I developed a mass file up loader (a swing application) recently.One of the new requirements is to support uploading thousands of documents (GIF,JPG,PNG,DOCX,XLSX), each of these are like 3MB-10MB of size and we don't want to upload these huge files, we generally support TIFF files which has small byte size like 60KB-100KB. We are not concerned about the image quality, all we need to upload these docs for future reference. Right now I don't have any idea how to solve this problem, I started researching it. Please point me in right direction.
-PD
My first approach would be to convert them to pdf files. Everything that can be printed can be converted to pdf. This also allows for image compression. Tiff won't be a good idea for doc/xls I think, it might make them bigger.
a .doc or .xlsx can be gzipped very quickly for decent savings.
Images are more risky, depends on what the data is. Pictures of people? Pictures of text?
Canon/Nikon/other cameras save raw output of their sensor in some of their proprietary formats (.CR2, whatever). Is there any Java library designed to read them and convert into manageable BufferedImages?
I don't reqlly care here about fully customizable conversion similar to ufraw or imagemagick, rather something simple that "just works" for rendering simple previews of such images.
I've been where you are, and I feel for you. Your best bet is to use an Adobe or dcraw-based program to create thumbnails automatically. Temporary DNG files using Adobe's converter may be easier to use.
IF you insist on doing it in Java, you're about to run into a mountain of pain. RAW formats change often, have all sorts of crazy nuances and are intentionally hard to work with. Camera makers want you to use THEIR RAW conversion software, to show the camera's abilities at its best and screw Adobe. The guy behind dcraw found that some are camera manufacturers even use encryption now!
The existing Java libraries are poor -- JRawIO has improved since I last looked at it, but it supports only a fraction of the formats that dcraw does. In addition to the listed libraries, the imagero library may provide the ability to display a thumbnail for your image.
From personal experience, don't even think about writing your own RAW file reader.
I tried to do this with a very simple RAW format once (just a solid array of sensor data, 12 bits per pixel). The dcraw source translates badly to Java. You haven't seen such a nightmare of bit-fiddling ever. Took me days to debug problems with byte alignment and endian-ness.
jrawio is a plugin for Java Image I/O. With it you can read the raster data, the thumbnails and the metadata from the raw image file.
nef and cr2 already contains preview images in jpeg. just find the right offset and the right length to extract it...
Laurent Clevy # lclevy.free.fr/raw
Laurent
Unless you want to write you own file parser/loader (sounds fun imho ;) ), perhaps JMagick will help you. I haven't tried it and it might not work given your target platform since JMagick uses JNI.
UPDATE: dcraw looks like a good resource/reference