Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   converted jpg in b&w (http://www.velocityreviews.com/forums/t297746-converted-jpg-in-b-and-w.html)

David McDivitt 06-07-2006 04:42 PM

converted jpg in b&w
 
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();
}
}


Oliver Wong 06-07-2006 09:23 PM

Re: converted jpg in b&w
 

"David McDivitt" <x12code-del@del-yahoo.com> wrote in message
news:6hvd82hda4qhqg43olvkogbhdvapv6bkv5@4ax.com...
> 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



All times are GMT. The time now is 07:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.