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
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
Voici quelques éléments, que tu pourras (devras) adapter :
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 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();
... par exemple.
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){} } }
oh lalla,
ca parait un difficil d'adapter ton code, y a pas de méthodes plus faciles.
merci quand meme.
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(); }
Merci de penser au tagquand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.
suivez mon blog sur Développez.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook
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(); } } }
Partager