Difference between revisions of "Encode a JPEG image"

From CodeCodex

(MATLAB / Octave)
Line 27: Line 27:
  
 
===MATLAB / Octave===
 
===MATLAB / Octave===
<syntaxhighlight language="matlab5">
+
<highlightsyntax language="matlab5">
 
imwrite(ImageMatrix, "dsfargeg.jpg", QualityLevel);
 
imwrite(ImageMatrix, "dsfargeg.jpg", QualityLevel);
</syntaxhighlight>
+
</highlightsyntax>
  
 
===OCaml===
 
===OCaml===

Revision as of 16:04, 22 August 2008

Implementations

Java

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

<highlightsyntax language="matlab5"> imwrite(ImageMatrix, "dsfargeg.jpg", QualityLevel); </highlightsyntax>

OCaml

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>