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 eu.svjatoslav.imagesqueeze.codec.Image;
16 import java.awt.image.BufferedImage;
19 public class ImagePanel extends javax.swing.JPanel {
20 private BufferedImage bufferedImage;
21 private JLabel imageLabel;
29 * Auto-generated main method to display this JPanel inside a new JFrame.
31 public static void main(final String[] args) {
32 final JFrame frame = new JFrame();
33 frame.getContentPane().add(new ImagePanel());
34 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
36 frame.setVisible(true);
39 public void createEmptyImage(final Dimension dimension) {
41 bufferedImage = new BufferedImage(dimension.width, dimension.height,
42 BufferedImage.TYPE_3BYTE_BGR);
44 final ImageIcon icon = new ImageIcon(bufferedImage);
46 imageLabel.setIcon(icon);
49 private JLabel getImageLabel() {
53 private void initGUI() {
55 final BorderLayout thisLayout = new BorderLayout();
56 setLayout(thisLayout);
57 setPreferredSize(new Dimension(660, 500));
59 imageLabel = new JLabel();
60 this.add(getImageLabel(), BorderLayout.CENTER);
62 } catch (final Exception e) {
67 public void loadImage(final File inputFile, final boolean isImgSqz)
70 try (final FileInputStream fileInputStream = new FileInputStream(inputFile)) {
71 loadImage(fileInputStream, isImgSqz);
75 public void loadImage(final InputStream inputStream, final boolean isImgSqz)
78 // load ImageSqueeze file
80 final Image image = new Image();
81 image.loadImage(inputStream);
83 bufferedImage = image.bufferedImage;
85 final ImageIcon icon = new ImageIcon(bufferedImage);
86 // ImageIcon icon = new ImageIcon("sample data/original.png");
88 imageLabel.setIcon(icon);
91 // load JPEG, PNG, GIF file
92 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
96 final int b = inputStream.read();
99 outputStream.write(b);
103 final ImageIcon icon = new ImageIcon(outputStream.toByteArray());
105 bufferedImage = new BufferedImage(icon.getIconWidth(),
106 icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
107 bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
109 final ImageIcon displayIcon = new ImageIcon(bufferedImage);
110 imageLabel.setIcon(displayIcon);
114 public void saveImage(final File outputFile) {
115 final Image image = new Image(bufferedImage);
117 image.saveImage(outputFile);
118 } catch (final Exception e) {
119 System.out.println("Error while saving image: " + e.toString());