converted jpg in b&w

Discussion in 'Java' started by David McDivitt, Jun 7, 2006.

  1. 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();
    }
    }
    David McDivitt, Jun 7, 2006
    #1
    1. Advertising

  2. David McDivitt

    Oliver Wong Guest

    "David McDivitt" <> wrote in message
    news:...
    > 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
    Oliver Wong, Jun 7, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roland
    Replies:
    0
    Views:
    1,042
    Roland
    May 12, 2004
  2. Matthias
    Replies:
    5
    Views:
    412
    Whitecrest
    Jun 1, 2004
  3. Yannick

    ASP changes Big JPG -> Small JPG

    Yannick, Feb 2, 2004, in forum: ASP General
    Replies:
    6
    Views:
    146
    Tim Williams
    Feb 3, 2004
  4. Vilmar Brazão de Oliveira
    Replies:
    14
    Views:
    328
    Aaron Bertrand [MVP]
    Mar 5, 2004
  5. Replies:
    3
    Views:
    250
    Andy Dingley
    Sep 27, 2006
Loading...

Share This Page