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

 Java Discussion :

Utilisation API C Ghostscript avec JNA


Sujet :

Java

  1. #1
    Membre habitué Avatar de snay13
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2009
    Messages : 236
    Points : 166
    Points
    166
    Par défaut Utilisation API C Ghostscript avec JNA
    Bonjour Je cherche à utiliser l'API ecrite en C de Ghoscript avec JNA
    j'ai ajouter jna à mon classPath et télécharger ghostscript avec la DLL gsdll32.dll mais je bloque pour son utilisation

    Je compte m'en servir pour eclater un fichier PDF en plusieurs images.

    Quelqu'un saurait il m'expliquer la procédure à suivre pour son utilisation
    Merci

  2. #2
    Membre habitué Avatar de snay13
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2009
    Messages : 236
    Points : 166
    Points
    166
    Par défaut
    Je poste mon code avec mes erreurs

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    import com.sun.jna.Library;
    import com.sun.jna.Native;
    import com.sun.jna.Pointer;
    import com.sun.jna.ptr.PointerByReference;
     
    public interface GhostscriptLibrary extends Library 
    {
    	public GhostscriptLibrary INSTANCE = (GhostscriptLibrary) Native.loadLibrary("gsdll32", GhostscriptLibrary.class);
    	public int gsapi_new_instance(PointerByReference instance, Pointer caller_handler);
    	public void gsapi_delete_instance(Pointer instance);
    	public int gsapi_init_with_args(Pointer instance, int argc, String[] argv);
    	public int gsapi_exit(Pointer instance);
    }
    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
     
    package javaapplication42;
     
    import com.sun.jna.ptr.PointerByReference;
     
    public class PdfExtractor
    {
        public void extractPdf(String pdfPath)
        {
    	GhostscriptLibrary gs = GhostscriptLibrary.INSTANCE;
    	PointerByReference instancePointer = new PointerByReference();
    	gs.gsapi_new_instance(instancePointer, null);
     
    	String[] argv = { "pdf2ps", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-sDEVICE=jpeg", "-dJPEGQ=100",
                        "-dCOLORSCREEN=true", "-dNOINTERPOLATE", "-dGraphicsAlphaBits=2",
                        "C:/tmp/test-gs-java-%d.jpg", "-r300x300", "c:/pdf" };
    	gs.gsapi_init_with_args(instancePointer.getValue(), argv.length, argv);
    	gs.gsapi_exit(instancePointer.getValue());
    	gs.gsapi_delete_instance(instancePointer.getValue());
        }
    }
    voila l'erreur quis'affiche quand j'execute
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'gsapi_new_instance': La procédure spécifiée est introuvable.
     
            at com.sun.jna.Function.<init>(Function.java:179)
            at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:344)
            at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:324)
            at com.sun.jna.Library$Handler.invoke(Library.java:203)
            at $Proxy0.gsapi_new_instance(Unknown Source)
            at javaapplication42.PdfExtractor.extractPdf(PdfExtractor.java:20)
            at javaapplication42.Main.main(Main.java:20)
    Apparemment il ne trouve pas la fonction
    J'ai pourtant bien la DLL dans c:/windows/system32

    Je ne vois pas ce qui me manque

  3. #3
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Sinon tu sais qu'il existe déjà un wrapper java pour cette API? Et puis qu'en plus ce wrapper est basé sur JNA?

    http://ghost4j.sourceforge.net/
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  4. #4
    Membre habitué Avatar de snay13
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2009
    Messages : 236
    Points : 166
    Points
    166
    Par défaut
    Merci pour ta reponse

    j'ai donc testé ghost4j avec l'example qui est fourni sur le site mais il me donne une erreur

    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
     
    import net.sf.ghost4j.Ghostscript;
    import net.sf.ghost4j.GhostscriptException;
     
    public class Main
    {
        public static void main(String[] args)
        {
            //get Ghostscript instance
            Ghostscript gs = Ghostscript.getInstance();
     
            //prepare Ghostscript interpreter parameters
            //refer to Ghostscript documentation for parameter usage
            String[] gsArgs = new String[10];
            gsArgs[0] = "-pdf2ps";
            gsArgs[1] = "-dNOPAUSE";
            gsArgs[2] = "-dBATCH";
            gsArgs[3] = "-dSAFER";
            gsArgs[4] = "-sDEVICE=pdfwrite";
            gsArgs[5] = "-sOutputFile=c:/azerty.ps";
            gsArgs[6] = "-c";
            gsArgs[7] = ".setpdfwrite";
            gsArgs[8] = "-f";
            gsArgs[9] = "c:/test.pdf";
     
            //execute and exit interpreter
            try 
            {
                gs.initialize(gsArgs);
                gs.exit();
            } catch (GhostscriptException e) 
            {
                System.out.println("ERROR: " + e.getMessage());
            }
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    run:
    log4j:WARN No appenders could be found for logger (net.sf.ghost4j.Ghostscript).
    log4j:WARN Please initialize the log4j system properly.
    GÉNÉRATION TERMINÉE (durée totale* 0 secondes)
    Pourtant j'ai bien ajouté ghost4j.jar, jna.jar et log4j.jar et gsdll32.dll

    apparemment log4j ne serait pas initialiser correctement

  5. #5
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Pour Log4J il te faut un fichier de configuration à minima.

    http://gfx.developpez.com/tutoriel/java/log4j/

    A noter que ce que tu obtiens n'est qu'un warning, et que ton programme s'est normalement bien déroulé.

    Si tu avais eu une erreur tu aurais eu log4j:ERROR au lieu de log4j:WARN au début des deux lignes de la console.
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  6. #6
    Membre habitué Avatar de snay13
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2009
    Messages : 236
    Points : 166
    Points
    166
    Par défaut
    Ok je te remercie pour le lien j'ai donc créer mon fichier de configuration et je n'est plus ces messages d'erreur

    log4j.properties
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    l# Set root category priority to DEBUG and its only appender to CONSOLE.
    log4j.rootCategory=DEBUG, CONSOLE
    # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.Threshold=INFO
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %c{1} %x - %m%n
    log4j.appender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c{1}] %m%n
    J'ai essyé le premier exemple mis sur le site de ghost4j il fonctionne, par contre le 2eme exemple qui m'interresse le plus car c'est le but de mon utilisation de ghostscript me renvoie un message d'erreur

    voici le code que j'utilise
    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
    57
    58
    59
    60
    61
     
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import net.sf.ghost4j.Ghostscript;
    import net.sf.ghost4j.GhostscriptException;
    import net.sf.ghost4j.display.ImageWriterDisplayCallback;
    import org.apache.log4j.PropertyConfigurator;
     
    public class Main
    {
        public static void main(String[] args)
        {
            PropertyConfigurator.configure("log4j.properties");
     
            //get Ghostscript instance
            Ghostscript gs = Ghostscript.getInstance();
     
            //create display callback (capture display output pages as images)
            ImageWriterDisplayCallback displayCallback = new ImageWriterDisplayCallback();
     
            //set display callback
            gs.setDisplayCallback(displayCallback);
     
            //prepare Ghostscript interpreter parameters with display device
            String[] gsArgs = new String[7];
            gsArgs[0] = "-dQUIET";
            gsArgs[1] = "-dNOPAUSE";
            gsArgs[2] = "-dBATCH";
            gsArgs[3] = "-dSAFER";
            gsArgs[4] = "-sDEVICE=display";
            gsArgs[5] = "-dDisplayHandle=0";
            gsArgs[6] = "-dDisplayFormat=16#804";
     
            //run PostScript (also works with PDF) and exit interpreter
            try 
            {
                gs.initialize(gsArgs);
                gs.runFile("c:/test.pdf");
                gs.exit();
            } 
            catch (GhostscriptException e) 
            {
                System.out.println("ERROR: " + e.getMessage());
            }
     
            //write images captured by the display callback to the disk in PNG format
            try 
            {
                for (int i = 0; i < displayCallback.getImages().size(); i++) 
                {
                    ImageIO.write((RenderedImage) displayCallback.getImages().get(i), "png", new File((i + 1) + ".png"));
                }
            }
            catch (IOException e) 
            {
                System.out.println("ERROR: " + e.getMessage());
            }
        }
    }
    et voila l'erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    [INFO] Ghostscript  - GPL Ghostscript 8.64 (2009-02-03)
    [INFO] Ghostscript  - Copyright (C) 2009 Artifex Software, Inc.  All rights reserved.
    [INFO] Ghostscript  - This software comes with NO WARRANTY: see the file PUBLIC for details.
    [INFO] Ghostscript  - Unrecoverable error: undefined in obj
    [INFO] Ghostscript  - Operand stack:
    [INFO] Ghostscript  -     5  0
    ERROR: Cannot run file on Ghostscript interpreter. Error code -21
    Ce code a pour but d'éclater un fichier pdf en plusieurs images PNG. J'ai testé avec plusieurs documents PDF

  7. #7
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    As tu déjà testé tes PDF directement avec Ghostscript?
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  8. #8
    Membre habitué Avatar de snay13
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2009
    Messages : 236
    Points : 166
    Points
    166
    Par défaut
    oui j'ai essayé de les ouvrir avec gswin32.exe situé dans le repertoire bin de gs là ou se situe le fichier dll de ghostscript sans aucun pb. J'utilise gs8.64

    Est ce que le pb proviendrait plutot de ma version de la DLL que du code en lui meme?

  9. #9
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Possible, je n'en sais rien à vrai dire.
    Une bonne idée serait de poser la question sur le forum du projet en lui même dans la section help: http://sourceforge.net/projects/ghos...s/forum/886757
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  10. #10
    Membre habitué Avatar de snay13
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2009
    Messages : 236
    Points : 166
    Points
    166
    Par défaut
    Ok je te remercie
    J'ai déja bien avancé pour l'utilisation de GS

    J'ai posté un message sur le forum et je reviens si j'ai du nouveau

    Si quelqu'un a déja eu ce pb ou voit d'ou ca vient je suis preneur

  11. #11
    Membre habitué Avatar de snay13
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2009
    Messages : 236
    Points : 166
    Points
    166
    Par défaut
    Voila, je reviens avec la reponse à mon problème
    Mon exemple ne fonctionnait car je n'intégrer pas la totalité des fichiers GhostScript

    En tout cas j'ai réussi à avoir ce que je voulais, c'était à dire pouvoir afficher un pdf dans un jpanel et Ghostscript est de loin le meilleur
    Je les ai tous essayé (les OpenSource) PDFRenderer, PDFBox, IcePdf et aucun ne lui arrive à la cheville

    Pour utiliser ghostScript pour éclater un PDFen Image PNG : ajouter Ghost4j et donc JNA et log4j, installer GhostScript 8.71 et récupérer la DLL gsdll32.dll pour l'ajouter au classpath

    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
     
            Ghostscript gs = Ghostscript.getInstance();
     
            //Paramètres ligne de commandes
            String[] gsArgs = new String[7];
            gsArgs[0] = "-dSAFER";
            gsArgs[1] = "-dBATCH";
            gsArgs[2] = "-dNOPAUSE";
            gsArgs[3] = "-sDEVICE=png16m";
            gsArgs[4] = "-r150";
            gsArgs[5] = "-sOutputFile=" + dossier + "/%d.png";
            gsArgs[6] = fichier;
     
            try
            {
                gs.initialize(gsArgs);
                gs.exit();
            }
            catch (GhostscriptException e)
            {
                System.out.println("ERROR: " + e.getMessage());
            }
    Dans le répertoire d'installation de GhostScript il y a toutes les lignes de commandes dans le dossier "doc"

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. utiliser BULK API de SalesForce avec C#
    Par KhaledZero dans le forum Salesforce.com
    Réponses: 10
    Dernier message: 23/04/2014, 09h54
  2. Réponses: 0
    Dernier message: 30/04/2013, 21h13
  3. Utilisation les API de SCRIBD avec l'ASP
    Par Adnane91 dans le forum ASP.NET
    Réponses: 8
    Dernier message: 16/05/2011, 13h48
  4. Réponses: 15
    Dernier message: 23/03/2009, 16h07

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