Skip to content

Instantly share code, notes, and snippets.

@TonyWhite
Created September 2, 2012 01:33
Show Gist options
  • Select an option

  • Save TonyWhite/3593426 to your computer and use it in GitHub Desktop.

Select an option

Save TonyWhite/3593426 to your computer and use it in GitHub Desktop.
ArtHTML
/**
* La classe ArtHTML converte in'immagine in una tabella HTML
*
* Autore Bianco Antonio
* Versione 1.0
*/
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import eu.kostia.gtkjfilechooser.ui.ImagePreviewer;
public class ArtHTML
{
/**
* Costruttore della classe ArtHTML
*/
public static void main(String args[])
{
if (args.length==0) // Se non gli passi alcun parametro carica la GUI
{
setTema();
apriMessaggio();
}
else if (args.length==1) // Se gli passi come parametro il percorso del file da convertire fa tutto senza GUI
{
String errore = "";
DisegnaHTML html = new DisegnaHTML();
try
{
errore = "L'immagine " + args[0] + " non esiste.";
html.apri(args[0]);
errore = "Impossibile convertire l'immagine " + args[0] + ".";
html.converti();
}
catch(Exception e)
{
System.out.println(errore);
System.exit(1);
}
}
else // Se gli hai passato più di 1 parametro
{
System.out.println("Devi passare solo 1 parametro: il percorso del file da convertire.");
}
System.exit(0); // Il programma termina correttamente
}
private static void apriMessaggio()
{
JFileChooser fc = new JFileChooser();
// Attiva l'anteprima delle immagini
ImagePreviewer anteprima = new ImagePreviewer(fc);
JComponent accessory = fc.getAccessory();
fc.setAccessory(anteprima);
if (accessory != null) ((ImagePreviewer)accessory).loadImage(null);
// Visualizza il JFileChooser
int returnVal = fc.showOpenDialog(null);
switch (returnVal)
{
case JFileChooser.APPROVE_OPTION:
String errore = "";
DisegnaHTML html = new DisegnaHTML(true);
String nomeFile = fc.getSelectedFile().getAbsolutePath();
try
{
errore = "L'immagine " + nomeFile + " non esiste.";
html.apri(nomeFile);
errore = "Impossibile convertire l'immagine " + nomeFile + ".";
html.converti();
JOptionPane.showMessageDialog(null, "Il file è stato creato con successo", "ArtHTML", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
// Errore di apertura
JOptionPane.showMessageDialog(null, errore, "ArtHTML", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
break;
case JFileChooser.CANCEL_OPTION:
// Apertura annullata
JOptionPane.showMessageDialog(null, "Apertura annullata", "ArtHTML", JOptionPane.INFORMATION_MESSAGE);System.exit(0);
break;
case JFileChooser.ERROR_OPTION:
// Errore
JOptionPane.showMessageDialog(null, "Errore su JFileChooser", "ArtHTML", JOptionPane.ERROR_MESSAGE);
System.exit(1);
break;
default:
// Errore sconosciuto
JOptionPane.showMessageDialog(null, "Errore interno: condizione non gestita.\nJFileChooser.showOpenDialog() ha ritornato " + returnVal + ".", "ArtHTML", JOptionPane.ERROR_MESSAGE);
System.exit(1);
break;
}
}
/**
* Carica il tema predefinito.
*/
private static void setTema()
{
try
{
String classeTemaPredefinito = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(classeTemaPredefinito);
// Correggi il FileChooser se il tema è GTK+
if ("GTK look and feel".equals(UIManager.getLookAndFeel().getName())) UIManager.put("FileChooserUI", "eu.kostia.gtkjfilechooser.ui.GtkFileChooserUI");
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}
/**
* La classe disegna l'immagine tramite una tabella HTML
*
* Autore: Antonio Bianco
*/
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class DisegnaHTML
{
// Istanza di variabili
private String immagine = "";
private BufferedImage immagine_sorgente;
private final String virgolette = ((char)((byte)34))+"";
private boolean gui = false;
private Finestra finestra;
/**
* Costruttore dell'oggetto
*/
public DisegnaHTML()
{
super();
}
/**
* Costruttore dell'oggetto
*/
public DisegnaHTML(boolean gui)
{
super();
this.gui = gui;
}
public void apri(String path_immagine) throws Exception
{
this.immagine = path_immagine;
immagine_sorgente = ImageIO.read(new File(path_immagine));
}
/**
* Converte l'immagine in una tabella HTML
*/
public void converti() throws Exception
{
// Legge le dimensioni dell'immagine
String documento = immagine + ".html"; //Documento senza i tag <html> e <body>: pronto per essere cop-incollato in un altro html
int larghezza=immagine_sorgente.getWidth(null);
int altezza=immagine_sorgente.getHeight(null);
int pixel;
// Crea la finestra con la barra di avanzamento
if (gui)
{
finestra = new Finestra("Leggo il file...");
finestra.setMax(altezza);
}
// Creazione del file
FileWriter f = new FileWriter(documento);
PrintWriter out = new PrintWriter(f);
// Crea la tabella HTML
out.print("<table cellpadding=0 cellspacing=0>\n");
// Crea la prima riga e imposta le larghezze delle colonne
if (gui) finestra.setValore(1, "Conversione riga 1 / " + (altezza+1));
out.print("<tr height=1>");
for(int x=0; x<larghezza; x++)
{
pixel = immagine_sorgente.getRGB(x, 0);
int alpha = (pixel >> 24) & 0x000000FF;
int red = (pixel >> 16) & 0x000000FF;
int green = (pixel >>8 ) & 0x000000FF;
int blue = (pixel) & 0x000000FF;
BigDecimal value = new BigDecimal((100*alpha)/255);
value = value.setScale(0, RoundingMode.HALF_UP);
double alphaVal = value.doubleValue()/100;
out.print("<td width=1 style="+virgolette+"background-color:rgba("+red+","+green+","+blue+","+alphaVal+");"+virgolette+"></td>");
}
out.print("</tr>\n");
/*
* Crea tutte le altre righe
* Non hanno bisogno di larghezza
*/
for(int y=1; y<altezza; y++)
{
if (gui) finestra.setValore((y+1), "Conversione riga " + (y+1) + " / " + (altezza+1));
else System.out.println("Riga " + (y+1) + " / " + (altezza+1));
out.print("<tr height=1>");
for(int x=0; x<larghezza; x++)
{
pixel = immagine_sorgente.getRGB(x, y);
int alpha = (pixel >> 24) & 0x000000FF;
int red = (pixel >> 16) & 0x000000FF;
int green = (pixel >>8 ) & 0x000000FF;
int blue = (pixel) & 0x000000FF;
BigDecimal value = new BigDecimal((100*alpha)/255);
value = value.setScale(0, RoundingMode.HALF_UP);
double alphaVal = value.doubleValue()/100;
out.print("<td width=1 style="+virgolette+"background-color:rgba("+red+","+green+","+blue+","+alphaVal+");"+virgolette+"></td>");
}
out.print("</tr>\n");
}
out.print("</table>");
// Chiudo il file
out.close();
if (gui) finestra.chiudi();
else System.out.println("La tabella è pronta");
}
}
/**
* La classe mostra la barra di avanzamento per far vedere che il programma non si è ancora bloccato.
*
* Autore: Antonio Bianco
* Finestra con una progressBar: talmente innovativa, minimalista figa e magica... che quasi quasi ci faccio un brevetto e querelo Samsung.
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class Finestra extends JFrame implements WindowListener
{
// Istanza di variabili
private JProgressBar progress;
private JLabel infoStato;
/**
* Costruttore dell'oggetto
*/
public Finestra(String stato)
{
// Inizializzazione delle variabili
super("ArtHTML");
this.impostaIcona("img/256/ArtHTML.png");
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setLayout(new BorderLayout());
infoStato = new JLabel(stato, JLabel.CENTER);
add(infoStato, BorderLayout.CENTER);
progress = new JProgressBar(JProgressBar.HORIZONTAL);
add(progress, BorderLayout.SOUTH);
Dimension dimensioni = Toolkit.getDefaultToolkit().getScreenSize();
pack();
setSize(400, 100);
setLocation((int)(dimensioni.getWidth()/2-getWidth()/2), (int)(dimensioni.getHeight()/2-getHeight()/2));
this.setVisible(true);
this.addWindowListener(this);
}
/** Imposta il valore massimo MASSIMO MASSIMO EHHHH!!! */
public void setMax(int massimo) {progress.setMaximum(massimo);}
/** Imposta il valore corrente */
public void setValore(int valore) {progress.setValue(valore);}
/** Imposta il valore corrente ed aggiorna lo stato */
public void setValore(int valore, String stato)
{
progress.setValue(valore);
infoStato.setText(stato);
}
/**
* Imposta l'icona per la finestra corrente
*/
private void impostaIcona(String path)
{
URL imgURL = Finestra.class.getResource(path);
if (imgURL != null)
{
setIconImage(Toolkit.getDefaultToolkit().getImage(imgURL));
}
else
{
System.err.println("Impossibile trovare " + imgURL.getPath() + "\nFile: " + imgURL.getFile());
}
}
/** Visualizza lo stato corrente */
public void setStato(String stato) {infoStato.setText(stato);}
/** Esce dall'applicazione con delicatezza, cazzo. */
public void chiudi()
{
if (progress.getMaximum()==progress.getValue())
{
this.setVisible(false);
this.dispose();
}
else if (JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(this,"Vuoi chiudere l'applicazione?", this.getTitle(), JOptionPane.YES_NO_OPTION))
{
this.setVisible(false);
this.dispose();
}
}
/**
* Ascoltatore della finestra
*/
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {chiudi();}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment