title: Scale multi page TIFF Image in java
body: <p>I want to change the height of multi page TIFF image so I am using below code snippet to scale it. But It returns just first page from the tiff file , I guess it converts it to JPEG image. How to retain all pages of the file??</p>
  
  <pre><code>public static byte[] scale(byte[] fileData, int width, int height) {
    System.out.println("width:::"+width+"::::height:::"+height);
    ByteArrayInputStream in = new ByteArrayInputStream(fileData);
    ByteArrayOutputStream buffer=null;
    BufferedImage img=null;
    BufferedImage imageBuff=null;
    try {
      ImageInputStream imageStream = ImageIO.createImageInputStream(new ByteArrayInputStream(fileData));
  
      java.util.Iterator&lt;ImageReader&gt; readers = ImageIO.getImageReaders(imageStream); 
      while(readers.hasNext()) {  
        ImageReader nextImageReader = readers.next();  
        nextImageReader.reset();  
      } 
  
      img = ImageIO.read(in);
      if(height == 0) {
        height = (width * img.getHeight())/ img.getWidth(); 
      }
      if(width == 0) {
        width = (height * img.getWidth())/ img.getHeight();
      }
      Image scaledImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
      imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      imageBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0,0,0), null);
  
      buffer = new ByteArrayOutputStream();
  
      ImageIO.write(imageBuff, "TIF", buffer);
  
  
    } catch (IOException e) {
      e.printStackTrace();
    } finally{
      if(img!=null){
        img.flush();
        img=null;
      }
      if(imageBuff!=null){
        imageBuff.flush();
        imageBuff=null;
      }
  
      if(buffer!=null){
        try {
          buffer.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
  
      if(in!=null){
        try {
          in.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
    return buffer.toByteArray();
  }
  </code></pre>
  
  <p><strong>UPDATED CODE:</strong></p>
  
  <pre><code>public static byte[] resize(byte[] img,int height,int width) throws IOException {
          byte[] outimage = null;
  
          ImageReader reader=null;
          ImageWriter writer=null;
          ByteArrayOutputStream baos=null;
          ImageOutputStream ios=null;
          ImageInputStream imageStream=null;
          try {
              baos = new ByteArrayOutputStream(30000);
              ios = ImageIO.createImageOutputStream(baos);
  
              reader = getTiffImageReader();
              imageStream= ImageIO.createImageInputStream(new ByteArrayInputStream(img));
              reader.setInput(imageStream);
  
              int pages = reader.getNumImages(true);
  
              Iterator&lt;ImageWriter&gt; imageWriters = ImageIO.getImageWritersByFormatName("TIFF");
              writer = imageWriters.next();
  
              writer.setOutput(ios);
              ImageWriteParam writeParam = writer.getDefaultWriteParam();
              writeParam.setTilingMode(ImageWriteParam.MODE_DEFAULT);
  
              writer.prepareWriteSequence(reader.getStreamMetadata()); 
  
              for (int i = 0; i &lt; pages; i++) {
                  //IIOImage iioImage = reader.readAll(i, null);
                  BufferedImage bufimage=null;
                  BufferedImage imageBuff=null;
                  bufimage=reader.read(i);
                  imageBuff=Thumbnails.of(bufimage).size(1200, 1200).asBufferedImage();
                  IIOImage scalediioImage = new IIOImage(imageBuff, null, null);
                  writer.writeToSequence(scalediioImage, writeParam);
                  bufimage.flush();
                  imageBuff.flush();
              }
  
              writer.endWriteSequence();
              outimage = baos.toByteArray();            
  
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          finally{
                  if(imageStream!=null){
                      imageStream.close();
                      }
                  if(ios!=null){
                      ios.flush();
                      ios.close();
                      }
  
                  if(baos!=null){
                      baos.close();
                  }
                  if(reader!=null){
                       reader.dispose();
                  }
                  if(writer!=null){
                      writer.dispose();
                  }
              }
          return outimage;
      }
  </code></pre>