converted jpg in b&w

D

David McDivitt

I am writing an app to harvest faxes from a fax server and load into a
database. Several things may be in the same fax, so different pages may be
used for different things. Users will work all the pages individually,
associating them with whatever. My initial plan was to save the original
TIFF file as however many JPEG files, one for each page.

Documentation on the JPEGEncodeParam class says to use the JPEG classes
directly if more control is needed. It also says a single-band color
defaults to grayscale. I need black and white.

To save space I may just save the TIFF file as it is in the database and
pull out images, converting to JPEG, as needed. That would require another
table and degrade performance since images would not already be converted.

If anyone knows how to save JPEGs as black and white please advise. Thanks


public static void main(String [] args) throws Exception {
JPEGEncodeParam param = new JPEGEncodeParam();
param.setQuality(0.25f);
SeekableStream s = new FileSeekableStream(new
File("C:/tiff/0862904.TIF"));
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, null);
int pageCount = dec.getNumPages();
if (pageCount > 9) pageCount = 9;
for (int page=0;page<pageCount;page++) {
RenderedImage image = dec.decodeAsRenderedImage(page);
FileOutputStream stream = new
FileOutputStream("C:/jpg/"+page+".jpg");
ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG",
stream, param);
encoder.encode(image);
stream.close();
}
}
 
O

Oliver Wong

David McDivitt said:
My initial plan was to save the original
TIFF file as however many JPEG files, one for each page.

Documentation on the JPEGEncodeParam class says to use the JPEG classes
directly if more control is needed. It also says a single-band color
defaults to grayscale. I need black and white.

JPEG is probably the wrong tool for the job then. JPEG uses the YCbCr
color space, where Y is the luminance, and and Cb and Cr are the chroma
components. A single-band JPEG essentially drops the Cb and Cr components,
so you only have luminance (i.e. values from white to black, and all the
greys in between).

You could in theory try to set Y to some sort of binary signal. If the
values range from 0 to 255, for example, you'd set Y to 0 whenever you want
black, and 255 whenever you want white. Then, due to the lossy nature of
JPEG, when you try to retrieve your data, you'd get back values like 10
(which you'd round down to 0) and 130 (which you'd round up to 255).

However, that seems like a lot of work, when instead you could switch
from JPEG to PNG. PNG supports a black-and-white-only color depth.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top