Android Question OCR application using gallery images

eng.khalidvb

Member
Licensed User
Longtime User
Hi all,

I would thank Erle for this example :
http://www.b4x.com/android/forum/th...es-to-your-android-application.27080/#content

but could using this example to import image from gallery not from camera to be uploaded to the website for OCR , what is the code it will be ?

second, the result which come back from the website online OCR service can be show in edit text box not in the web view or is there any way to copy the text direct from webview to the edit text directly ?


Thanks

Regards
eng.khalidvb
 

eng.khalidvb

Member
Licensed User
Longtime User
OK I try this solution but I failed the result come bak as html code not as extracted text see this is my code :

B4X:
'Activity module
Sub Process_Globals
    Private template As String
    Private user = "khalidvb", license = "381CE5F0-4074-4A64-B593-A28EBD1AB968" As String
    Private sax As SaxParser
    Private ResultString As String
    Private tempDir, tempFile ="1.jpg" As String
   
'___________________________________________

    Dim dbsql As SQL
    Dim dbcurosr As Cursor
    Dim id As String
   
End Sub

Sub Globals

    Dim txt_note As EditText
    Dim ImageChooser1 As ContentChooser
   
    Dim rbmp As RSImageProcessing
    Dim bmp As Bitmap
   
    Dim image_view As ImageView
    Dim lbl_direction As Label
    Dim web_ocr As WebView
    Dim txt_ocr As EditText
   
End Sub
Sub imageChooser_Result (Success As Boolean, pathname As String, FileName As String)

    If Success Then
   
  image_view.Bitmap  = LoadBitmap(pathname, FileName)
  image_view.Gravity = Gravity.FILL
  bmp.Initialize(pathname, FileName)
    Else
          ToastMessageShow("No image selected", True)
    End If
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   
    If FirstTime Then
        'If File.ExternalWritable Then tempDir = File.DirDefaultExternal Else tempDir = File.DirInternalCache
        If File.ExternalWritable Then tempDir = File.DirRootExternal  Else tempDir = File.DirInternalCache
        sax.Initialize
        template = File.ReadString(File.DirAssets, "template.xml")
    End If
   
    ' menu items
   
   
    Activity.LoadLayout("editimage_activity")
    Activity.AddMenuItem("Rotate","rotate")
    Activity.AddMenuItem("OCR","ocr")
    Activity.AddMenuItem("Save","save")
    Activity.AddMenuItem("Cancel","cancel")
   
   
    '________________________________________________________________________________________
   
    ' load image from gallery
ImageChooser1.Initialize("imageChooser")
ImageChooser1.show("image/*", "Select image")

    '________________________________________________________________________________________
    'connect to the database
   
If File.Exists(File.DirInternal,"csa.db") = False Then
        File.Copy(File.DirAssets,"csa.db",File.DirInternal,"csa.db")
    End If
    If dbsql.IsInitialized = False Then
        dbsql.Initialize(File.DirInternal, "csa.db", False)
    End If
   
End Sub

Sub Activity_Resume
' load image from gallery
' ImageChooser1.Initialize("imageChooser")
' ImageChooser1.show("image/*", "Select image")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub rotate_click
  'load and initalise the bitmap
  bmp = rbmp.Rotate(bmp, -90)
  image_view.Bitmap=bmp 'Put bitmap into imageview
End Sub

Sub ocr_click()

'If File.Exists(File.DirRootExternal, "image_view") = False Then
If File.Exists(tempDir, tempFile) = False Then

        ToastMessageShow("Cannot find picture file.", True)
        Return
    End If
    ProgressDialogShow2("Sending data...", False)
    Dim job As HttpJob
    job.Initialize("ocr", Me)
    job.PostString("http://www.ocrwebservice.com/services/OCRWebService.asmx", _
        BuildRequest(ReadFile(tempDir, tempFile)))
    job.GetRequest.SetContentType("text/xml; charset=utf-8")
    job.GetRequest.SetHeader("SOAPAction", """http://stockservice.contoso.com/wse/samples/2005/10/OCRWebServiceRecognize""")
   
End Sub

Sub save_click

If txt_ocr.Visible = False Then
        Msgbox("No extracted text to save","No text")
Else
   
    'Grab the last ID number which is the highest number
    dbcurosr = dbsql.ExecQuery("SELECT id FROM csanote")
   
    If dbcurosr.RowCount > 0 Then
        For i = 0 To dbcurosr.RowCount - 1   
    dbcurosr.Position = i
   
    Dim NewID As Int
    NewID = dbcurosr.GetInt("id")
Next

End If
    NewID = NewID +1 ' add 1 to the ID number to make a new ID field
    dbsql.ExecNonQuery("INSERT INTO csanote VALUES('" & NewID & "','" & txt_ocr.Text & "')")
    Msgbox("Note saved","Save")
    StartActivity(Main)
    Activity.Finish
End If
End Sub

Sub cancel_click
Activity.Finish
End Sub
Sub ReadFile(Dir As String, FileName As String) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(100) 'size not really important
    File.Copy2(File.OpenInput(Dir, FileName), out)
  'File.Copy2(File.OpenInput(File.dirRootExternal, "image_view"), out)
    Return out.ToBytesArray
End Sub

Sub WriteFile(Dir As String, FileName As String, Data() As Byte)
    Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
End Sub

Sub BuildRequest (data() As Byte) As String
    Dim req As String = template
    req = req.Replace("$USER$", user)
    req = req.Replace("$LICENSE$", license)
    req = req.Replace("$FILENAME$", tempFile)
    Dim su As StringUtils
    req = req.Replace("$DATA$", su.EncodeBase64(data))
    req = req.Replace("$LANGUAGE$", "ENGLISH")
    req = req.Replace("$OUTPUT$", "HTML")
    Return req
End Sub


Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    ToastMessageShow("Job completed", False)
    If Job.Success Then
        File.WriteString(File.DirRootExternal, "1.xml", Job.GetString)
        ResultString = ""
        sax.Parse(Job.GetInputStream, "sax")
        If ResultString.Length > 0 Then
           
            image_view.Visible = False
            'CallSubDelayed2(showtxt_layout, "ShowText", ResultString)
           
            txt_ocr.Visible = True
            web_ocr.Visible = True
            web_ocr.LoadHtml(ResultString)
           
            txt_ocr.Text= ResultString
           
           
        End If
    Else
        ToastMessageShow(Job.ErrorMessage, True)
        Log(Job.ErrorMessage)
    End If
    Job.Release
End Sub

'Sub btn_paste_click()
'
'If web_ocr.Visible = True Then
'txt_ocr.Visible = True
'web_ocr.Visible = False
'Else
'Msgbox("Please Copy the text which recived and then Paste it to save"," Warrning")
'End If
'End Sub

Sub sax_StartElement (Uri As String, Name As String, Attributes As Attributes)
   
End Sub

Sub sax_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If Name = "fileData" Then
        Dim su As StringUtils
        Dim data() As Byte = su.DecodeBase64(Text)
        'Note that they use UTF16 for TXT output.
        ResultString = BytesToString(data, 0, data.Length, "UTF8")
        Log(ResultString)
    Else If Name = "errorMessage" Then
        Log("Error: " & Text)
    End If
End Sub


My be the the problem here in this code line :

B4X:
If FirstTime Then
        'If File.ExternalWritable Then tempDir = File.DirDefaultExternal Else tempDir = File.DirInternalCache
        If File.ExternalWritable Then tempDir = File.DirRootExternal  Else tempDir = File.DirInternalCache
        sax.Initialize
        template = File.ReadString(File.DirAssets, "template.xml")
    End If

can you give me a solution ? how to fix this problem ?

regards
eng.khalidvb
 
Upvote 0
Top