How to load image in canvas using JPEGImageDecoder class in Java?

 * Created on 07-2011 Copyright(c) All Rights Reserved.
package com.kodehelp.image.codec;

import com.sun.image.codec.jpeg.JPEGCodec;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

 * This java code example show how to use JPEGImageDecoder class of  com.sun.image.codec.jpeg
 * package.
 * Created by
 * Date: 7/18/11
public class JPEGImageDecoder {

     public static void main(String[] args) throws Exception {
        JFrame jf = new JFrame("Demo");
        Container cp = jf.getContentPane();
        MyCanvas tl = new MyCanvas();
        jf.setSize(800, 500);

    static class MyCanvas extends JPanel {
        BufferedImage mImage;
        public MyCanvas() {
            try {
                InputStream in = getClass().getResourceAsStream("demo.jpg");
                com.sun.image.codec.jpeg.JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
                mImage = decoder.decodeAsBufferedImage();
            }catch (Exception e){
            ImageIcon icon = new ImageIcon(mImage);
            add(new JLabel(icon));

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.