在日常工作中,常遇到IE6多出一行字或一个字的情况,网上大多同仁说是注释问题。经查,不一定是注释才会引起该类问题,个人感觉是因为一些不可显示的元素存在在了具有 float css属性的元素内的问题,只要将这些不可显示的元素移出这个范围一般就会恢复,如: 脚本块,css块,注释块,隐藏表单等。
月度归档: 2010年12月
C# Speech 中文发音
我们要想实现中文发音或中文语音识别,必需先安装微软的Speech Application SDK(SASDK),它的最新版本是 SAPI 5.1 他能够识别中、日、英三种语言,你可以在这里下载:http://www.microsoft.com/speech/download/sdk51/,需要安装这两个文件Speech SDK 5.1和5.1 Language Pack,其中5.1 Language Pack可以选择安装支持的语言。 安装好以后,我们就可以开始进行语音程序的开发了。
下面我们设计一个能够朗读中英文混合语言的类:
我们将用单例模式实现该类,类的代码如下,我们将详细解释:
public class Speach
{ private static Speach _Instance = null ; private SpeechLib.SpVoiceClass voice =null; private Speach() { BuildSpeach() ; } public static Speach instance() { if (_Instance == null) _Instance = new Speach() ; return _Instance ; } private void SetChinaVoice() { voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ; } private void SetEnglishVoice() { voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ; } private void SpeakChina(string strSpeak) { SetChinaVoice() ; Speak(strSpeak) ; } private void SpeakEnglishi(string strSpeak) { SetEnglishVoice() ; Speak(strSpeak) ; } public void AnalyseSpeak(string strSpeak) { int iCbeg = 0 ; int iEbeg = 0 ; bool IsChina = true ; for(int i=0;i<strSpeak.Length;i++) { char chr = strSpeak[i] ; if (IsChina) { if (chr<=122&&chr>=65) { int iLen = i - iCbeg ; string strValue = strSpeak.Substring(iCbeg,iLen) ; SpeakChina(strValue) ; iEbeg = i ; IsChina = false ; } } else { if (chr>122||chr<65) { int iLen = i - iEbeg ; string strValue = strSpeak.Substring(iEbeg,iLen) ; this.SpeakEnglishi(strValue) ; iCbeg = i ; IsChina = true ; } } }//end for if (IsChina) { int iLen = strSpeak.Length - iCbeg ; string strValue = strSpeak.Substring(iCbeg,iLen) ; SpeakChina(strValue) ; } else { int iLen = strSpeak.Length - iEbeg ; string strValue = strSpeak.Substring(iEbeg,iLen) ; SpeakEnglishi(strValue) ; } } private void BuildSpeach() { if (voice == null) voice = new SpVoiceClass() ; } public int Volume { get { return voice.Volume ; } set { voice.SetVolume((ushort)(value)) ; } } public int Rate { get { return voice.Rate ; } set { voice.SetRate(value) ; } } private void Speak(string strSpeack) { try { voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ; } catch(Exception err) { throw(new Exception("发生一个错误:"+err.Message)) ; } } public void Stop() { voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ; } public void Pause() { voice.Pause() ; } public void Continue() { voice.Resume() ; } }//end class
|
在 private SpeechLib.SpVoiceClass voice =null;这里,我们定义个一个用来发音的类,并且在第一次调用该类时,对它用BuildSpeach方法进行了初始化。
我们还定义了两个属性Volume和Rate,能够设置音量和语速。
我们知道,SpVoiceClass 有一个Speak方法,我们发音主要就是给他传递一个字符串,它负责读出该字符串,如下所示。
private void Speak(string strSpeack)
{ try { voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ; } catch(Exception err) { throw(new Exception("发生一个错误:"+err.Message)) ; } }
|
其中SpeechVoiceSpeakFlags.SVSFlagsAsync表示异步发音。
screen.width-600)this.style.width=screen.width-600;" border=0> 但是,这个方法本身并不知道你给的字符串是什么语言,所以需要我们它这个字符串用什么语言读出。SpVoiceClass 类的Voice 属性就是用来设置语种的,我们可以通过SpVoiceClass 的GetVoices方法得到所有的语种列表,然后在根据参数选择相应的语种,比如设置语种为汉语如下所示:
private void SetChinaVoice()
{ voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ; } |
0表示是汉用,1234都表示英语,就是口音不同。
这样,我们就设置了语种,如果结合发音方法,我们就可以设计出一个只发汉语语音的方法
private void SpeakChina(string strSpeak)
{ SetChinaVoice() ; Speak(strSpeak) ; } |
只发英语语音的方法也是类似的,上面程序里有。
对于一段中英文混合的语言,我们让程序读出混合语音的方法就是:编程把这段语言的中英文分开,对于中文调用SpeakChina方法,英文调用SpeakEnglishi方法;至于怎样判断一个字符是英文还是中文,我采用的是判断asc码的方法,具体的类方法是通过AnalyseSpeak实现的。
这样,对于一段中英文混合文字,我们只需把它作为参数传递给AnalyseSpeak就可以了,他能够完成中英文的混合发音。
当然,对于发音的暂定、继续、停止等操作,上面也给出了简单的方法调用,很容易明白。
下面简单介绍一下中文语音识别的方法:
先把该语音识别的类源代码贴在下面,然后再做说明:
public class SpRecognition
{ private static SpRecognition _Instance = null ; private SpeechLib.ISpeechRecoGrammar isrg ; private SpeechLib.SpSharedRecoContextClass ssrContex =null; private System.Windows.Forms.Control cDisplay ; private SpRecognition() { ssrContex = new SpSharedRecoContextClass() ; isrg = ssrContex.CreateGrammar(1) ; SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle = new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition) ; ssrContex.Recognition += recHandle ; } public void BeginRec(Control tbResult) { isrg.DictationSetState(SpeechRuleState.SGDSActive) ; cDisplay = tbResult ; } public static SpRecognition instance() { if (_Instance == null) _Instance = new SpRecognition() ; return _Instance ; } public void CloseRec() { isrg.DictationSetState(SpeechRuleState.SGDSInactive) ; } private void ContexRecognition(int iIndex,object obj,SpeechLib.SpeechRecognitionType type,SpeechLib.ISpeechRecoResult result) { cDisplay.Text += result.PhraseInfo.GetText(0,-1,true) ; } }
|
我们定义了ssrContex 和isrg为语音识别的上下文和语法,通过设置isrg的DictationSetState方法,我们可以开始或结束识别,在上面的程序中是BeginRec和CloseRec方法。cDisplay 是我们用来输出识别结果的地方,为了能够在大部分控件上都可以显示结果,我用了一个Control 类来定义它。当然,每次语音识别后都会触发ISpeechRecoContextEvents_RecognitionEventHandler 事件,我们定义了一个这样的方法ContexRecognition来响应事件,并且在这个方法里输出识别结果
TTS 语音报价
步骤如下:
1、在网上下载speechsdk51、speechsdk51LangPack这两个文件并安装,这是微软提供的语音开发包。
2、在VS里新建一个项目,引用语音库,如图:

3、在窗体的按钮里这样写:
Private Sub Button
1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim v As New SpeechLib.SpVoice
v.Speak(ChineseNum(CDec(TextBox1.Text)), SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync)
MsgBox(ChineseNum(CDec(TextBox1.Text)))
End Sub
其实说话的只有这一句代码:
v.Speak(ChineseNum(CDec(TextBox1.Text)), SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync)
4、运行如图:
这时一个动听的男音就会给您报出价格了,呵呵。
这位说什么?女音才能用“动听”?
。。。是的,但是想听女音吗?那就掏银子买开发包吧!
附:下面附上我用到的金额转达写函数
Public Function ChineseNum(ByVal Single_to As Decimal) As String
Dim Str_to As String
Dim PartStr() As String = {"", ""}
Dim i As Integer
Dim Full As Boolean = False
Dim Zero As Boolean = False
Dim Delimiters As Char = CChar(".")
Str_to = CStr(Single_to)
PartStr = Str_to.Split(Delimiters)
Str_to = ""
For i = 0 To PartStr(0).Length - 1
Select Case PartStr(0).Chars(i)
Case CChar("0")
If Zero = False Then
Zero = True
End If
Case CChar("1")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "壹")
Case CChar("2")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "贰")
Case CChar("3")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "叁")
Case CChar("4")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "肆")
Case CChar("5")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "伍")
Case CChar("6")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "陆")
Case CChar("7")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "柒")
Case CChar("8")
Str_to = Str_to.Insert(Str_to.Length, "捌")
Zero = False
Case CChar("9")
If Zero = True Then
Str_to = Str_to.Insert(Str_to.Length, "零")
Zero = False
End If
Str_to = Str_to.Insert(Str_to.Length, "玖")
End Select
Select Case -(i - PartStr(0).Length + 1)
Case 0
Str_to = Str_to.Insert(Str_to.Length, "元")
Case 1
If Zero = False Then
Str_to = Str_to.Insert(Str_to.Length, "拾")
End If
Case 2
If Zero = False Then
Str_to = Str_to.Insert(Str_to.Length, "佰")
End If
Case 3
If Zero = False Then
Str_to = Str_to.Insert(Str_to.Length, "仟")
End If
Case 4
Str_to = Str_to.Insert(Str_to.Length, "万")
Zero = False
Case 5
If Zero = False Then
Str_to = Str_to.Insert(Str_to.Length, "拾")
End If
Case 6
If Zero = False Then
Str_to = Str_to.Insert(Str_to.Length, "佰")
End If
Case 7
If Zero = False Then
Str_to = Str_to.Insert(Str_to.Length, "仟")
End If
Case 8
Str_to = Str_to.Insert(Str_to.Length, "亿")
Zero = False
Case 9
Str_to = Str_to.Insert(Str_to.Length, "拾")
End Select
Next
If PartStr.GetUpperBound(0) <> 0 Then
For i = 0 To PartStr(1).Length - 1
Select Case PartStr(1).Chars(i)
Case CChar("0")
Str_to = Str_to.Insert(Str_to.Length, "零")
Case CChar("1")
Str_to = Str_to.Insert(Str_to.Length, "壹")
Case CChar("2")
Str_to = Str_to.Insert(Str_to.Length, "贰")
Case CChar("3")
Str_to = Str_to.Insert(Str_to.Length, "叁")
Case CChar("4")
Str_to = Str_to.Insert(Str_to.Length, "肆")
Case CChar("5")
Str_to = Str_to.Insert(Str_to.Length, "伍")
Case CChar("6")
Str_to = Str_to.Insert(Str_to.Length, "陆")
Case CChar("7")
Str_to = Str_to.Insert(Str_to.Length, "柒")
Case CChar("8")
Str_to = Str_to.Insert(Str_to.Length, "捌")
Case CChar("9")
Str_to = Str_to.Insert(Str_to.Length, "玖")
End Select
If i = 0 Then
Str_to = Str_to.Insert(Str_to.Length, "角")
Else
Str_to = Str_to.Insert(Str_to.Length, "分")
Full = True
End If
Next
End If
If Not Full Then
Str_to = Str_to.Insert(Str_to.Length, "整")
End If
Return Str_to
End Function
Microsoft SQL Server 2005 Express 远程访问设置详述
概述
Microsoft SQL Server 2005 Express Edition是Microsoft数据库的低端解决方案,是免费的,并且可以随软件免费发布,而就其数据库功能对于一般的企业级应用已足够了。但 默认安装时只允许本地访问,而不能远程访问。为了解决这种问题,网上有不少文章进行了介绍,但是都不全,如果你照着做,大都只完成了部份功能,而不能完全成功。本人查了大量资料,几次都想放弃,最终还是成功配置了,现写本文详细阐述 SQL Server 2005 Express Edition 启用网络访问,供大家参考。
准备工作:1、安装 SQL Server 2005 Express Edition 并启用数据库服务;2、安装SQL Server 2005 Management Studio Express。
操作步骤
一、配置远程访问的协议(TCP)并启用端口:
详细操作步骤请参照熊义龙的《SQL Server 2005 Express远程访问设置方法》写得非常详细且配有图文。感谢熊义龙朋友。
在此补充:1、在上文中“第3步”启用TCP协议时,除了启用服务器的外网IP外,还要启用127.0.0.1以及IPAll的端口都设置好。2、也是上文“第3步”启用TCP协议时,建议设置一个静态的端口,三个要设置的端口都要统一。
二、启用“SQL Server 和 Windows 身份验证模式”:
1、打开 SQL Server 2005 Management Studio Express(说明,写本文时,我用的SQL Server 企业版的管理器,操作是一样的)。服务器名(图中为NBFUQIN)右键,选择“属性”(如下图),即打开“服务器属性”对话框。
2、在“服务器属性”对话框中,选择“选择页/安全性”,将“服务器身份验证”选为“SQL Server 和 Windows身份验证模式”,点击“确定”并重启数据库服务。如下图所示。
三、增加SQL Server登录名或者开启SA远程访问:
由于SQL Server 2005 Express Edition默认sa为禁止登录。所以要么开启sa登录,或者新建一个登录名即可。
1、新建登录名:打开 SQL Server 2005 Management Studio Express,并选中“服务器名/安全性/登录名”,右击选择“新建登录名”,如下图所示。打开“登录名 - 新建”对话框。
在“登录名—新建”对话框中,选择“常规”选项卡,然后新建一个登录名,要注意选中“SQL Server 身份验证”。如下图所示。
选中“状态”选项卡,确保“登录”在“启用”状态、“是否允许连接到数据库引擎”处于“授予”状态。
2、启用sa:在“登录”中,选中sa,右击“属性”,在“常规”中设置好密码,在“状态”中启用“登录”和授予“是否允许连接到数据库引擎”。
补充
1、SQL Server 2005 Express Edition的登录的服务器与SQL Server 2005的其他版本(服务器即为“服务器IP或名称”)不同,它的服务器为“服务器IP\SQLexpress,1433”(也可用服务器名代替IP),后面的1433为第一步所启用的端口。 2、如果是XP做为服务器,还要在防火墙中开放对应的端口(当然你也可以关闭防火墙),如1433端口。
结束语
以上就是整个配置过程的详细操作步骤,欢迎大家一起交流。在此还要感谢我的赵师弟。