2 * Imagesqueeze - Image codec. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 3 of the GNU Lesser General Public License
6 * or later as published by the Free Software Foundation.
9 package eu.svjatoslav.imagesqueeze.sampleApplication;
11 import eu.svjatoslav.imagesqueeze.codec.Image;
15 import java.awt.image.BufferedImage;
18 public class ImagePanel extends javax.swing.JPanel {
19 private BufferedImage bufferedImage;
20 private JLabel imageLabel;
28 * Auto-generated main method to display this JPanel inside a new JFrame.
30 public static void main(final String[] args) {
31 final JFrame frame = new JFrame();
32 frame.getContentPane().add(new ImagePanel());
33 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
35 frame.setVisible(true);
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 private 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)
69 try (final FileInputStream fileInputStream = new FileInputStream(inputFile)) {
70 loadImage(fileInputStream, isImgSqz);
74 public void loadImage(final InputStream inputStream, final boolean isImgSqz)
77 // load ImageSqueeze file
79 final Image image = new Image();
80 image.loadImage(inputStream);
82 bufferedImage = image.bufferedImage;
84 final ImageIcon icon = new ImageIcon(bufferedImage);
85 // ImageIcon icon = new ImageIcon("sample data/original.png");
87 imageLabel.setIcon(icon);
90 // load JPEG, PNG, GIF file
91 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
95 final int b = inputStream.read();
98 outputStream.write(b);
102 final ImageIcon icon = new ImageIcon(outputStream.toByteArray());
104 bufferedImage = new BufferedImage(icon.getIconWidth(),
105 icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
106 bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
108 final ImageIcon displayIcon = new ImageIcon(bufferedImage);
109 imageLabel.setIcon(displayIcon);
113 public void saveImage(final File outputFile) {
114 final Image image = new Image(bufferedImage);
116 image.saveImage(outputFile);
117 } catch (final Exception e) {
118 System.out.println("Error while saving image: " + e.toString());