Dimensoes Imagem JPG

Projeto [x]Harbour - Compilador de código aberto compatível com o Clipper.

Moderador: Moderadores

Paulo Pereira
Usuário Nível 2
Usuário Nível 2
Mensagens: 79
Registrado em: 28 Abr 2011 00:29
Localização: Joinville / SC

Dimensoes Imagem JPG

Mensagem por Paulo Pereira »

Preciso se uma funcao para pegar as dimensoes de uma IMAGEM JPG
(altura x largura)
Imatech
Usuário Nível 3
Usuário Nível 3
Mensagens: 350
Registrado em: 24 Ago 2010 23:48
Localização: Goiânia-GO

Dimensoes Imagem JPG

Mensagem por Imatech »

hbFImage ( FreeImage.DLL )

ou

hbGD ( GDLib )



Disponiveis na pasta contrib do Harbour via SVN
M., Ronaldo

by: IMATECH

IMATION TECNOLOGIA
Paulo Pereira
Usuário Nível 2
Usuário Nível 2
Mensagens: 79
Registrado em: 28 Abr 2011 00:29
Localização: Joinville / SC

Dimensoes Imagem JPG

Mensagem por Paulo Pereira »

Obrigado.
Mas nao tem outra forma ?? So preciso das dimensoes da imagem, nao preciso manipular ou criar
.
Sou iniciante,, Uso modo console, nao consegui achar essa lib...
precisa compilar em C ??
Avatar do usuário
Pablo César
Usuário Nível 7
Usuário Nível 7
Mensagens: 5312
Registrado em: 31 Mai 2006 10:22
Localização: Curitiba - Paraná

Dimensoes Imagem JPG

Mensagem por Pablo César »

Tem outra em código C, indicado pelo grande Grigory Filatov no fórum da HMG. Coloquei o executável e o código-fonte para apreciação.

Segue o código em C como no exemplo em anexo:

Código: Selecionar todos

#pragma BEGINDUMP 

#include "windows.h"

#include "hbapi.h" 
#include "hbapifs.h"

/*
   Author: Andi Jahja <harbour@cbn.net.id>
*/
BOOL GetImageSize( const char *fn, int *x, int *y ) 
{ 
   unsigned char buf[24]; 
   long len; 

   FILE *f = hb_fopen( fn, "rb" ); 

   if (!f) 
      return FALSE; 

   fseek( f, 0, SEEK_END ); 

   len = ftell( f ); 

   fseek( f, 0, SEEK_SET ); 

   if ( len<24 ) 
   { 
      fclose( f ); 
      return FALSE; 
   } 

   // Strategy: 
   // reading GIF dimensions requires the first 10 bytes of the file 
   // reading PNG dimensions requires the first 24 bytes of the file 
   // reading JPEG dimensions requires scanning through jpeg chunks 
   // In all formats, the file is at least 24 bytes big, so we'll read 
   //that always 
   fread( buf, 1, 24, f ); 

   // For JPEGs, we need to read the first 12 bytes of each chunk. 
   // We'll read those 12 bytes at buf+2...buf+14, i.e. overwriting 
   // the existing buf. 
   if (buf[0]==0xFF && buf[1]==0xD8 && buf[2]==0xFF && buf[3]==0xE0 && 
       buf[6]=='J' && buf[7]=='F' && buf[8]=='I' && buf[9]=='F') 
   { 
     long pos = 2; 
     while ( buf[2]==0xFF ) 
     { 
       if (buf[3]==0xC0 || buf[3]==0xC1 || buf[3]==0xC2 || 
          buf[3]==0xC3 || buf[3]==0xC9 || buf[3]==0xCA || 
          buf[3]==0xCB) 
          break; 
       pos += 2+(buf[4]<<8)+buf[5]; 
       if ( pos+12>len ) 
          break; 
       fseek( f, pos, SEEK_SET ); 
       fread( buf+2, 1, 12, f); 
     } 
   } 

   fclose(f); 

   // JPEG: (first two bytes of buf are first two bytes of the jpeg 
   // file; rest of buf is the DCT frame 
   if (buf[0]==0xFF && buf[1]==0xD8 && buf[2]==0xFF) 
   { 
      *y = (buf[7]<<8) + buf[8]; 
      *x = (buf[9]<<8) + buf[10]; 
      return TRUE; 
   } 

   // GIF: first three bytes say "GIF", next three give version 
   // number. Then dimensions 
   if (buf[0]=='G' && buf[1]=='I' && buf[2]=='F') 
   { 
      *x = buf[6] + (buf[7]<<8); 
      *y = buf[8] + (buf[9]<<8); 
      return TRUE; 
   } 

   // PNG: the first frame is by definition an IHDR frame, which gives 
   // dimensions 
   if ( buf[0]==0x89 && buf[1]=='P' && buf[2]=='N' && buf[3]=='G' && 
       buf[4]==0x0D && buf[5]==0x0A && buf[6]==0x1A && buf[7]==0x0A 
       && buf[12]=='I' && buf[13]=='H' && buf[14]=='D' && 
       buf[15]=='R') 
   { 
      *x = (buf[16]<<24) + (buf[17]<<16) + (buf[18]<<8)+(buf[19]<<0); 
      *y = (buf[20]<<24) + (buf[21]<<16) + (buf[22]<<8)+(buf[23]<<0); 
      return TRUE; 
   } 

   return FALSE; 
} 

HB_FUNC( HB_GETIMAGESIZE ) 
/* 
  Syntax: HB_GETIMAGESIZE( cPicFile ) 
  Parameter: cPicFile = graphic file (JPG, GIF, PNG) 
  Return: 2 dim array -> array[1] = width, array[2] == height
*/ 
{ 
   int x = 0, y = 0; 

   GetImageSize( hb_parcx( 1 ), &x, &y ); 

   hb_reta( 2 ); 
   hb_storvni( x, -1, 1 ); 
   hb_storvni( y, -1, 2 ); 
} 

#pragma ENDDUMP
Outro exemplo tem no C:\MiniGUI\SAMPLES\Advanced\GdiPlus da Minigui que trabalha com GDI como o Ronaldo indicou, mas compor o código em C vai ser mais trabalhoso.
Anexos
Teste55.rar
(750.2 KiB) Baixado 199 vezes
Um clip-abraço !

Pablo César Arrascaeta
Compartilhe suas dúvidas e soluções com todos os colegas aqui do fórum.
Evite enviar as dúvidas técnicas por MPs ou eMails, assim todos iremos beneficiar-nos.
Paulo Pereira
Usuário Nível 2
Usuário Nível 2
Mensagens: 79
Registrado em: 28 Abr 2011 00:29
Localização: Joinville / SC

Dimensoes Imagem JPG

Mensagem por Paulo Pereira »

OPa..
Mexer com imagem nao é facil.. Funcionou.. valeu mesmo...
Responder