注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

你想快速掌握安卓编程吗

 
 
 

日志

 
 

拍摄照片发网上 OCR(一)  

2014-04-26 11:10:23|  分类: B.网络 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

拍摄照片发网上 OCR(一) - 沉默蜂QQ793136625 - Basic4android安卓编程
 例程由3个部分组成

拍摄照片发网上 OCR(一) - 沉默蜂QQ793136625 - Basic4android安卓编程
布局文件1

 
拍摄照片发网上 OCR(一) - 沉默蜂QQ793136625 - Basic4android安卓编程
布局文件2



————————————————  下面是Main活动视图代码  ————————————————



#Region 代码说明

'版本:2014-04-26
'注解:沉默蜂(QQ793136625)
'博客:http://silentbees.blog.163.com


'这是一个通过在本地拍摄照片,然后把照片上传到网上的例程
'但作者没有提供网络端的文件

'源码来源
'Tutorial [Example] Add OCR features To your Android application
'http://www.basic4ppc.com/android/forum/threads/example-add-ocr-features-To-your-android-application.27080/


#End Region

#Region  Project Attributes 
#ApplicationLabel: OCR Example
#VersionCode: 1
#VersionName: 
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
Private template As String
Private user = "ENTER USER", license = "LICENSE" As String
'库 XmlSax > 类 SaxParser
Private sax As SaxParser
Private ResultString As String
Private tempDir, tempFile = "1.jpg" As String
End Sub

Sub Globals
Dim CamEx As CameraExClass
Dim Panel1 As Panel
Dim btnTakePicture As Button
Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)

If FirstTime Then
'如果SD卡可用就设置拍照后图片保存路径
If File.ExternalWritable Then tempDir = File.DirDefaultExternal Else tempDir = File.DirInternalCache
sax.Initialize
template = File.ReadString( File.DirAssets, "template.xml")
End If
Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
'初始化相机
InitializeCamera
End Sub

'初始化相机,随后程序将自动调用Sub Camera1_Ready (Success As Boolean)事件
Private Sub InitializeCamera
CamEx.Initialize(Panel1, False, Me, "Camera1")
End Sub


'关闭相机对象
Sub Activity_Pause (UserClosed As Boolean)
CamEx.Release
End Sub

Sub Camera1_Ready (Success As Boolean)
If Success Then
'设置连续自动对焦
CamEx.SetContinuousAutoFocus
'设置JPG质量
CamEx.SetJpegQuality(95)
'使刚才的设置生效
CamEx.CommitParameters
'开始预览
CamEx.StartPreview
Else
ToastMessageShow("Cannot open camera.", True)
End If
End Sub


'拍照
Sub btnTakePicture_Click
If ImageView1.Visible = False Then
btnTakePicture.Enabled = False
CamEx.TakePicture
Else
ImageView1.Visible = False
btnTakePicture.Text = "Take Picture"
End If
End Sub

'显示拍摄的照片
Sub ShowImage
btnTakePicture.Text = "New Picture"
ImageView1.Visible = True
ImageView1.Bitmap = LoadBitmapSample( tempDir, tempFile, ImageView1.Width, ImageView1.Height)
End Sub

'保存照片
Sub Camera1_PictureTaken (Data() As Byte)
btnTakePicture.Enabled = True
btnTakePicture.Text = "New Picture"
CamEx.SavePictureToFile(Data, tempDir, tempFile)
ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(tempDir, tempFile), False)
CamEx.StartPreview
ShowImage
End Sub

'发送刚拍摄的照片到网上
Sub btnSend_Click

Return '为了防止乱发照片,沉默蜂加了个Return

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""")
'随后程序将自动调用Sub JobDone(Job As HttpJob)事件
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)
    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
CallSubDelayed2(ShowText, "ShowText", ResultString)
End If
Else
ToastMessageShow(Job.ErrorMessage, True)
Log(Job.ErrorMessage)
End If
Job.Release
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

 


  评论这张
 
阅读(404)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017