简体   繁体   中英

libjpeg-turbo java encode jpeg

Ive installed libjpeg-turbo on my mac and added the jar and libs to my eclipse project. Im trying to create jpeg byte[] from my bufferedimage:

TJCompressor tj = new TJCompressor();
tj.compress(image, TJ.PF_BGR);

The error I get is: java.lang.Exception: tjBufSize(): Invalid argument

I use a custom robot to create my bufferedimage with the type TYPE_3BYTE_BGR. I can create a byte[] using ImageIO but its to slow.

I tried to find some tutorials on the java wrapper, but I havnt found any that encode a jpeg from a bufferedimage. I would be very grateful if someone could help me.

You appear to be passing in the wrong type of argument into the method. You are passing in a pixel format when you should be passing in zero or more flags from the TJ class (prefixed with "FLAG_"). The pixel format type will be automatically detected by the TJCompressor object.

Make sure you set the chrominance subsampling level and JPEG quality to your desired levels before using the TJCompressor as well.

Sample code:

TJCompressor tj = new TJCompressor();
tj.setJPEGQuality(75);
tj.setSubsamp(TJ.SAMP_420);
byte[] jpegBytes = tj.compress(image, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM