0

I want to rotate an jpeg image lossless.

My code works with success but is with loss:

ByteArrayInputStream inputstream = new ByteArrayInputStream(imgByteArraySource);
BufferedImage oldBufferedImage = ImageIO.read(inputstream);

BufferedImage newBufferedImage = rotate(oldBufferedImage, degrees);
ByteArrayOutputStream baos = new ByteArrayOutputStream();   
ImageIO.write(newBufferedImage, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();

I found an old post on this site: Lossless JPEG Rotate (90/180/270 degrees) in Java?

Maybe there is a better solution. I'm not very confident about the media-util library (it is old, and it is packaged in a .exe file).

Has someone an idea? Is there a new and better way to do this?

Thanks in advance.

Bobby

Community
  • 1
  • 1
BobyFish
  • 11
  • 7
  • 1
    The mediaUtil is not packed in an EXE file. In fact, the source code is available at http://mediachest.cvs.sourceforge.net/viewvc/mediachest/mediautil/ and in a ZIP file from the main website. – Marco13 Mar 11 '14 at 15:08
  • Thanks. The zip file contains an exe file and the code is very old. Furthermore, I'm not sure that my legacy department will accept this library. – BobyFish Mar 12 '14 at 08:16
  • Not sure what you are talking about, but I don't see an EXE file in http://sourceforge.net/projects/mediachest/files/MediaUtil/Version%201.0/mediautil-1.zip/download – Marco13 Mar 12 '14 at 09:08
  • You're right Marco, it's not an exe file. It's jar file. Thanks. – BobyFish Mar 12 '14 at 09:37

1 Answers1

0

It is only possible to rotate losslessly in 90 degree increments.

To do it in 90 increments, you need to either compress using the same quantization tables used in the original (which still may introduce loss, depending upon the encoder) or use a special program that rearranges the MCUs in the JPEG stream.

Any time you decompress a JPEG using different quantization tables you will get loss.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • Thanks, I want to rotate losslessly in 90 degree increments. When I do it 4 times (360 degree) I have a different image. And the problem is here. Have you ever tested and can you recommend me a library. – BobyFish Mar 12 '14 at 08:03