IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Imprimer une image avec java


Sujet :

Langage Java

  1. #1
    Membre habitué Avatar de adilou1981
    Profil pro
    Étudiant
    Inscrit en
    Février 2004
    Messages
    251
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2004
    Messages : 251
    Points : 127
    Points
    127
    Par défaut Imprimer une image avec java
    Salut les gars,

    je voudrais imprimer une image "png" avec java
    cette image est accessible par une url "http://toto"
    alors comment faire, je compte sur vous

    merci d'avance

  2. #2
    Membre confirmé Avatar de spekal
    Inscrit en
    Mai 2005
    Messages
    502
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 502
    Points : 510
    Points
    510
    Par défaut
    Voici quelques éléments, que tu pourras (devras) adapter :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
            bis = new BufferedInputStream(input de mon image);
            df = DocFlavor.INPUT_STREAM.PNG;
            attributes = new HashPrintRequestAttributeSet();
            ps = ServiceUI.printDialog(
                    null, 
                    50, 
                    60, 
                    PrintServiceLookup.lookupPrintServices(df, null), 
                    PrintServiceLookup.lookupDefaultPrintService(), 
                    null, 
                    attributes);
            if (ps != null)
            {
              job = ps.createPrintJob();
              d = new SimpleDoc(bis, df, null);
              ei = new EcouteurImprimante();
              job.addPrintJobListener(ei);
              job.print(d, null);
              ei.attenteToutFini();
            }
            bis.close();
    Et, pour EcouteurImprimante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
        private class EcouteurImprimante implements javax.print.event.PrintJobListener
        {
          private boolean fait;
     
          private EcouteurImprimante()
          {
            fait = false;
          }
     
          public void printDataTransferCompleted(PrintJobEvent pje)
          {
            cEstBon();
          }
     
          public void printJobCanceled(PrintJobEvent pje)
          {
            cEstBon();
          }
     
          public void printJobCompleted(PrintJobEvent pje)
          {
            cEstBon();
          }
     
          public void printJobFailed(PrintJobEvent pje)
          {
            cEstBon();
          }
     
          public void printJobNoMoreEvents(PrintJobEvent pje)
          {
            cEstBon();
          }
     
          public void printJobRequiresAttention(PrintJobEvent pje)
          {
            cEstBon();
          }
     
          private synchronized void cEstBon()
          {
            fait = true;
            notifyAll();
          }
     
          private synchronized void attenteToutFini()
          {
            try
            {
              while (!fait)
                wait();
            }
            catch (InterruptedException ie){}
          }
        }
    ... par exemple.

  3. #3
    Membre habitué Avatar de adilou1981
    Profil pro
    Étudiant
    Inscrit en
    Février 2004
    Messages
    251
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2004
    Messages : 251
    Points : 127
    Points
    127
    Par défaut
    oh lalla,
    ca parait un difficil d'adapter ton code, y a pas de méthodes plus faciles.
    merci quand meme.

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 854
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 854
    Points : 22 878
    Points
    22 878
    Billets dans le blog
    51
    Par défaut
    Probablement (quoi que je n'ai pas teste).
    Recuperer l'image localement avec ImageIcon ou ImageIO (ces deux classe acceptent une URL) et utiliser une classe implementant Printable et qui dessineras l'image dans le Graphics passe en parametre de la methode print(Graphics graphics, PageFormat pageFormat, int pageIndex)
    (attention a la taille et a l'orientation de la page).

    Ensuite :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);
    if (job.printDialog()) {
      job.print();
    }

  5. #5
    Membre habitué Avatar de adilou1981
    Profil pro
    Étudiant
    Inscrit en
    Février 2004
    Messages
    251
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2004
    Messages : 251
    Points : 127
    Points
    127
    Par défaut
    Ok merci je vais regarder

  6. #6
    Membre régulier
    Inscrit en
    Juillet 2006
    Messages
    209
    Détails du profil
    Informations forums :
    Inscription : Juillet 2006
    Messages : 209
    Points : 101
    Points
    101
    Par défaut
    voilà un code qui marche:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    import java.io.FileInputStream;
    import java.io.IOException;
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
     
    public class PrintImage {
     
        /** Creates a new instance of PrintImage */
        static public void main(String args[]) throws Exception {
        try {
          PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
          pras.add(new Copies(1));
     
          PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
     
          if (pss.length == 0)
            throw new RuntimeException("No printer services available.");
     
          PrintService ps = pss[0];
          System.out.println("Printing to " + ps);
     
          DocPrintJob job = ps.createPrintJob();
     
          FileInputStream fin = new FileInputStream("C:/graphe.png");
          Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
     
          job.print(doc, pras);
     
          fin.close();
        } catch (IOException ie) {
          ie.printStackTrace();
        } catch (PrintException pe) {
          pe.printStackTrace();
        }
      }
     
    }

Discussions similaires

  1. Affiche une image avec java?
    Par menoulette dans le forum Débuter
    Réponses: 9
    Dernier message: 29/05/2010, 12h36
  2. imprimer une canvas avec java
    Par hammasaidi dans le forum Graphisme
    Réponses: 2
    Dernier message: 20/04/2010, 14h28
  3. dessiner des cercles sur une image avec java
    Par inès83 dans le forum Traitement d'images
    Réponses: 12
    Dernier message: 04/05/2008, 17h34
  4. chargement d'une image avec java
    Par mona_81 dans le forum Interfaces Graphiques en Java
    Réponses: 5
    Dernier message: 23/07/2007, 00h54
  5. [Image]Créer une image avec JAVA 1.1
    Par burno dans le forum 2D
    Réponses: 4
    Dernier message: 11/08/2004, 09h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo