Encode a JPEG image

From CodeCodex

Implementations[edit]

Java[edit]

40px

This code requires com.sun.image.codec.jpeg, an external library, to run.
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;

public class JpegEncoder implements ImageEncoder {

	public byte[] encode(BufferedImage image) throws IOException {
		ByteArrayOutputStream result = new ByteArrayOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(result);
		encoder.encode(image);
		result.flush();
		return result.toByteArray();
	}

	public String getContentType() {
		return "image/jpeg";
	}

}

MATLAB / Octave[edit]

imwrite(ImageMatrix, "dsfargeg.jpg", QualityLevel);

OCaml[edit]

40px

This code requires camlimages, an external library, to run.

The "save" function in the "Jpeg" module of the camlimages library does this:

$ ocaml -I +camlimages ci_core.cma ci_jpeg.cma
        Objective Caml version 3.09.2

# Jpeg.save;;
- : string -> Images.save_option list -> Images.t -> unit = <fun>