Image pixel manipulation: Use native APIs or 3rd party libraries? - java

I need to write an application in Java which need to be able to manipulate the pixels of an image regardless of the format of the image (JPEG, BitMap, Gif, ...).
It means I need to access the pixels and also I need to calculate some attributes of the image such as size, resolution, contrast, brightness and the compression algorithm.
I also need to manipulate the bits of individual pixels.
Now what I need to know is that can I do these by means of pure Java SE classes or I need to use a third party library? If so, which is the best for me? JAI, ImageJ, ...?

Take a look at the Java Advanced Imagining API FAQ and you should be able to decide from it whether you want to use the native Java APIs or a 3rd-party library. (The concept of manipulating pixels can vary tremendously in complexity. The examples you mentioned are not difficult but other operations may be.) Getting the size, resolution and compression alg is somewhat trivial as they are all attributes of the objects you use in image manipulation. (Though resolution is relative.) Working with contrast and brightness is a little more involved but not much. See here for an interesting sample. Java2s has lots of Java image-manipulation snippets available .
Here's a link directly to the Supported File Formats section in the FAQ.
JAI Hello World:
PlanarImage source = JAI.create("fileload", file);
This function automatically determines
the file format and produces an image
object which can be displayed or
processed.

Related

find compression mode of JPEG image

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.

How to read/write custom PNG metadata in Android?

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.

SWT vs AWT in image file process performance?

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.

Java applet screen capture to a video

I wanted to find out how can one capture screencast using java. I know that using Robot class one can get a screenshot but how do I go about capturing it as a video and then uploading it to the server? How exactly would that work?
ideas?
With a pure Java solution, I doubt that it will work, but it depends of course on what your interpretation of "video".
On my desktop with a 1920x1200 resolution, I am able to get about 20 frames per second when using the Java Robot to capture the entire screen. Since each image contains >6 MByte of uncompressed data, I would need more than 1 Gbps bandwidth to transmit the raw data of these images to a server. Most probably, requiring so much bandwidth is not acceptable, so you either have to decrease the number of frames per second or apply some kind of compression to the images.
One possibility is to compress each image using one of the image formats supported by ImageIO. The size of the compressed images will of course depend heavily on what is actually shown on the screen, but the performance of the compressors is not particularly good. Compressing to PNG ought to give the best lossless compression ratio for most desktop content, but at least my computer is only able to process just about 2 frames per second. Using the JPEG compressor with default quality settings reaches about 5 frames per second.
Using common video codecs through an abstraction layer like jffmpeg will probably achieve both better performance and better compression ratio, but I doubt that mainstream video codecs like WMV or H.264 are suitable for common desktop content.
If you really require a pure Java solution (and are not able to use any of the available standalone software, which do what you're asking for), I would make an attempt to implement my own, simple compression algorithm. With common desktop activity, there ought to be very little difference between most consecutive screen shots, so what might work quite well is to transmit the first frame completely and after that implement an algorithm to roughly detect rectangles, in which changes have been made and then transmit only these combined with JPG or preferrably (quality) PNG compression.
Or use Xuggler, a better wrapper for FFmpeg in Java. In fact, the code for capturing the screen and encoding the video is one of the standard tutorials.
I'm also curious about this. https://www.screencast.com/ is currently doing just this with a pure java (or at least straight out of the browser) experience.
You can just use something like Java to a native FFMPEG build, and execute the command line at runtime. Here is an applet that I made that does just that: http://have2chat.net/screencast/
I have downloaded the main capture *.JAR file for the Screencast-O-Matic.com. To download the file:
Go to http://screencast-o-matic.com/jars/ScreencastOMaticRun-1.0.5.jar
Save the file
Extract the contents (I DO NOT intend to use this commercially!)

Reading RAW images from Java

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

Categories

Resources