title: How to write Exif to a JPEG with TwelveMonkey's ExifWriter class
body: <p>Im using the <a href="https://github.com/haraldk/TwelveMonkeys" rel="nofollow">TwelveMonkey's</a> lib to read Exif data from jpeg like:</p>
  
  <pre><code>       try (ImageInputStream stream = ImageIO.createImageInputStream(input)) {
              List&lt;JPEGSegment&gt; exifSegment = JPEGSegmentUtil.readSegments(stream, JPEG.APP1, "Exif");
              InputStream exifData = exifSegment.get(0).data();
              exifData.read(); // Skip 0-pad for Exif in JFIF
              try (ImageInputStream exifStream = ImageIO.createImageInputStream(exifData)) {
                  return new EXIFReader().read(exifStream);
              }
         }
  </code></pre>
  
  <p>therefore I have a <code>CompoundDirectory</code> with a bunch of <code>Entry</code> elements. But how do I use the <code>ExifWriter</code> to a jpeg. Using it to write to the outputstream just corrupts the jpeg (image viewers think it is a broken tiff).</p>
  
  <p><strong><em>Update</em></strong>:
  What I like to achieve is reading a jpeg to a <code>BufferedImage</code>, also reading exif data, scaling it and then compressing it to jpeg again retaining the exif data (ie. writing the previously read data to the scaled out jpeg). For this I currently use some verbose version of <code>ImageIO</code> methods. Here is the basic code to do this currently: <a href="https://gist.github.com/patrickfav/5a51566f31c472d02884" rel="nofollow">https://gist.github.com/patrickfav/5a51566f31c472d02884</a> (exif reader seems to work, writer not of course)</p>