Process Image File in Java: Read RGB Info from Bitmap

  • Context: Java 
  • Thread starter Thread starter BicycleTree
  • Start date Start date
  • Tags Tags
    File Image Processing
Click For Summary

Discussion Overview

The discussion revolves around reading RGB information pixel-by-pixel from image files in Java, specifically focusing on Bitmap files and potential alternatives. Participants explore various methods and tools for image processing, including Java libraries and external applications.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks guidance on reading RGB data from Bitmap files in Java, expressing openness to alternative methods if they simplify the process.
  • Another suggests using ImageJ, providing links to its plugins and source code, but a later reply questions its suitability for the participant's needs.
  • Some participants mention the Java Advanced Imaging toolkit (JAI) as a potential solution, while others propose writing custom methods to handle Bitmap files directly.
  • There are suggestions to use Perl for image processing from the command line, but some participants emphasize the need to focus on Java solutions.
  • Multiple participants highlight the built-in image-processing functions in Java, specifically mentioning java.awt.Toolkit and javax.imageio.ImageIO for reading image files.
  • One participant reports encountering a null pointer exception when attempting to read a BMP file, leading to discussions about file format compatibility with different Java versions.
  • There are observations about unexpected behavior when processing different image files, including issues with image alignment and data integrity.
  • A participant notes that saving images in a specific format may resolve some issues encountered during processing.
  • Ultimately, one participant expresses success in transforming images into text after receiving assistance from others in the thread.

Areas of Agreement / Disagreement

The discussion features multiple competing views on the best approach to reading RGB data from Bitmap files. There is no consensus on a single method, and participants share differing opinions on the effectiveness of various tools and libraries.

Contextual Notes

Participants mention limitations related to Java version compatibility, particularly regarding support for BMP files. There are also indications of unresolved issues with specific image formats and their processing outcomes.

Who May Find This Useful

This discussion may be useful for Java developers interested in image processing, particularly those looking for methods to read and manipulate RGB data from Bitmap files or exploring alternative tools for image handling.

BicycleTree
Messages
519
Reaction score
0
I want to write a program in Java that reads RGB information pixel-by-pixel from an image file, processes that information, and then writes it to a text file. I know how to do everything except read from the image file. I'm thinking a Bitmap file might be the best choice for the image file, unless there's a simpler option.

I am not averse to using a "kludge" like transforming the image file into text using some other application and then working with that, if that would make the processing easier; I only care about the result. I have read that it is easy to do a text dump of a bitmap like that in Unix, but I'm using Windows XP. So, how does one work with image files? Thanks for any help.
 
Technology news on Phys.org
How do I use all this stuff? I can't tell if that's what I want or not. It seems that it was designed as a stand-alone program.

All I want to do is read RGB triplets from a bitmap file.
 
Sun has there own API for accessing and manipulating images It is called the Java Advanced Imaging toolkit (JAI). Documentation can be found here

An alternative would be to write your own methods to read in the header and map the file to some custom class(I would call it BMPmage or something like that) and perform you processing from there. BMP iis a pretty simple format, unlike say JPEG or even GIF that has compression. Here is a URL I found that documents the BMP format http://www.inversereality.org/tutorials/graphics%20programming/BitmapLoading.html
You should be able to google for more.

ImageJ's functionality doesn't seem easier to use than Sun's JAI, so I wouldn't see much point in using it. If you did go that route, it looks like you would download the source code for the application and use the classes to load your image file into an ImagePlus class, to perform manipulation on them. Browsing the source however, the API does not seem like it was that well designed (classes are too interdependent and not self-contained like they should be)
 
Last edited by a moderator:
Try using perl from the XP DOS command line. If you don't know it, read something about some of the instructions, these problems are nicely dealt with in perl.

example:

$_='p0.jpg';
open inf,$_; binmode inf;

while (read(inf,$buffer,6))
{ # here you can do whatever with each character of the binary file


execute from dos as c:\perl thisprogram.pl > result
 
Java contains built-in image-processing functions.

Use java.awt.Toolkit.createImage() or java.awt.Toolkit.getImage() to read a GIF, JPG, or PNG file into a java.awt.Image object.

Next, use java.awt.image.PixelGrabber to read the pixel data from the Image.

Whatever you do, don't try to follow oldtobor's horrible advice and try to decode the image yourself, byte-by-byte. Image manipulation libraries exist for Perl, also.

- Warren
 
chroot said:
Java contains built-in image-processing functions.

Use java.awt.Toolkit.createImage() or java.awt.Toolkit.getImage() to read a GIF, JPG, or PNG file into a java.awt.Image object.

Next, use java.awt.image.PixelGrabber to read the pixel data from the Image.

It sounds like he needs access to BMP, which is not supported by the AWT methods.

Whatever you do, don't try to follow oldtobor's horrible advice and try to decode the image yourself, byte-by-byte. Image manipulation libraries exist for Perl, also.
- Warren

I don't see how reading and parsing an BMP is that bad. Its a pretty simple, uncompressed image format.
 
oldtobor said:
Try using perl from the XP DOS command line. If you don't know it, read something about some of the instructions, these problems are nicely dealt with in perl.

example:

$_='p0.jpg';
open inf,$_; binmode inf;

while (read(inf,$buffer,6))
{ # here you can do whatever with each character of the binary file

execute from dos as c:\perl thisprogram.pl > result

He wants to do it in Java, not Perl.
 
I think you can use javax.imageio.ImageIO.read to read BMP files.
 
  • #10
Coda, ImageIO might be the trick. Here is the line I'm going to try:
BufferedImage input = ImageIO.read(new File(INPUT));
And then I can use input to get RGB triplets (I hope).
 
  • #11
I don't know, I keep getting a null pointer exception. I'm not sure how I'm supposed to write the filename--my best guess right now is "c:\\javawork\\textpics\\input.bmp", is that right?
 
  • #12
Ah, it was just that it couldn't read a bmp file. It's working with a gif file.
 
  • #13
BicycleTree said:
Ah, it was just that it couldn't read a bmp file. It's working with a gif file.

I know that it supports BMP files in the newest version of Java (1.5 or 5.0, whatever). It might not in the 1.4 version.
 
  • #14
Yeah, I have the 1.4.2 version. That image there was the output of the program when I just ran it (only a couple bugs). There still is a bug though because a line got chopped off the top of that. Hmm.
 
  • #15
Aha, it didn't get chopped off, it's just way over on the right.
 
  • #16
This is strange... I just changed the input file for another one and now it's not working. Maybe variation in the file format?
 
  • #17
It works! Almost! Now that is one strange error, with the right side of his head getting chopped off and put on the next line. I don't know what's causing that...
 
  • #18
(The trick, by the way, I believe lies in first saving it as a 24 bit bitmap before saving it as a gif; I think that ensures that the gif is in the right format)
 
  • #19
NOW it's working! Thanks a lot everyone for your help. Now I can transform a smiley into a text image in a matter of seconds :). If you posted in this thread and you want to see the code so you can do it too then ask me; I don't want to just post it for everyone to see.
 
Last edited:

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
Replies
65
Views
5K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
11K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
2
Views
1K