1 package eu.svjatoslav.imagesqueeze.sampleApplication;
3 import java.awt.BorderLayout;
5 import java.awt.Dimension;
6 import java.awt.image.BufferedImage;
7 import java.io.BufferedReader;
8 import java.io.ByteArrayOutputStream;
10 import java.io.FileInputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
14 import javax.swing.JButton;
16 import javax.swing.ImageIcon;
17 import javax.swing.JPanel;
18 import javax.swing.WindowConstants;
19 import javax.swing.JFrame;
20 import javax.swing.JLabel;
22 import eu.svjatoslav.imagesqueeze.codec.Image;
25 public class ImagePanel extends javax.swing.JPanel {
26 private JLabel imageLabel;
28 public BufferedImage bufferedImage;
31 * Auto-generated main method to display this
32 * JPanel inside a new JFrame.
34 public static void main(String[] args) {
35 JFrame frame = new JFrame();
36 frame.getContentPane().add(new ImagePanel());
37 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
39 frame.setVisible(true);
47 private void initGUI() {
49 BorderLayout thisLayout = new BorderLayout();
50 this.setLayout(thisLayout);
51 setPreferredSize(new Dimension(660, 500));
53 imageLabel = new JLabel();
54 this.add(getImageLabel(), BorderLayout.CENTER);
56 } catch (Exception e) {
61 public JLabel getImageLabel() {
65 public void loadImage(File inputFile, boolean isImgSqz) throws IOException{
66 FileInputStream fileInputStream = new FileInputStream(inputFile);
68 loadImage(fileInputStream, isImgSqz);
71 public void loadImage(InputStream inputStream, boolean isImgSqz) throws IOException{
73 // load ImageSqueeze file
75 Image image = new Image();
76 image.loadImage(inputStream);
78 bufferedImage = image.bufferedImage;
80 ImageIcon icon = new ImageIcon(bufferedImage);
81 //ImageIcon icon = new ImageIcon("sample data/original.png");
83 imageLabel.setIcon(icon);
86 // load JPEG, PNG, GIF file
87 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
90 int b = inputStream.read();
91 if (b == -1) break readLoop;
92 outputStream.write(b);
96 ImageIcon icon = new ImageIcon(outputStream.toByteArray());
98 bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
99 bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
101 ImageIcon displayIcon = new ImageIcon(bufferedImage);
102 imageLabel.setIcon(displayIcon);
107 public void createEmptyImage(Dimension dimension){
109 bufferedImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_3BYTE_BGR);
111 ImageIcon icon = new ImageIcon(bufferedImage);
113 imageLabel.setIcon(icon);
117 public void saveImage(File outputFile){
118 Image image = new Image(bufferedImage);
120 image.saveImage(outputFile);
121 } catch (Exception e) {
122 System.out.println("Error while saving image: " + e.toString());