1

I have REST service that allows users to upload images and then serves those images. The images are stored to a database. I'd like to do JPG optimization for these images.

There's several command line tools to do this but I'd like to do it without first saving them to disk and then running some command-line tools. I'd rather use some Java library to directly operate on a binary stream that contains the image data.

What I'm after is a treatment similar to what for example Trimage does:

  • Remove all EXIF metadata from the image
  • Losslessy (re)compressed to the highest available compression levels

Is this possible?

vertti
  • 7,539
  • 4
  • 51
  • 81
  • 1
    Short answer: yes, it is possible. Could you please enumerate specifically what do you want to do with image. Remove metadata. What else? – AlexR Mar 06 '14 at 05:36
  • you should use a more specific and technical wording, at that point even a web search will do the trick. For example with "metadata" you are probably referring to `exif` https://www.google.com/#q=java+strip+exif – user2485710 Mar 06 '14 at 05:39
  • My google searches return libraries that seem all to operate on files.. – vertti Mar 06 '14 at 06:00
  • http://stackoverflow.com/a/6667110/2485710 – user2485710 Mar 06 '14 at 06:03
  • @user2485710 that's a bad answer, check out it's last comment, it will do a _lossy_ recompress. – vertti Mar 06 '14 at 06:07
  • JPG is lossy, you should try it first, you are going to lose quality anyway, if you don't have not-so-high quality compression already; considering your first post you already have compressed images. – user2485710 Mar 06 '14 at 06:12
  • Nope, I'll leave the selection of lossy compression up to the graphics guys, I'll just take the "free" bandwidth saves I can get. – vertti Mar 06 '14 at 06:18
  • What do you mean by "Losslessy (re)compressed to the highest available compression levels"? It's not possible further compress a JPEG without losing quality. Do you simply mean "storing the image data, as it was, without any metadata"? PS: Many command line tools supports the concept of pipes, would that help you? – Harald K Mar 06 '14 at 08:51
  • PPS: You could perhaps use this [class to parse the JPEG structure](https://github.com/haraldk/TwelveMonkeys/blob/master/imageio/imageio-jpeg/src/main/java/com/twelvemonkeys/imageio/plugins/jpeg/JPEGSegmentImageInputStream.java) as a starting point, and filter out thumbnails, Exif, XMP, IPTC etc. – Harald K Mar 06 '14 at 08:57
  • 1
    @haraldK It is often possible to further compress jpegs without losing quality. Apparently it's based (atleast) on optimizing the Huffman tables. Besides that, metadata and sometimes color profile can be stripped. – vertti Mar 06 '14 at 10:25
  • @vertti Ok, fair point. – Harald K Mar 06 '14 at 10:47

1 Answers1

1

It is possible to remove extraneous APPn and COM markers from the data stream. You can do that without expanding and recompressing.

Each time you expand and decompress with different quantization tables, you loose data in JPEG. There is no real point in decompression.

Yes to question #1. No to question #2.

user3344003
  • 20,574
  • 3
  • 26
  • 62