Hi,
I am trying to adapt this Github code in VB NET
/A9T9/Free-OCR-API-CSharp
I have create an accont and have a ApiKey
Await oHttpClient.PostAsync(“https://api.ocr.space/Parse/Image”, oListener) seam to be OK, but i do not know if the content is ok or not, and code block on following instruction:
sContenu = Await oReponse.Content.ReadAsStringAsync()
I Will put the code below,
Is somebody has an idea on what i do wrong ?
’ ------------------------------------------------------------------------
’ — VERSION 1.0 - 2022/08/08
’ ------------------------------------------------------------------------
Imports System.Drawing
Imports System.Linq
Imports System.Net.Http
Imports System.IO
Imports Newtonsoft.Json
Public Class ClassFreeOCR
Implements IDisposable
Public c_const_FreeOCRKey = "XXXXXXXXXXXX"
Public oHttpClient As New Net.Http.HttpClient()
' ------------------------------------------------------
' ---
' --- CONSTRUCTEUR
' ---
' ------------------------------------------------------
#Region “Constructeur”
‘’’
‘’’ New()
‘’’
Sub New()
Try
Catch ex As Exception
Dim oMethod As Reflection.MethodBase = Reflection.MethodBase.GetCurrentMethod()
Throw New Exception(ex.Message & Chr(10) & oMethod.DeclaringType.Name & "::" & oMethod.Name)
End Try
End Sub
#End Region
' ------------------------------------------------------
' ---
' --- METHODES PUBLIQUES
' ---
' ------------------------------------------------------
#Region “Methodes Publques”
‘’’
‘’’ _PerformOCRDenorm()
‘’’
#Region “_PerformOCRDenorm”
Sub _PerformOCRDenorm(sRepertoireFrom As String, sFichierFrom As String, sRepertoireTo As String)
Try
Dim oListener As New MultipartFormDataContent()
Dim oImageData() As Byte = Nothing
Dim sExt As String
oG.fCR.fSousTitre._Ecrire("Configuration du listener <MultipartFormDataContent>")
oHttpClient.Timeout = New TimeSpan(1, 1, 1)
sExt = UCase(DLLSystemIOFile._GetExtensionName(sFichierFrom))
'oListener.Add(New StringContent(c_const_FreeOCRKey), "apikey") '' Added api key In form data
'oListener.Add(New StringContent("helloworld"), "apikey") '' Added api key In form data
oListener.Add(New StringContent("French"), "language")
oListener.Add(New StringContent("2"), "ocrengine")
oListener.Add(New StringContent("true"), "scale")
oListener.Add(New StringContent("true"), "istable")
oImageData = File.ReadAllBytes(sRepertoireFrom & "\" & sFichierFrom)
Select Case sExt
Case "JPG", "PNG"
oListener.Add(New ByteArrayContent(oImageData, 0, oImageData.Length), "image", "image." & LCase(sExt))
Case "PDF"
oListener.Add(New ByteArrayContent(oImageData, 0, oImageData.Length), "PDF", "pdf." & LCase(sExt))
End Select
Dim oReponse As Threading.Tasks.Task(Of HttpResponseMessage)
oG.fCR.fSousTitre._Ecrire("Attente de la reponse <HttpResponseMessage>")
oReponse = _AttenteReponse(oListener)
Dim sContenu As Threading.Tasks.Task(Of String)
oG.fCR.fSousTitre._Ecrire("Lecture du contenu <HttpResponseMessage>")
sContenu = _LectureContenuReponse(oReponse.Result)
MessageBox.Show(sContenu.Result)
sExt = sExt
'Dim oContenu
'Dim oHttpReponse As HttpResponseMessage
'sContenu = oHttpReponse.Content.ReadAsStringAsync()
'sContenu = sContenu
'Dim oObjetOCR As New Object
'oObjetOCR = JsonConvert.DeserializeObject < Rootobject > (strContent);
Catch ex As Exception
Dim oMethod As Reflection.MethodBase = Reflection.MethodBase.GetCurrentMethod()
Throw New Exception(ex.Message & Chr(10) & oMethod.DeclaringType.Name & "::" & oMethod.Name)
End Try
End Sub
#End Region
#End Region
Async Function _AttenteReponse(oListener As MultipartFormDataContent) As Threading.Tasks.Task(Of HttpResponseMessage)
Dim oReponse As HttpResponseMessage
Try
'Dim sContenu As String
oReponse = Await oHttpClient.PostAsync("https://api.ocr.space/Parse/Image", oListener)
'sContenu = Await oReponse.Content.ReadAsStringAsync()
Catch ex As Exception
Dim oMethod As Reflection.MethodBase = Reflection.MethodBase.GetCurrentMethod()
Throw New Exception(ex.Message & Chr(10) & oMethod.DeclaringType.Name & "::" & oMethod.Name)
End Try
Return oReponse
End Function
Async Function _LectureContenuReponse(oReponse As HttpResponseMessage) As Threading.Tasks.Task(Of String)
Dim sContenu As String
Try
sContenu = Await oReponse.Content.ReadAsStringAsync()
Catch ex As Exception
Dim oMethod As Reflection.MethodBase = Reflection.MethodBase.GetCurrentMethod()
Throw New Exception(ex.Message & Chr(10) & oMethod.DeclaringType.Name & "::" & oMethod.Name)
End Try
Return sContenu
End Function
#Region “IDisposable Support”
Private disposedValue As Boolean ’ Pour détecter les appels redondants
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: supprimer l'état managé (objets managés).
End If
' TODO: libérer les ressources non managées (objets non managés) et remplacer Finalize() ci-dessous.
' TODO: définir les champs de grande taille avec la valeur Null.
End If
disposedValue = True
End Sub
' TODO: remplacer Finalize() seulement si la fonction Dispose(disposing As Boolean) ci-dessus a du code pour libérer les ressources non managées.
'Protected Overrides Sub Finalize()
' ' Ne modifiez pas ce code. Placez le code de nettoyage dans Dispose(disposing As Boolean) ci-dessus.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' Ce code est ajouté par Visual Basic pour implémenter correctement le modèle supprimable.
Public Sub Dispose() Implements IDisposable.Dispose
' Ne modifiez pas ce code. Placez le code de nettoyage dans Dispose(disposing As Boolean) ci-dessus.
Dispose(True)
' TODO: supprimer les marques de commentaire pour la ligne suivante si Finalize() est remplacé ci-dessus.
' GC.SuppressFinalize(Me)
End Sub
#End Region
End Class