JPEGImageDecoder: decodeAsBufferedImage() : JPEGImageDecoder « com.sun.image.codec.jpeg « Java by API






JPEGImageDecoder: decodeAsBufferedImage()

import java.awt.Container;
import java.awt.image.BufferedImage;
import java.io.InputStream;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;

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


public class MainClass {
  public static void main(String[] args) throws Exception {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}

class MyCanvas extends JPanel {
  BufferedImage mImage;
  public MyCanvas() {
  try {
    InputStream in = getClass().getResourceAsStream("myExampleImage.jpg");
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
    mImage = decoder.decodeAsBufferedImage();
    in.close();
  }
  catch (Exception e){
    System.err.println(e.getLocalizedMessage());
  }
    ImageIcon icon = new ImageIcon(mImage);
    add(new JLabel(icon));
  }
} 

           
       








Related examples in the same category