-1

Hello I was able to convert a tif file to jpeg with the following code that I got from

https://stackoverflow.com/questions/15429011/how-to-convert-tiff-to-jpeg-png-in-java#=

String inPath = "./tifTest/113873996.002.tif";
String otPath = "./tifTest/113873996.002-0.jpeg";

BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
    input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
    output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);

    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int length;
    while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }

} catch (FileNotFoundException ex) {
    Logger.getLogger(TifToJpeg.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
    Logger.getLogger(TifToJpeg.class.getName()).log(Level.SEVERE, null, ex);
} finally {
    try {
        output.flush();
        output.close();
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This only works with one-page tif file, and when I use it with a multi-page tif, it only saves the first page.

How can I modified this to save a mymultipagetif.tif into:

  • mymultipagetif-0.jpeg
  • mymultipagetif-1.jpeg
  • mymultipagetif-2.jpeg

Thanks!

Community
  • 1
  • 1
Christian Salvador
  • 311
  • 1
  • 4
  • 12
  • 1
    Do you realise you got your code from the linked question rather than from one of its responses? The code in question is laughable, it reads from a file and writes back to the other without any transformation, only achieving to change the extension of the file... You obviously don't have to take lessons from a stranger on the net, but reading code rather than copy/pasting it would be a good first step to becoming more proficient in development. – Aaron Aug 23 '16 at 21:45

1 Answers1

0

This will takes a multi-page TIFF file (in the SeekableStream), extract the pages in the pages array ("1", "3", "4" for example) and write them into a single multi-page tiff into the file outTIffFileName. Modify as desired.

private String _ExtractListOfPages (SeekableStream ss, String outTiffFileName, String[] pages){
    // pageNums is a String array of 0-based page numbers.
    try {
        TIFFDirectory td = new TIFFDirectory(ss, 0);
        if (debugOn) {
            System.out.println("Directory has " + Integer.toString(td.getNumEntries()) +  " entries");
            System.out.println("Getting TIFFFields");
            System.out.println("X resolution = " +  Float.toString(td.getFieldAsFloat(TIFFImageDecoder.TIFF_X_RESOLUTION)));
            System.out.println("Y resolution = " +  Float.toString(td.getFieldAsFloat(TIFFImageDecoder.TIFF_Y_RESOLUTION)));
            System.out.println("Resolution unit = " +  Long.toString(td.getFieldAsLong(TIFFImageDecoder.TIFF_RESOLUTION_UNIT)));
            }
        ImageDecoder decodedImage = ImageCodec.createImageDecoder("tiff", ss, null);
        int count = decodedImage.getNumPages();
        if (debugOn) { System.out.println("Input image has " + count + " page(s)"); }
        TIFFEncodeParam param = new TIFFEncodeParam();
        TIFFField tf = td.getField(259); // Compression as specified in the input file
        param.setCompression(tf.getAsInt(0)); // Set the compression of the output to be the same.
        param.setLittleEndian(false); // Intel
        param.setExtraFields(td.getFields());
        FileOutputStream fOut = new FileOutputStream(outTiffFileName);
        Vector<RenderedImage> vector = new Vector<RenderedImage>();
        RenderedImage page0 = decodedImage.decodeAsRenderedImage(Integer.parseInt(pages[0]));
        BufferedImage img0 = new BufferedImage(page0.getColorModel(), (WritableRaster)page0.getData(), false, null);
        int pgNum;
// Adding the extra pages starts with the second one on the list.
        for (int i = 1; i < pages.length; i++ ) {
            pgNum = Integer.parseInt(pages[i]);
            if (debugOn) { System.out.println ("Page number " + pgNum); }
            RenderedImage page = decodedImage.decodeAsRenderedImage(pgNum);
            if (debugOn) { System.out.println ("Page is " + Integer.toString(page.getWidth()) + " pixels wide and "+ Integer.toString(page.getHeight()) + " pixels high."); }
            if (debugOn) { System.out.println("Adding page " + pages[i] + " to vector"); }
            vector.add(page);
        }
        param.setExtraImages(vector.iterator());
        ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", fOut, param);
        if (debugOn) { System.out.println("Encoding page " + pages[0]); }
        encoder.encode(decodedImage.decodeAsRenderedImage(Integer.parseInt(pages[0])));
        fOut.close();
    } catch (Exception e) {
        System.out.println(e.toString());
        return("Not OK " + e.getMessage());
    }
    return ("OK");
}
Duston
  • 1,601
  • 5
  • 13
  • 24