2 * Imagesqueeze - Image codec optimized for photos.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.imagesqueeze.sampleApplication;
12 import java.awt.BorderLayout;
13 import java.awt.Dimension;
14 import java.awt.image.BufferedImage;
15 import java.io.ByteArrayOutputStream;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import javax.swing.ImageIcon;
22 import javax.swing.JFrame;
23 import javax.swing.JLabel;
24 import javax.swing.WindowConstants;
26 import eu.svjatoslav.imagesqueeze.codec.Image;
28 public class ImagePanel extends javax.swing.JPanel {
29 private JLabel imageLabel;
31 public BufferedImage bufferedImage;
38 public void createEmptyImage(final Dimension dimension) {
40 bufferedImage = new BufferedImage(dimension.width, dimension.height,
41 BufferedImage.TYPE_3BYTE_BGR);
43 final ImageIcon icon = new ImageIcon(bufferedImage);
45 imageLabel.setIcon(icon);
48 public JLabel getImageLabel() {
52 private void initGUI() {
54 final BorderLayout thisLayout = new BorderLayout();
55 setLayout(thisLayout);
56 setPreferredSize(new Dimension(660, 500));
58 imageLabel = new JLabel();
59 this.add(getImageLabel(), BorderLayout.CENTER);
61 } catch (final Exception e) {
66 public void loadImage(final File inputFile, final boolean isImgSqz)
68 final FileInputStream fileInputStream = new FileInputStream(inputFile);
70 loadImage(fileInputStream, isImgSqz);
73 public void loadImage(final InputStream inputStream, final boolean isImgSqz)
76 // load ImageSqueeze file
78 final Image image = new Image();
79 image.loadImage(inputStream);
81 bufferedImage = image.bufferedImage;
83 final ImageIcon icon = new ImageIcon(bufferedImage);
84 // ImageIcon icon = new ImageIcon("sample data/original.png");
86 imageLabel.setIcon(icon);
89 // load JPEG, PNG, GIF file
90 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
93 final int b = inputStream.read();
96 outputStream.write(b);
100 final ImageIcon icon = new ImageIcon(outputStream.toByteArray());
102 bufferedImage = new BufferedImage(icon.getIconWidth(),
103 icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
104 bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
106 final ImageIcon displayIcon = new ImageIcon(bufferedImage);
107 imageLabel.setIcon(displayIcon);
111 public void saveImage(final File outputFile) {
112 final Image image = new Image(bufferedImage);
114 image.saveImage(outputFile);
115 } catch (final Exception e) {
116 System.out.println("Error while saving image: " + e.toString());
121 * Auto-generated main method to display this JPanel inside a new JFrame.
123 public static void main(final String[] args) {
124 final JFrame frame = new JFrame();
125 frame.getContentPane().add(new ImagePanel());
126 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
128 frame.setVisible(true);