Jump to content
Fivewin Brasil

Funcão para verificar JPG (RESOLVIDO)


emotta

Recommended Posts

Eu uso a classe TIMAGE para exibir a foto dos funcionários em meu sistema onde o nome do arquivo é CODIGO.JPG

Aconteceu essa semana do arquivo JPG estar danificado e ai quando era pra exibir a foto do funcionario selecionado o programa trava.

Existe uma maneira de verificar, antes de dar o LOAD na imagem, se o JPG é valido?

Ex:

If VerificaJPG(cFile)

ExibeJPG(cFile)

Else

MsgStop("Arquivo de imagem do funcionario está danificado. Verifique: "+cFile)

EndIf

É possivel isso de alguma maneira?

Link to comment
Share on other sites

Uma idéia... Veja se consegue pegar o retorno do WAITRUN()



#include "FiveWin.ch"
#include "Image.ch"

//----------------------------------------------------------------------------//

function Main()

LOCAL oDlg, oImage, lSetAlpha := .t.

DEFINE DIALOG oDlg FROM 0, 0 TO 22, 60 ;
TITLE FWDESCRIPTION + " JPG,JIF,GIF,BMP,DIB,RLE,TGA,PCX support!"

@ 0, 0 IMAGE oImage SIZE 150, 150 OF oDlg SCROLL // ADJUST

oImage:Progress( .f. )

@ 1, 28 BUTTON "Select Image" SIZE 50,10 OF oDlg ACTION GetImage( oImage )

@ 2, 28 BUTTON "Print" SIZE 50,10 OF oDlg ACTION PrintImage( oImage )

@ 3, 28 BUTTON "Copy" SIZE 50, 10 OF oDlg ;
ACTION oImage:CopyToClipboard()

@ 4, 28 BUTTON "Paste" SIZE 50, 10 OF oDlg ;
ACTION ( oImage:LoadFromClipboard(), oImage:Refresh() )

@ 5, 28 BUTTON "Save" SIZE 50, 10 OF oDlg ;
ACTION ( oImage:SaveImage( "SAVED.JPG", 2, 25 ), MsgInfo( "saved as saved.jpg" ) )

@ 6, 28 BUTTON "Exit" SIZE 50, 10 OF oDlg ACTION oDlg:End()

@ 10, 26 CHECKBOX oImage:lStretch PROMPT "Stretch" SIZE 50, 10 OF oDlg ;
ON CHANGE ( oImage:ScrollAdjust(), oImage:Refresh() )

@ 11, 26 CHECKBOX lSetAlpha PROMPT "Set Alpha Channel" SIZE 80, 10 OF oDlg ;
ON CHANGE ( SetAlpha( lSetAlpha ), oImage:Refresh() )


ACTIVATE DIALOG oDlg CENTER

return nil

//----------------------------------------------------------------------------//

function GetImage( oImage )

local lSucesso := .F., cOutFile

local gcFile := cGetFile( "Bitmap (*.bmp)| *.bmp|" + ;
"DIB (*.dib)| *.dib|" + ;
"PCX (*.pcx)| *.pcx|" + ;
"JPEG (*.jpg)| *.jpg|" + ;
"GIF (*.gif)| *.gif|" + ;
"TARGA (*.tga)| *.tga|" + ;
"RLE (*.rle)| *.rle|" + ;
"All Files (*.*)| *.*" ;
,"Please select a image file", 4 )

if ! Empty( gcFile ) .and. File( gcFile )

cOutFile := gcFile

// ? cOutFile

WAITRUN( GetEnv( 'ComSpec' )+' /C START MSPaint "'+cOutFile+'"', 0)

// SO NAO SEI PEGAR O RETORNO DO WAITRUN()
/*
IF lSucesso // .T.

? [Sucesso]

oImage:LoadBmp( gcFile )

ELSE

? [Ferrou a foto, voce e muito feio mano. kkkkkkkkkkkk]

ENDIF
*/

endif

return nil


//----------------------------------------------------------------------------//

function PrintImage( oImage )

local oPrn

PRINT oPrn NAME "Image Printing" PREVIEW
PAGE
oPrn:SayImage( 0, 0, oImage )
ENDPAGE
ENDPRINT

return nil


Link to comment
Share on other sites

Pessoal fiz a função, fucei na classe TIMAGE e encontrei uma maneira de identificar se o arquivo é uma imagem válida. Segue a função e o exemplo de uso.

Obrigado a todos por tentarem ajudar ! sempre é válido ! vlwww

Function u_Teste()
Local cFile := cGetFile( "*.*", "Seleccione um arquivo" )
If ValidJpg(cFile)
   Mensagem("Imagem válida")
Else
   Mensagem("Imagem inválida")
EndIf
Return

Static Function ValidJpg(cArq)
Return FITypeFromMemory(MemoRead(cArq)) >= 0
Link to comment
Share on other sites

Grande Kapiaba, vlw e vlw pelos créditos !

Depois vou enviar ao Linares a classe TImagem alterada para pelo menos não travar qdo se der um LOAD de uma imagem inválida.

abraços

Link to comment
Share on other sites

Se alguém quiser eliminar de vez o problema de travar o programa ou ficar doidão ao abrir um JPG inválido via TIMAGE é só compilar o código abaixo no seu script de compilação. Ele vai substituir a função padrão do LOADIMAGE por uma que verifica que a imagem é válida, se for inválida em RUNTIME ele cria uma imagem JPG em branco em uma pasta temporária e exibe ela.

No seu código, antes de dar o LOADIMAGE você pode verificar se a imagem é valida através do ISVALIDIMAGE mas caso não verifique a rotina internamente faz o tratamento e exibe o JPG em branco, pelo menos não vai mais travar.


//----------------------------------------------------------------------------//

#ifndef CBM_INIT
   #define CBM_INIT       4
#endif
#ifndef DIB_RGB_COLORS
   #define DIB_RGB_COLORS 0
#endif

function FILOADIMG( cFile, nFormat, cResFile )

   local hDib, hInfoH, hInfo, hBits, hWnd, hDC, hBmp
   local cFileTemp := ""

   if Upper( cFileExt( cFile ) ) = "BMP"
      return ReadBitmap( 0, cFile )
   endif
   
   If !IsValidImage(cFile)
      cFileTemp := TmpFile("jpg")
      CriaJPGEmBranco(cFileTemp)
      cFile := cFileTemp
   EndIf
   
   if LoadFreeImage( cResFile ) > 32

      nFormat = FIGETFILETYPE( cFile, 0 )
      hDib    = FILOAD( nFormat, cFile, 0 )
      hInfoH  = FIGETINFOHEADER( hDib )
      hInfo   = FIGETINFO( hDib )
      hBits   = FIGETBITS( hDib )
      hWnd    = GETDESKTOPWINDOW()

      #ifdef __CLIPPER__
         hDC = GETDC32( hWnd )
      #else
        hDC = GETDC( hWnd )
      #endif

      hBmp = CreateDiBitmap( hDC, hInfoH, CBM_INIT, hBits, hInfo, DIB_RGB_COLORS )

      ReleaseDC( hWnd, hDC )
      FIUNLOAD( hDib )

   endif
   
   If !Empty(cFileTemp)
      fErase(cFileTemp)
   EndIf

return hBmp

//----------------------------------------------------------------------------//

Function IsValidImage(cArq)
Return FITypeFromMemory(MemoRead(cArq)) >= 0

//----------------------------------------------------------------------------//

Static Function CriaJPGEmBranco(cFile)
Local cStr := "FFD8FFE000104A46494600010101006000600000FFE100224578696600004D4D002A00000008000101120003000000010001000000000000FFDB0043000201010201010202020202020202030503030303030604040305070607070706070708090B0908080A0807070A0D0A0A0B0C0C0C0C07090E0F0D0C0E0B0C0C0CFFDB004301020202030303060303060C0807080C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0CFFC00011080001000103012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5"
cStr+="100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B511000201020404030407050404000102770001020311040521310612415107617113"
cStr+="22328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00FDFCA28A2803FFD9"
MemoWrit(cFile,HexToSTr(cStr))
Return cFile

//----------------------------------------------------------------------------//

Static Function TmpFile(cExt)
Local cTmpDir := GetEnv( "TEMP" ) + "\"          // LK Mar/26/2008 added path to avoid file being created in _SET_DEFAULT directory
Local cTmpName := "__temp"
Default cExt := "tmp"

Do While File( cTmpName := cTmpDir + "__" + StrZero( nRandom( 99999 ), 5 ) + "."+cExt )
Enddo
FClose( FCreate( cTmpName ) )

Return cTmpName

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...