Differenze tra le versioni di "Estrarre immagini da un file PDF con le risorse del API di libpoppler-glib e di libcairo"

Da Gambas-it.org - Wikipedia.
Riga 54: Riga 54:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
   Dim percorso, en As String
+
   Dim uri, percorso, en As String
 
   Dim p, doc, pag, cai As Pointer
 
   Dim p, doc, pag, cai As Pointer
 
   Dim w, h, frm, err As Integer
 
   Dim w, h, frm, err As Integer
 
    
 
    
 +
  uri = "file://"
 
   percorso = "<FONT Color=gray>''/percorso/del/file.pdf''</font>"
 
   percorso = "<FONT Color=gray>''/percorso/del/file.pdf''</font>"
 
    
 
    
 
  <FONT Color=gray>' ''Carica il file PDF:''</font>
 
  <FONT Color=gray>' ''Carica il file PDF:''</font>
   doc = poppler_document_new_from_file("file://" & percorso, Null, VarPtr(p))
+
   doc = poppler_document_new_from_file(uri & percorso, Null, VarPtr(p))
   If doc = 0 Then Error.Raise("Error: " & poppler_error[String@(p)])
+
   If doc = 0 Then Error.Raise("Error: " & poppler_error[CInt(String@(p))])
 
    
 
    
 
  <FONT Color=gray>' ''Prende in considerazione la prima pagina (indice 0) del file pdf:''</font>
 
  <FONT Color=gray>' ''Prende in considerazione la prima pagina (indice 0) del file pdf:''</font>

Versione delle 07:57, 2 ago 2018

Se un file PDF possiede delle immagini, queste possono essere estratte.

E' necessario avere installata nel sistema e richiamare nel programma Gambas le librerie dinamiche condivise: "libpoppler-glib.so" e " libcairo:2"


Mostriamo un semplice esempio:

Library "libpoppler-glib"

Private poppler_error As String[] = [Null, "INVALID", "ENCRYPTED", "OPEN_FILE", "BAD_CATALOG", "DAMAGED"]

' popplerDocument *poppler_document_new_from_file (const char *uri, const char *password, GError **error)
' Creates a new PopplerDocument.
Private Extern poppler_document_new_from_file(uri As String, password As String, GError As Pointer) As Pointer

' PopplerPage * poppler_document_get_page (PopplerDocument *document, int index)
' Returns the PopplerPage indexed at index.
Private Extern poppler_document_get_page(document As Pointer, index As Integer) As Pointer

' cairo_surface_t * poppler_page_get_image (PopplerPage *page, gint image_id)
' Returns a cairo surface for the image of the page.
Private Extern poppler_page_get_image(page As Pointer, image_id As Integer) As Pointer

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(gobject As Pointer)


Library "libcairo:2"

Private cairo_format As String[] = ["CAIRO_FORMAT_ARGB32", "CAIRO_FORMAT_RGB24", "CAIRO_FORMAT_A8",
                                    "CAIRO_FORMAT_A1", "CAIRO_FORMAT_RGB16_565", "CAIRO_FORMAT_RGB30"]

' int cairo_image_surface_get_width (cairo_surface_t *surface)
' Get the width of the image surface in pixels.
Private Extern cairo_image_surface_get_width(surface As Pointer) As Integer

' int cairo_image_surface_get_height (cairo_surface_t *surface)
' Get the height of the image surface in pixels.
Private Extern cairo_image_surface_get_height(surface As Pointer) As Integer

' cairo_format_t cairo_image_surface_get_format (cairo_surface_t *surface)
' Get the format of the surface.
Private Extern cairo_image_surface_get_format(surface As Pointer) As Integer

' cairo_status_t cairo_surface_write_to_png(cairo_surface_t *surface, const char *filename)
' Writes the contents of surface to a new file filename as a PNG image.
Private Extern cairo_surface_write_to_png(cairo_surface As Pointer, filename As String) As Integer

' void cairo_surface_destroy (cairo_surface_t *surface)
' Decreases the reference count on surface by one.
Private Extern cairo_surface_destroy(cairo_surface As Pointer)


Public Sub Main()

 Dim uri, percorso, en As String
 Dim p, doc, pag, cai As Pointer
 Dim w, h, frm, err As Integer
  
  uri = "file://"
  percorso = "/percorso/del/file.pdf"
  
' Carica il file PDF:
  doc = poppler_document_new_from_file(uri & percorso, Null, VarPtr(p))
  If doc = 0 Then Error.Raise("Error: " & poppler_error[CInt(String@(p))])
  
' Prende in considerazione la prima pagina (indice 0) del file pdf:
  pag = poppler_document_get_page(doc, 0)
  If pag = 0 Then Error.Raise("Error !")
  
' Estrae la prima immagine (indice 0) presente nella pagina del file pdf:
  cai = poppler_page_get_image(pag, 0)
  If cai = 0 Then Error.Raise("Error !")
  
   ''''''''''''''''
   
' Ottiene alcune informazioni sull'immagine estratta:
   w = cairo_image_surface_get_width(cai)
   h = cairo_image_surface_get_height(cai)
   Print "Informazioni sull'immagine estratta"
   Print "Dimensioni: "; w; " x "; h; " pixel"
   frm = cairo_image_surface_get_format(cai)
   If frm = -1 Then
     en = "CAIRO_FORMAT_INVALID"
   Else
     en = cairo_format[frm]
   Endif
   Print "Formato:    "; en
  
' Salva i dati dell'immagine estratta in un'immagine di tipo PNG:
   err = cairo_surface_write_to_png(cai, "/tmp/immagine.png")
   If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !")
 
' Al termine libera la memoria precedentemente occupata:
 cairo_surface_destroy(cai)
 g_object_unref(pag)
 g_object_unref(doc)
  
End



Riferimenti