title: Compressing and converting a jpg to tiff in Java
body: <p>I have a jpg image and I want to convert it to a tiff file, but when I create the output file from byteArrayOutputStream, the output file has 0 byte length.</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      String root = "E:\\Temp\\imaging\\test\\";
  
      File image = new File(root + "0riginalTif-convertedToJpg.JPG");
  
      byte[] bytes = compressJpgToTiff(image);
      File destination = new File(root + "OriginalJpg-compressedToTiff.tiff");
      FileOutputStream fileOutputStream = new FileOutputStream(destination);
      fileOutputStream.write(bytes);
  }
  
  
  public static byte[] compressJpgToTiff(File imageFile) throws Exception {
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(255);
      ImageOutputStream imageOutputStream = null;
      try {
          File input = new File(imageFile.getAbsolutePath());
  
          Iterator&lt;ImageWriter&gt; imageWriterIterator = ImageIO.getImageWritersByFormatName("TIF");
          ImageWriter writer = imageWriterIterator.next();
          imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
          writer.setOutput(imageOutputStream);
  
          ImageWriteParam param = writer.getDefaultWriteParam();
          param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          param.setCompressionType("JPEG");
          param.setCompressionQuality(0.1f);
  
          BufferedImage bufferedImage = ImageIO.read(input);
          writer.write(null, new IIOImage(bufferedImage, null, null), param);
          writer.dispose();
          return byteArrayOutputStream.toByteArray();
  
      } catch (Exception e) {
  
          throw new RuntimeException(e);
      } finally {
          if (imageOutputStream != null)
              imageOutputStream.close();
          byteArrayOutputStream.close();
      }
  }
  </code></pre>
  
  <p>I want to reduce the size of output tiff as much as possible. Is there a better approach? Is it even possible to reduce the size of a tiff image? </p>