[?]|登录|免费注册®
高级...

浏览选项

  • 手机网站排名
  • 手机网用户圈
  • UA设置:(默认)
  • 设置浏览参数
  • 重新下载页面

[+](无标题)

?博客园_首页代码改变世界uuid:b958b8db-33a0-4c9d-9638-2ef5084288bf;id=43702014-07-30T17:07:56Zfeed.cnblogs.comhttp://www.cnblogs.com/polossk/p/3879676.html【综合】天梯算法 - Polo Shen目标今天听到有人说某学校的ACM队搞了一个Rating System,就是类似TopCoder和Codeforces的Rating一样,直观的积分更容易体现比赛的结果,以及大家的最近表现。而且据说他们把这个作为一个考核标准之一。听上去好赞的样子!可是我们学校木有啊!!!紧接着我就想到了当时看《社交网...2014-07-30T16:44:00Z2014-07-30T16:44:00ZPolo Shenhttp://www.cnblogs.com/polossk/<p><span style="font-family: 'Courier New';">&lt;!--版权声明!转载请说明出处谢谢:)--&gt;</span></p><p><span style="font-family: 'Courier New';">&lt;!--by Polo Shen: http://www.cnblogs.com/polossk/ &ndash;&gt;</span></p><p>&nbsp;</p><p><strong>目标</strong></p><p>今天听到有人说某学校的ACM队搞了一个Rating System,就是类似TopCoder和Codeforces的Rating一样,直观的积分更容易体现比赛的结果,以及大家的最近表现。而且据说他们把这个作为一个考核标准之一。</p><p>听上去好赞的样子!</p><p>可是我们学校木有啊!!!</p><p>紧接着我就想到了当时看《社交网络》时,扎克伯格搞得那个&ldquo;点妹子&rdquo;活动。</p><p>程序员木有点新创意简直是在玷污程序员的工作啊!</p><p>搞起一个自己的Rating System。</p><p><strong>天梯算法简介</strong></p><p>还是先恶补知识好了。所谓的天梯算法,其实很简单的,虽然数学证明很复杂。。。天梯算法的基础是建立在人群的水平基本满足正态分布上的。简单地说,就是假设大部分人旗鼓相当,同时存在少部分精英和部分水平较低的人。同时核心的操作也不言而喻,简单地说,同一水平的人比赛,胜者加分,败者减分。差距比较大的两个人比赛,爆冷之后加分减分会高出同水平的但是也不会暴涨。</p><p>这里我用的是Elo Rating System.</p><p>大致的算法如下:</p><p>1、假设这两个人的当前的分数已经给出:</p><p><a href="http://images.cnitblog.com/blog/627360/201407/310043291659554.gif"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="Rating_1" src="http://images.cnitblog.com/blog/627360/201407/310043298524412.gif" alt="Rating_1" width="246" height="60" border="0" /></a></p><p>2、计算其获胜的期望:</p><p><a href="http://images.cnitblog.com/blog/627360/201407/310043301495855.gif"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="Rating_2" src="http://images.cnitblog.com/blog/627360/201407/310043303373097.gif" alt="Rating_2" width="105" height="63" border="0" /></a></p><p><a href="http://images.cnitblog.com/blog/627360/201407/310043305245041.gif"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="Rating_3" src="http://images.cnitblog.com/blog/627360/201407/310043307122283.gif" alt="Rating_3" width="116" height="116" border="0" /></a></p><p>3、根据实际的结果来进行对分数的修正:</p><p><a href="http://images.cnitblog.com/blog/627360/201407/310043309305983.gif"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="Rating_4" src="http://images.cnitblog.com/blog/627360/201407/310043311183225.gif" alt="Rating_4" width="86" height="90" border="0" /></a></p><p><a href="http://images.cnitblog.com/blog/627360/201407/310043313218397.gif"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="Rating_5" src="http://images.cnitblog.com/blog/627360/201407/310043315407396.gif" alt="Rating_5" width="188" height="76" border="0" /></a></p><p>在这里,S表示的是结果,也就是胜平负代表的1, 0.5, 0;K表示的是比赛系数。通常一次比赛分值变动量不会太大。</p><p><strong>实际实现</strong></p><p>但是,这里和刚才的那个模型不一样。刚才的那个模型是为国际象棋棋手准备的,也就是两个人随意的交手,然后在涨分。换句话说,也许你的比赛场数越多,你的分数也就会更好看。</p><p>没听懂?Dota,LOL玩过吧,比赛永远是两个队在互相掐架。</p><p>但是Codeforces不一样,可能是上百人上千人同时在玩这个游戏,那么应该怎么搞?</p><p>我给的想法是最简单的想法:均摊比赛系数,每个人都参与比较,最后再和自己比较。</p><p>什么意思呢?我把一次比赛,假设有n个选手同时参赛,对于其中的选手i,相当于同时和n-1个选手进行了一场同样题目的比赛。所以我将比赛的系数均摊给参赛选手(当然有更好的分配方法,就是根据两者的赛前差距进行分配,这样更为接近现实),分配好之后,只需要让他们两两之间互相&ldquo;比试&rdquo;一番,就好了。</p><p>但是这样的话,就会造成有的人不停涨分,而且涨分幅度一次比一次大。所以必须引入自我比较法。简单地说,假设一个1700分的人参加一个800人参加的,所有参赛选手的最高分即为1700分的比赛,如果这个人,前一场是全场第2,这场全场第12。虽然他的排名很高,但是他输给了自己的上一场,还是会&ldquo;下降&rdquo;一些分数。但是这里的下降并不是绝对意义的下降,是指&ldquo;涨幅&rdquo;下降。也就是说,他的分数会基本保持在这个水平,但是不会突然跌下去或者突然涨分。</p><p>这就是我的大致思路了。毕竟不是大的项目,简单的做下就可以了。</p><p><strong>代码</strong></p><p>代码我使用的是Ruby,通过传入一个文本文档,上面记录参赛同学的:</p><p>&lt;Rank&gt;&lt;Name&gt;&lt;Rating&gt;&lt;SolvedProblem&gt;</p><p>四个数据来进行计算。(其实那个出题数是没有必要的。。。)</p><p>最后输出一个文本文档,上面记录同学的</p><p>&lt;Rank&gt;&lt;Name&gt;&lt;NewRating&gt;</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" onclick="cnblogs_code_show('211c43c8-9005-4e0a-90df-b26b69c33417')"><div id="cnblogs_code_open_211c43c8-9005-4e0a-90df-b26b69c33417" class="cnblogs_code_hide"><span style="color: #008000;">#</span><span style="color: #008000;"> encoding = UTF-8</span><br/><br/><span style="color: #0000ff;">class</span><span style="color: #000000;"> Student<br/> <br/> attr_accessor :name<br/> attr_accessor :rating<br/> attr_accessor :rank<br/> attr_accessor :_solve<br/> attr_accessor :_drating<br/> <br/> </span><span style="color: #0000ff;">def</span> initialize(name = <span style="color: #800000;">"</span><span style="color: #800000;">Unknown</span><span style="color: #800000;">"</span>, rak = 100, rtg = 1500, solve =<span style="color: #000000;"> 0)<br/> @rank </span>=<span style="color: #000000;"> rak.to_i<br/> @name </span>=<span style="color: #000000;"> name<br/> @rating </span>=<span style="color: #000000;"> rtg.to_i<br/> @_drating </span>=<span style="color: #000000;"> 0<br/> @_solve </span>=<span style="color: #000000;"> solve.to_i<br/> end<br/> <br/> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> initialize(str2)<br/> str </span>=<span style="color: #000000;"> str2.to_s<br/> data </span>= str.split(<span style="color: #800000;">'</span> <span style="color: #800000;">'</span><span style="color: #000000;">)<br/> @rank </span>=<span style="color: #000000;"> data[0].to_i<br/> @name </span>= data[1<span style="color: #000000;">]<br/> @rating </span>= data[2<span style="color: #000000;">].to_i<br/> @_solve </span>= data[3<span style="color: #000000;">].to_i<br/> @_drating </span>=<span style="color: #000000;"> 0<br/> end<br/> <br/> </span><span style="color: #0000ff;">def</span> update(d) <span style="color: #008000;">#</span><span style="color: #008000;"> 累加 rating 改变量</span><br/> @_drating +=<span style="color: #000000;"> d<br/> end<br/> <br/> </span><span style="color: #0000ff;">def</span> modify <span style="color: #008000;">#</span><span style="color: #008000;"> 更新rating</span><br/> @rating +=<span style="color: #000000;"> @_drating<br/> @_drating </span>=<span style="color: #000000;"> 0<br/> @rating </span>=<span style="color: #000000;"> @rating.to_i<br/> end<br/> <br/> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> toStringLine<br/> </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">#{@rank}\t#{@name}\t#{@rating}\n</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/> end<br/> <br/> </span><span style="color: #0000ff;">def</span> <span style="color: #0000ff;">print</span><br/> <span style="color: #008000;">#</span><span style="color: #008000;">puts "#{@rank}\t#{@name}\t#{@rating}"</span><br/> puts <span style="color: #800000;">"</span><span style="color: #800000;">#{@rating}</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/> end<br/> <br/>end<br/><br/>$Kcef </span>= 60 <span style="color: #008000;">#</span><span style="color: #008000;"> 比赛的系数</span><br/><br/>=<span style="color: #000000;">begin<br/>Rating算法:<br/>针对两个参加比赛的人,互相比较,每次比较都更新一次_drating。<br/><br/>比较法则:<br/>先比较题数:多者胜,少者负;<br/>若平手,比较排名;<br/>根据比较结果选取系数进行计算。<br/><br/>更新系数法则:<br/>将本场的比赛系数按参赛人数分配给各个学生<br/>计算方法如下:<br/> cef </span>= $Kcef /<span style="color: #000000;"> sz<br/><br/>更新法则:<br/>更新时,将两两同学互相比较,然后每次计算出的增量累加到_drating上即可<br/>增量计算方法如下:<br/> A: dA </span>= cef * (sA -<span style="color: #000000;"> eA)<br/> B: dB </span>= cef * (sB -<span style="color: #000000;"> eB)<br/>最后直接累加即可。<br/></span>=<span style="color: #000000;">end<br/><br/></span><span style="color: #0000ff;">def</span><span style="color: #000000;"> calc_eA_eB(rA, rB)<br/> da </span>= (rB - rA) / 400.0<span style="color: #000000;"><br/> db </span>= -<span style="color: #000000;">da<br/> ta </span>= 10.0**da + 1.0<span style="color: #000000;"><br/> tb </span>= 10.0**db + 1.0<span style="color: #000000;"><br/> ea </span>= 1.0 /<span style="color: #000000;"> ta<br/> eb </span>= 1.0 /<span style="color: #000000;"> tb<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> [ea, eb]<br/>end<br/><br/></span><span style="color: #0000ff;">def</span><span style="color: #000000;"> calc_sA_sB(a, b)<br/> solveA </span>=<span style="color: #000000;"> a._solve<br/> solveB </span>=<span style="color: #000000;"> b._solve<br/> pA </span>=<span style="color: #000000;"> a.rank<br/> pB </span>=<span style="color: #000000;"> b.rank<br/> </span><span style="color: #0000ff;">if</span> solveA &lt;<span style="color: #000000;"> solveB<br/> </span><span style="color: #0000ff;">return</span> [0.0, 1.0<span style="color: #000000;">]<br/> elsif solveA </span>&gt;<span style="color: #000000;"> solveB<br/> </span><span style="color: #0000ff;">return</span> [1.0, 0.0<span style="color: #000000;">]<br/> elsif pA </span>&lt;<span style="color: #000000;"> pB<br/> </span><span style="color: #0000ff;">return</span> [1.0, 0.0<span style="color: #000000;">]<br/> </span><span style="color: #0000ff;">else</span><br/> <span style="color: #0000ff;">return</span> [0.0, 1.0<span style="color: #000000;">]<br/> end<br/>end<br/><br/></span><span style="color: #0000ff;">def</span><span style="color: #000000;"> calc_cef(ra, rb, sz)<br/> </span><span style="color: #0000ff;">return</span> $Kcef /<span style="color: #000000;"> sz<br/>end<br/><br/></span><span style="color: #0000ff;">def</span> competition(a, b, sz) <span style="color: #008000;">#</span><span style="color: #008000;"> 修改两人的增量</span><br/> eA, eB =<span style="color: #000000;"> calc_eA_eB(a.rating.to_f, b.rating.to_f)<br/> sA, sB </span>=<span style="color: #000000;"> calc_sA_sB(a, b)<br/> cef </span>=<span style="color: #000000;"> calc_cef(a.rank.to_f, b.rank.to_f, sz.to_f)<br/> dA </span>= (sA - eA) *<span style="color: #000000;"> cef<br/> dB </span>= (sB - eB) *<span style="color: #000000;"> cef<br/> a.update(dA)<br/> b.update(dB)<br/>end<br/><br/></span><span style="color: #0000ff;">def</span><span style="color: #000000;"> read( path )<br/> items </span>=<span style="color: #000000;"> Array.new<br/> File.open(path, :encoding </span>=&gt; <span style="color: #800000;">'</span><span style="color: #800000;">utf-8</span><span style="color: #800000;">'</span>).each_line do |line|<span style="color: #000000;"><br/> item </span>=<span style="color: #000000;"> Student.new(line)<br/> items </span>&lt;&lt;<span style="color: #000000;"> item<br/> end<br/> sz </span>=<span style="color: #000000;"> items.size<br/> </span><span style="color: #008000;">#</span><span style="color: #008000;"> 逐一比较</span><br/> <span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span><span style="color: #000000;"> 0...sz<br/> </span><span style="color: #0000ff;">for</span> j <span style="color: #0000ff;">in</span><span style="color: #000000;"> 0...sz<br/> </span><span style="color: #0000ff;">if</span> i ==<span style="color: #000000;"> j then<br/> next<br/> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> <br/> competition(items[i], items[j], sz.to_i);<br/> end <br/> end<br/> end<br/> </span><span style="color: #008000;">#</span><span style="color: #008000;"> 自身比较</span><br/> items.each do |data|<span style="color: #000000;"><br/> temp </span>=<span style="color: #000000;"> data<br/> temp.modify </span><span style="color: #008000;">#</span><span style="color: #008000;"> 更新这场的rating</span><br/><span style="color: #000000;"> competition(temp, data, sz)<br/> temp.modify </span><span style="color: #008000;">#</span><span style="color: #008000;"> 自我比较后的rating</span><br/> data.modify <span style="color: #008000;">#</span><span style="color: #008000;"> 对 rating 取平均值</span><br/> data.rating = ((temp.rating.to_i + data.rating.to_i ) / 2<span style="color: #000000;">).to_i<br/> end<br/> <br/> file </span>= File.new(<span style="color: #800000;">"</span><span style="color: #800000;">result.txt</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">w+</span><span style="color: #800000;">"</span><span style="color: #000000;">)<br/> items.each do </span>|data|<span style="color: #000000;"><br/> data.modify<br/> file.</span><span style="color: #0000ff;">print</span><span style="color: #000000;"> data.toStringLine<br/> end<br/>end<br/>read(</span><span style="color: #800000;">"</span><span style="color: #800000;">contest1.txt</span><span style="color: #800000;">"</span>)</div></div><p>&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3879676" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/polossk/p/3879676.html" target="_blank">【综合】天梯算法</a>,转载请注明。</p>http://www.cnblogs.com/lijingyang/p/3879609.html.Net MVC 入门 - 烈阳耀京大家好,我叫李京阳,,很高兴认识大家,之所以我想开一个自己的博客,就是来把自己所了解的知识点通过自己的话写一下,今天我开始说一下我对MVC的认识和入门基础,明天补上Razor语法。Asp.net MVC框架1.区别 Asp.NetMVC框架 和 MVC设计模式。1.1.Asp.Net Mvc框架.....2014-07-30T16:21:00Z2014-07-30T16:21:00Z烈阳耀京http://www.cnblogs.com/lijingyang/该文只有注册用户登录后才能阅读。<a href='http://www.cnblogs.com/lijingyang/p/3879609.html' target='_blank'>阅读全文</a>。http://www.cnblogs.com/JohnTsai/p/3875561.html在Android中,px,dp,dip,sp的不同之处 - JohnTsai最近在学习Android开发,一直没有弄清楚px,dp,dip,sp的区别。今天正好有时间,就花时间研究了一下。 众所周知,Android厂商非常多,各种尺寸的Android手机、平板层出不穷。导致了Android生态环境的碎片化现象越来越严重。Google公司为了解决分辨率过多的问题,在And.....2014-07-30T16:04:00Z2014-07-30T16:04:00ZJohnTsaihttp://www.cnblogs.com/JohnTsai/<p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;</strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp; &nbsp;&nbsp; 最近在学习Android开发,一直没有弄清楚px,dp,dip,sp的区别。今天正好有时间,就花时间研究了一下。</strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;&nbsp;&nbsp; 众所周知,Android厂商非常多,各种尺寸的Android手机、平板层出不穷。导致了Android生态环境的碎片化现象越来越严重。Google公司为了解决分辨率过多的问题,在Android的开发文档中定义了px,dp,dip,sp,方便开发者适配不同分辨率的Android设备。</strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>1.基础知识:</strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;&nbsp;&nbsp; mm:</strong>millimeters 毫米,长度单位<strong>。</strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;&nbsp;&nbsp; in:</strong>inches<strong> &nbsp;</strong>英寸,长度单位,屏幕物理尺寸大小的基本单位。<strong><br /></strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;&nbsp;&nbsp; pt:</strong>points<strong>&nbsp;</strong> 一个标准单位,大小为一英寸的屏幕大小的1/72。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>dpi</strong>: dots per inch 即每英寸的像素点。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp;&nbsp; 举个例子:<br /></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 16px;" lang="EN-US">假设<span lang="EN-US">QVGA(320*240)分辨率的屏幕物理尺寸是(<span lang="EN-US">2英寸<span lang="EN-US">*1.5英寸),那么这部手机的<span lang="EN-US">dpi=160。</span></span></span></span></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp; &nbsp;<span style="font-family: Microsoft YaHei; font-size: 16px;"><strong><span style="font-size: 16px; font-family: Microsoft YaHei;"><span lang="EN-US">Screen size</span></span></strong><span style="font-size: 16px; font-family: Microsoft YaHei;"><span lang="EN-US">(屏幕尺寸)<span style="font-family: Microsoft YaHei; font-size: 16px;">指的是手机实际的物理尺寸。</span></span></span></span></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><span style="font-family: Microsoft YaHei; font-size: 16px;"><span style="font-size: 16px; font-family: Microsoft YaHei;"><span lang="EN-US"><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp;&nbsp; 如iPhone4S是3.5英寸,这里指的是手机屏幕对角线的长度是3.5英寸。</span></span></span></span></span></p><p>&nbsp;</p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><img style="width: 400px; height: 302px; top: 136.5px; left: 274px;" title="点击查看源网页" src="http://news.ccidnet.com/upload_images/2013/01/22/2586263844.jpg" alt="" /></span></p><p>&nbsp;</p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp; &nbsp;<strong>Resolution</strong>(分辨率)指手机屏幕横、竖方向像素个数。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp; <span style="font-family: Microsoft YaHei; font-size: 16px;">常见的手机分辨率:QVGA(240&times;320像素),<span class="headline-content">HVGA(480&times;320像素),WVGA(800&times;480像素),720P(1280x720像素),1080P(<span style="font-family: Microsoft YaHei; font-size: 16px;">1920x1080像素),2K(2560x1440像素)</span></span></span></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;&nbsp;<br /></strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>2.基本定义:</strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;&nbsp;&nbsp; px </strong>(Pixel的缩写,即像素) 是指一个像素。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp; &nbsp; sp</strong>&nbsp; (scale-independent pixels)是指比例独立像素。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp; &nbsp; <strong>dip</strong> (device independent pixels)是指设备独立像素。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp;<strong>&nbsp; dp</strong>:(Density-independent pixel)在Android文档中(http://developer.android.com/guide/practices/screens_support.html#terms),它的计算方法是:px = dp * (dpi / 160)。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp;&nbsp; 看一下dp的单位是什么:像素&nbsp;=&nbsp;dp的单位&nbsp;*&nbsp;像素/英寸&nbsp; 。可以看出dp的单位是长度单位英寸。1dp的长度&nbsp;=&nbsp;1&nbsp;*&nbsp;dpi&nbsp;/&nbsp;160&nbsp;*&nbsp;1&nbsp;/&nbsp;dpi&nbsp;英寸&nbsp;=&nbsp;1&nbsp;/&nbsp;160&nbsp;英寸 。</span></p><p>&nbsp;&nbsp;<span style="font-family: Microsoft YaHei; font-size: 16px;"> dp的意义是:你可以根据看到的实际大小,在应用中设置对应的dp值,而使得不同的设备上看起来一样大。</span></p><p><strong><span style="font-family: Microsoft YaHei; font-size: 16px;">3.不同之处</span></strong></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp; px(像素)表示屏幕的<strong>真实像素</strong>,比如现在流行的1080P手机和2K手机,1080P手机就是1920x1080像素,也就是说这个手机的屏幕横向有1080个像素,纵向有1920个像素。这个值是<strong>绝对的</strong>,是多少就是多少,不会改变。</span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp; sp(比例独立像素),也就是说是与密度和字体缩放度无关的像素,主要用于字体的大小。<span style="font-family: Microsoft YaHei; font-size: 16px;"><strong><br /></strong></span></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;">&nbsp;&nbsp; dip(设备独立像素)这个是逻辑像素单位,<span style="font-family: Microsoft YaHei; font-size: 16px;">其实就是dp</span>。<br /></span></p><p>&nbsp;</p><p>&nbsp;&nbsp; &nbsp;<span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>欢迎讨论交流,我的邮箱是JohnTsai.Work@gmail.com。</strong></span></p><p><span style="font-family: Microsoft YaHei; font-size: 16px;"><span style="font-family: Microsoft YaHei; font-size: 16px;"><strong>&nbsp;&nbsp;&nbsp; 欢迎转载,转载请注明网址,谢谢!</strong></span></span></p><p>&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3875561" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/JohnTsai/p/3875561.html" target="_blank">在Android中,px,dp,dip,sp的不同之处</a>,转载请注明。</p>http://www.cnblogs.com/dataadapter/p/3879504.html一个ORM的实现(附源代码) - Mike.Jiang1 前言经过一段时间的编写,终于有出来一个稳定的版本,期间考虑了多种解决方案也偷偷学了下园子里面大神们的作品。已经有很多的ORM框架,为什么要自己实现一个?我的原因是在遇到特殊需求时,可以在ORM中加入特定的代码。如 :根据数据库的字段长度和可空性做基本的数据验证,在ORM中解决数据修改时的同步问....2014-07-30T15:31:00Z2014-07-30T15:31:00ZMike.Jianghttp://www.cnblogs.com/dataadapter/<p><strong>1  前言</strong></p><p>经过一段时间的编写,终于有出来一个稳定的版本,期间考虑了多种解决方案也偷偷学了下园子里面大神们的作品。</p><p>已经有很多的ORM框架,为什么要自己实现一个?我的原因是在遇到特殊需求时,可以在ORM中加入特定的代码。如 :根据数据库的字段长度和可空性做基本的数据验证,在ORM中解决数据修改时的同步问题&hellip;</p><p>本文主要关注的是如何实现ORM方面,其它的大家可以参考以下两篇文章:</p><p><a id="homepage1_HomePageDays_DaysList_ctl01_DayList_TitleUrl_0" class="postTitle2" href="http://www.cnblogs.com/dataadapter/p/3844394.html">用T4 Template生成代码</a>:参考此文,可以知道本ORM是如何根据数据库,生成实体层代码。有了这个基础,就可以看懂本文本中所有的T4模板。</p><p><a id="homepage1_HomePageDays_DaysList_ctl00_DayList_TitleUrl_0" class="postTitle2" href="http://www.cnblogs.com/dataadapter/p/3866668.html">DBHelper (支持事务与数据库变更)</a>:参考此文,可以知道本ORM是如何访问数据库,如何支持事务,分页和多种数据库。</p><p><strong>2  实现方法</strong></p><p><img src="http://images.cnitblog.com/i/323742/201407/302312541188325.png" alt="" /></p><p>本文主要关注的蓝色ORM部分。</p><p>Model:定义了实体的基类。每一张表都会生成对应的实体并继承此类;</p><p>ModelMapping:定义了一张表的结构信息,其中包含了表名、主键、字段类型等。每一张表都会生成一个;</p><p>ModelMappingReflector: 在程序第一次运行时,反射所有ModelMapping的子类,并将其加入一个字典集合中;</p><p>DataAccess&lt;T&gt;: 此类的作用时,根据实体和实体的结构信息(ModelMapping),自动生成SQL来访问数据库(Add,Update,Delete);或将数据库中值加到实体上(GetModel,GetList)。此类帮我们做CRUD的工作,我们不再需要写每张表基本的增删改查的工作了,但仍需要写复杂业务的查询。每一张表都会生成一个空的dao类并继承此类。</p><p>此ORM的核心类就是DataAccess&lt;T&gt;, 下面我们就拿Add作为示例:</p><p>Add 操作代码</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> Add(T model)<br/>{<br/> </span><span style="color: #0000ff;">if</span> (model == <span style="color: #0000ff;">null</span><span style="color: #000000;">)<br/> {<br/> </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> ArgumentNullException();<br/> }<br/><br/> StringBuilder cmdText </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuilder();<br/> cmdText.Append(</span><span style="color: #800000;">"</span><span style="color: #800000;">INSERT INTO </span><span style="color: #800000;">"</span>).Append(modelMapping.TableName).Append(<span style="color: #800000;">"</span><span style="color: #800000;"> (</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> cmdText.Append(</span><span style="color: #0000ff;">string</span>.Join(<span style="color: #800000;">"</span><span style="color: #800000;">,</span><span style="color: #800000;">"</span><span style="color: #000000;">, model.ColumnValues.Keys.ToArray()));<br/> cmdText.Append(</span><span style="color: #800000;">"</span><span style="color: #800000;">) VALUES (@</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> cmdText.Append(</span><span style="color: #0000ff;">string</span>.Join(<span style="color: #800000;">"</span><span style="color: #800000;">, @</span><span style="color: #800000;">"</span><span style="color: #000000;">, model.ColumnValues.Keys.ToArray()));<br/> cmdText.Append(</span><span style="color: #800000;">"</span><span style="color: #800000;">)</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> List</span>&lt;DbParameter&gt; dbParms = <span style="color: #0000ff;">new</span> List&lt;DbParameter&gt;<span style="color: #000000;">();<br/> </span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> pair <span style="color: #0000ff;">in</span><span style="color: #000000;"> model.ColumnValues) <br/> {<br/> dbParms.Add(DBHelper.CreateInDbParameter(</span><span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">@{0}</span><span style="color: #800000;">"</span><span style="color: #000000;">,pair.Key), modelMapping.ColumnDict[pair.Key].DbDataBype, pair.Value));<br/> }<br/><br/> </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">.ExecuteNonQuery(CommandType.Text, cmdText.ToString(), dbParms.ToArray());<br/>}</span></div><p>&nbsp;</p><p>当我们写这样的代码时User model=new User(); model.UserName时,实际上会向Model中ColumnValues集合中添加数据。而要生成一个INSERT SQL (INSERT INTO tablename (column1,colum2) VALUES (@column1,@column2)和添加插入参数,我们需要表名,列名,列值和列的类型。在列名和列值就存在于Model中的ColumnValues集合,而列的类型和表名,我们可以在ModelMapping中获取。</p><p><strong>3   示例</strong></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" onclick="cnblogs_code_show('37ed352a-ed69-4f80-894f-e0c3ec1a8e9e')"><div id="cnblogs_code_open_37ed352a-ed69-4f80-894f-e0c3ec1a8e9e" class="cnblogs_code_hide"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)<br/>{<br/> TestORM();<br/> TestPaging();<br/><br/> Console.ReadLine();<br/>}<br/><br/></span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> TestORM()<br/>{<br/> UserDao dao </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> UserDao();<br/> </span><span style="color: #0000ff;">try</span><span style="color: #000000;"><br/> {<br/> dao.BeginTransaction();<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> add model</span><br/> User mUser1 = <span style="color: #0000ff;">new</span><span style="color: #000000;"> User();<br/> mUser1.ID </span>= <span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser1.UserName </span>= <span style="color: #800000;">"</span><span style="color: #800000;">Mike1</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser1.UserPwd </span>= <span style="color: #800000;">"</span><span style="color: #800000;">1234</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser1.CreateBy </span>= <span style="color: #800000;">"</span><span style="color: #800000;">System</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser1.CreateDate </span>=<span style="color: #000000;"> DateTime.Now;<br/> dao.Add(mUser1);<br/><br/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Create Model Successfully.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">string</span> cmdText = <span style="color: #800000;">"</span><span style="color: #800000;">SELECT * FROM TUser WHERE ID='1' ORDER BY ID;</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> List</span>&lt;User&gt; list0 =<span style="color: #000000;"> dao.GetListBySQL(cmdText);<br/> </span><span style="color: #0000ff;">foreach</span> (User model <span style="color: #0000ff;">in</span><span style="color: #000000;"> list0)<br/> {<br/> Console.WriteLine(model.UserName);<br/> }<br/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Get List Successfully.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">嵌套事务</span><br/> UserDao dao2 = <span style="color: #0000ff;">new</span><span style="color: #000000;"> UserDao();<br/> </span><span style="color: #0000ff;">try</span><span style="color: #000000;"><br/> {<br/> dao2.BeginTransaction();<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">update model</span><br/> User mUser2 = <span style="color: #0000ff;">new</span><span style="color: #000000;"> User();<br/> mUser2.ID </span>= <span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser2.UserName </span>= <span style="color: #800000;">"</span><span style="color: #800000;">Mike.Jiang</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser2.UserPwd </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;<br/> mUser2.CreateDate </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;<br/> dao.Update(mUser2);<br/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Update Model Successfully.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> User mUser3 </span>= dao.GetModel(<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">if</span> (mUser3 != <span style="color: #0000ff;">null</span><span style="color: #000000;">)<br/> Console.WriteLine(mUser3.UserName);<br/><br/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Get Model Successfully.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> dao2.CommitTransaction();<br/> }<br/> </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"><br/> {<br/> dao2.RollbackTransaction();<br/> }<br/><br/> dao.Delete(</span><span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Delete Model Successfully.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> dao.CommitTransaction();<br/> }<br/> </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex)<br/> {<br/> Console.WriteLine(ex.Message);<br/> dao.RollbackTransaction();<br/> }<br/>}<br/><br/></span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> TestPaging()<br/>{<br/> UserDao dao </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> UserDao();<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> add 100 user</span><br/> <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">1</span>; i &lt;= <span style="color: #800080;">100</span>; i++<span style="color: #000000;">)<br/> {<br/> User mUser1 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> User();<br/> mUser1.ID </span>=<span style="color: #000000;"> i.ToString();<br/> mUser1.UserName </span>= <span style="color: #800000;">"</span><span style="color: #800000;">Mike</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> i.ToString();<br/> mUser1.UserPwd </span>= <span style="color: #800000;">"</span><span style="color: #800000;">1234</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser1.CreateBy </span>= <span style="color: #800000;">"</span><span style="color: #800000;">System</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> mUser1.CreateDate </span>=<span style="color: #000000;"> DateTime.Now;<br/> dao.Add(mUser1);<br/> }<br/><br/> </span><span style="color: #0000ff;">string</span> cmdText = <span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">SELECT * FROM TUser</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> DataTable table </span>= DBHelper.ExecutePagingDataTable(CommandType.Text, cmdText, <span style="color: #800080;">1</span>, <span style="color: #800080;">10</span>, <span style="color: #800000;">"</span><span style="color: #800000;">ORDER BY ID</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">foreach</span> (DataRow row <span style="color: #0000ff;">in</span><span style="color: #000000;"> table.Rows)<br/> {<br/> Console.WriteLine(row[</span><span style="color: #800000;">"</span><span style="color: #800000;">ID</span><span style="color: #800000;">"</span><span style="color: #000000;">]);<br/> }<br/><br/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Get Datatabe paging successfuly.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> List</span>&lt;User&gt; list = dao.GetListPaging(cmdText, <span style="color: #800080;">2</span>, <span style="color: #800080;">10</span>, <span style="color: #800000;">"</span><span style="color: #800000;">ORDER BY ID</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">foreach</span> (User m <span style="color: #0000ff;">in</span><span style="color: #000000;"> list)<br/> {<br/> Console.WriteLine(m.ID);<br/> }<br/><br/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Get Model list paging successfully.</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/><br/> cmdText </span>= <span style="color: #800000;">"</span><span style="color: #800000;">DELETE FROM TUser</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> dao.ExecuteNonQuery(CommandType.Text, cmdText);<br/>}</span></div></div><p>&nbsp;</p><p>&nbsp;<img src="http://images.cnitblog.com/i/323742/201407/302301343371103.png" alt="" /></p><p><strong>4  总结</strong></p><p>这个版本的ORM示例,示例是没有认真去写了,因为不认为会有人用。但是这个ORM的代码是我认为比较简单的一个版本,只具有ORM的核心功能,几个类,只要具有c#基础的人都能看懂,有兴趣的可以看下。后续,自己会加上分页的Pager相关的内容、数据同步、数据验证的功能和和根据SQL自动生成实体对象的功能。</p><p>所有的源代码: <a id="" href="http://files.cnblogs.com/dataadapter/BaseProject.7z">BaseProject.7z</a></p><img src="http://counter.cnblogs.com/blog/rss/3879504" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/dataadapter/p/3879504.html" target="_blank">一个ORM的实现(附源代码)</a>,转载请注明。</p>http://www.cnblogs.com/huang0925/p/3879542.htmlAWS系列之三 使用EBS - 黄博文Amazon Elastic Block Store(EBS)可作为EC2实例的持久性数据块级存储。其具有高可用性和持久性的特点,可用性高达99.999%。给现有的EC2实例扩展新的存储块只需要几分钟的时间,省时省力。每个EBS块都被放置在一个特定的可用区内,并且会自动维护一个副本,随时保护数据安全...2014-07-30T15:21:00Z2014-07-30T15:21:00Z黄博文http://www.cnblogs.com/huang0925/<div id="main"><article class="hentry" role="article"><div class="entry-content"><p>Amazon Elastic Block Store(EBS)可作为EC2实例的持久性数据块级存储。其具有高可用性和持久性的特点,可用性高达99.999%。给现有的EC2实例扩展新的存储块只需要几分钟的时间,省时省力。每个EBS块都被放置在一个特定的可用区内,并且会自动维护一个副本,随时保护数据安全。</p><!-- more --><p>Amazon EBS共提供三种硬盘类型,SSD(固态硬盘), Provisioned IOPS SSD(特供IOPS固态硬盘)和Magnetic(普通硬盘)。SSD是默认的EC实例的硬盘格式(凶残啊!Amazon真是有钱,自从咱的本本换了SSD后,也离不开SSD了。)Provisioned IOPS SSD更凶残,具有高一致性及超低延迟的性能,专门设计用于I/O密集型操作,比如数据库。IOPS全称为Input/Output Operations Per Second,即每秒进行读写(I/O)操作的次数,用来衡量随机访问的性能。Provisioned IOPS SSD的每GB IOPS可达30。Magnetic Volumes就是俗称的磁卷了,它最便宜,当然性能也最差了。如果你的应用程序不是I/O密集型的,对数据的访问不是很频繁,使用该类型就比较划算。</p><p>EBS还有一个功能就是很方便的创建快照,并保存到Amazon S3上去。EBS的快照是增量存储的。比如你有一块100G的硬盘,第一次快照使用了100G容量,第二次快照时只有5G的数据发生了变化,那么你总共只需花105G的存储费用。当然你不不必担心删除其中一个快照导致数据丢失,因为删除一个快照时,系统只会删除其中不会被其它快照使用的数据,所以不会影响其它快照。</p><p>好了,接下来是本文的两个主要任务是两个。第一个任务是给一个EC实例附加一个新的EBS卷,并且加入到当前EC实例的文件系统中。第二个任务是为该EC实例创建一个快照。</p><p>首先当然是准备工作了,创建一个新的EC2实例。如何创建上一篇文章已经详细介绍了,这里不再累述。如果还没有AWS account的同学可以使用可以qwiklabs提供的免费实验来进行该练习。地址是<a href="https://run.qwiklabs.com">https://run.qwiklabs.com</a>。说实话qwiklabs真是个好东西,如果我使用自己的account来进行练习,要花不少钱。而qwiklabs里面有几个免费实验,使用AWS的所有资源不收任何费用,真是业界良心。</p><p>EC2创建完毕后,应该是这个样子。</p><p><img src="http://www.huangbowen.net/images/ec2instance01.png" width="700"></p><p>然后远程ssh进去。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="nv">$:</span> ssh ec2-user@54.191.59.163 -i ~/Downloads/key.pem<br/></span></code></td></tr></table></div></figure><p>先查看下当前的磁盘情况。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/><span class="line-number">2</span><br/><span class="line-number">3</span><br/><span class="line-number">4</span><br/><span class="line-number">5</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>df -h<br/></span><span class="line">Filesystem Size Used Avail Use% Mounted on<br/></span><span class="line">/dev/xvda1 7.8G 1.1G 6.6G 14% /<br/></span><span class="line">devtmpfs 486M 56K 486M 1% /dev<br/></span><span class="line">tmpfs 499M 0 499M 0% /dev/shm<br/></span></code></td></tr></table></div></figure><p>可以看出当前磁盘总大小在8G左右,有三个文件系统。</p><p>接下来就创建一个硬盘。</p><p>点击EC2控制面板左侧的<code>Volumes</code>按钮,来到磁盘的控制面板。</p><p><img src="http://www.huangbowen.net/images/volumedb01.png" width="700"></p><p>这里展示的一块硬盘就是当前ec2实例使用的硬盘。</p><p>我们点击上面的<code>Create Volumes</code>来新添加一块硬盘。</p><p><img src="http://www.huangbowen.net/images/createVolume01.png" width="700"></p><p>硬盘类型就是我上文的说的那几种,IOPS越高,硬盘就越贵,可用区的选择一定要和当前要使用的EC2实例保持一致,要不然会无法跨可用区附加硬盘。如果提供snapshot ID的话,新创建的硬盘会具有该快照的数据。</p><p>硬盘创建完毕后如下图所示。</p><p><img src="http://www.huangbowen.net/images/newVolumes01.png" width="700"></p><p>目前它的状态是可用的,如果将其附加到EC2实例则会变为in-use。</p><p>点击<code>Action</code>按钮并选择<code>Attach Volume</code>命令,可以将该硬盘附加到EC2实例上。</p><p><img src="http://www.huangbowen.net/images/attachVolume01.png" width="700"></p><p>在该界面中选择实例名及映射的设备地址,然后附加。</p><p><img src="http://www.huangbowen.net/images/volumesStatus01.png" width="700"></p><p>可以看到该硬盘状态已经发生改变。</p><p>那么再回到终端中查看磁盘情况。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/><span class="line-number">2</span><br/><span class="line-number">3</span><br/><span class="line-number">4</span><br/><span class="line-number">5</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>df -h<br/></span><span class="line">Filesystem Size Used Avail Use% Mounted on<br/></span><span class="line">/dev/xvda1 7.8G 1.1G 6.6G 14% /<br/></span><span class="line">devtmpfs 486M 60K 486M 1% /dev<br/></span><span class="line">tmpfs 499M 0 499M 0% /dev/shm<br/></span></code></td></tr></table></div></figure><p>跟以前的一模一样,没有看到新添加的20G硬盘。这是怎么回事那?这是因为你虽然给机器添加了一块硬盘,但是还没有格式化和创建文件系统,并将文件系统添加到EC2的文件系统树上来。</p><p>我们换另一个命令来查看存储情况。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/><span class="line-number">2</span><br/><span class="line-number">3</span><br/><span class="line-number">4</span><br/><span class="line-number">5</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>lsblk<br/></span><span class="line">NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT<br/></span><span class="line">xvda 202:0 0 8G 0 disk<br/></span><span class="line">└─xvda1 202:1 0 8G 0 part /<br/></span><span class="line">xvdf 202:80 0 20G 0 disk<br/></span></code></td></tr></table></div></figure><p>这里可以看到有一块20G的大硬盘xvdf,但是没有MOUNTPOINT,这个MOUNTPOINT你可以理解为EC2实例上针对这个硬盘的根地址。</p><p>细心的读者可能会发现我们附加硬盘时输入的地址是<code>/dev/sdf</code>,为什么命令行中显示的是<code>xvdf</code>那?这是因为调皮的操作系统内核给改了,不同的Linux发行版其修改后的地址也会不太一样。</p><p>使用file命令来查看该块硬盘有没有建立文件系统。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/><span class="line-number">2</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>sudo file -s /dev/xvdf<br/></span><span class="line">/dev/xvdf: data<br/></span></code></td></tr></table></div></figure><p>如果输出是data,说明该块硬盘还未创建文件系统。</p><p>使用下述命令来给xvdf来创建文件系统。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>sudo mkfs -t ext4 /dev/xvdf<br/></span></code></td></tr></table></div></figure><p>然后再用file命令确认。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/><span class="line-number">2</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>sudo file -s /dev/xvdf<br/></span><span class="line">/dev/xvdf: Linux rev 1.0 ext4 filesystem data, <span class="nv">UUID</span><span class="o">=</span>1e14ec91-156a-4eb5-8846-cb5f2fa51b64 <span class="o">(</span>extents<span class="o">)</span> <span class="o">(</span>large files<span class="o">)</span> <span class="o">(</span>huge files<span class="o">)</span><br/></span></code></td></tr></table></div></figure><p>bash</p><p>可以看到该块硬盘已经建立了ext4格式的文件系统。</p><p>接下来要将该文件系统加入到当前实例的文件系统树中,这里要使用mount命令。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/><span class="line-number">2</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>sudo mkdir /boot2<br/></span><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>sudo mount /dev/xvdf /boot2<br/></span></code></td></tr></table></div></figure><p>搞定,现在看看EC2实例的存储情况。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/><span class="line-number">2</span><br/><span class="line-number">3</span><br/><span class="line-number">4</span><br/><span class="line-number">5</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>lsblk<br/></span><span class="line">NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT<br/></span><span class="line">xvda 202:0 0 8G 0 disk<br/></span><span class="line">└─xvda1 202:1 0 8G 0 part /<br/></span><span class="line">xvdf 202:80 0 20G 0 disk /boot2<br/></span></code></td></tr></table></div></figure><p>可以在/boot2目录下随意创建文件和文件夹了。</p><p>这里并不是万事大吉的时候,我们需要将该MOUNTPOINT加入到系统文件中,要不然系统重启后还是找不到该硬盘。编辑<code>/etc/fstab</code>文件,在其中加入以下行。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/></td><td class="code"><code class="text"><span class="line">/dev/xvdf /data ext4 defaults,nofail 0 2<br/></span></code></td></tr></table></div></figure><p>这样就不怕重启的时候丢失该MOUNTPOINT了。添加完毕以后可以试一下fstab文件是否能正常运行。</p><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><span class="line-number">1</span><br/></td><td class="code"><code class="bash"><span class="line"><span class="o">[</span>ec2-user@ip-172-31-17-37 ~<span class="o">]</span><span class="nv">$ </span>sudo mount -a<br/></span></code></td></tr></table></div></figure><p>如果没有错误就一切万事大吉,如果有错误而你重启了电脑的话,你就等着哭吧。</p><p>这是第一个任务,第二任务是给现有硬盘创建快照,非常简单。回到<code>Volumes</code>控制面板,选择<code>Action</code>里的<code>Create Snapshot</code>按钮。</p><p><img src="http://www.huangbowen.net/images/createSnapshot01.png" width="700"></p><p>等一会snapshot就会创建成功。</p><p><img src="http://www.huangbowen.net/images/snapshotStatus01.png" width="700"></p></div></article></div><img src="http://counter.cnblogs.com/blog/rss/3879542" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/huang0925/p/3879542.html" target="_blank">AWS系列之三 使用EBS</a>,转载请注明。</p>http://www.cnblogs.com/jacksu-tencent/p/3878780.htmlLinux内核策略介绍学习笔记 - jack(小保)在上周听了mike的关于linux内核策略的分享,这可是一个长达四个小时的分享,其中一句话印象非常深刻就是,“好的系统性能不是压测出来的,是设计出来的”。内容主要通过后台服务依赖的硬件资源(CPU、内存、外存、网络、其他)主线来介绍的。2014-07-30T15:07:00Z2014-07-30T15:07:00Zjack(小保)http://www.cnblogs.com/jacksu-tencent/<p><strong>主要内容</strong></p><p>| 硬件 | 策略 |<br />| :--------: | :-----: ||CPU |进程调度、系统调用、中断||内存 |内存管理||外存 |文件IO||网络 |协议栈||其他 |时间管理|</p><p><img src="http://p.blog.csdn.net/images/p_blog_csdn_net/gausing/EntryImages/20090609/linux内核代码架构图-清晰版.png" alt="linux kernel map" /></p><hr /><p><strong>进程调度</strong></p><p><strong>内核的运行时间</strong></p><p>系统启动、中断发生、系统调用以及<strong>内核线程</strong>。</p><p><strong>进程和线程的区别</strong></p><blockquote> <ul> <li>在linux中,底层都是调用clone函数。</li> <li>进程创建为60~8000次/s,线程为5~10w/s</li> <li>进程创建调用fork(),主要瓶颈在页表复制,如果一个进程没有访问内存,页表共享的内存栈不需要复制页表。(例如:一个网络处理服务,只是收到包,fork进程进行内存访问,性能应该在一万左右,但是如果主进程简单做一些处理的话,性能就会下降为一千左右。)</li> <li>线程创建调用pthread_create(),主要瓶颈在堆栈(8k)分配上,如果并发线程增多1w时约为5w</li> <li>并发性能CPU切换,进程30~100w/s,线程50~150w/s</li> <li>内存消耗,进程要比线程更消耗内存(3~N倍)</li> </ul></blockquote><p><strong>进程调度</strong></p><blockquote> <ul> <li>sched_yield更细粒度让出cpu</li> <li>各个核都有自己的运行队列</li> <li>就近balance原则,运行clone、wakeup的进程就在该核</li> <li>CPU绑定函数sched_setaffinity</li> </ul></blockquote><p><strong>中断</strong></p><blockquote> <ul> <li>通过/proc/interrupt查看中断</li> <li>中断主要来源于时钟、网卡、硬盘</li> <li>通过/proc/irq/N/smp_affinity查看在那个核</li> <li>调度顺序:硬中断调度->软中断->进程调度</li> </ul></blockquote><p><strong>常用系统调用性能</strong></p><p>是在C类机器测试,自己使用最好在自己的机器类型测试。</p><p>||性能(次/秒)||:-----:|:------:||ntohl |2.5亿||memset 1k |150w||getppid |100w||gettimeofday|30~400w||time |30w~400w~1000w||shmget|300w||shmat|40w||socket|10w~50w||sendto|40w||fork||60~8000||pthread_create|5~10w|</p><p><strong>工具</strong></p><blockquote> <ul> <li>strace</li> <li>perf</li> </ul></blockquote><hr /><p><strong>内存管理</strong></p><blockquote> <ul> <li>查看进程地址空间/proc/PID/maps或者pmap</li> <li>防止内存交换出去调用mlock,如果内存使用非常紧张的时候得注意</li> <li>空闲内存计算:/proc/meminfo查看,MemFree+cached+buffer-Mapped</li> </ul></blockquote><p><strong>共享内存建立方法</strong></p><blockquote> <ul> <li>IPC 我们经常使用</li> <li>shm_open,文件操作,方便append和dump</li> <li>tmpfs可以直接挂载到某个盘</li> <li>hugetlbfs节省页表空间</li> </ul></blockquote><p><strong>内存权限</strong></p><p>||shared|private||:---:|:---:|:---:||File|共享内存|数据段、代码段||anon|父子进程|堆|</p><hr /><p><strong>文件IO</strong></p><p>文件系统层次架构</p><blockquote> <ul> <li>VFS Layer</li> <li>NFS EXT2 EXT3 NTFS</li> <li>Page Cache</li> <li>Generic Block Layer</li> <li>I/O Scheduler Layer</li> <li>Block Device Driver Layer</li> <li>Block Device Layer</li> </ul></blockquote><p>PageCache起了缓存得作用,采用read ahead机制,如果我们顺序读性能比较好,随机读性能就比较差了。对于写,如果在pagecache中存在,那么直接写PacheCache中的页就可以,否则需要加载该页内容,然后写入PageCache,最后delay write。</p><p>虚拟文件系统为filetab[fd]->file->dentry->inode->mapping.dentry采用全局hash,查找性能为30~35w/s,100w个文件也为30w/s。</p><p>EXT3添加了日志模式,由高到低为journal、ordered、writeback</p><p><strong>协议栈</strong></p><p><img src="http://images.cnblogs.com/cnblogs_com/jacksu-tencent/531889/o_protocol_stack.jpg" alt="prototcol_stack" /></p><ul><li><p>recv_queue中的包大小,为内核的包大小,而不是ip包大小。</p></li><li><p>如果发出去的包太大,需要修改write<em>queue和tx</em>queue两个参数,tx_queue主要是流量控制。</p></li><li>多进程必须在socket后再fork,即使设置了REUSEADDR,从hashtable看出原因。</li><li>net.ipv4.tcp<em>max</em>syn<em>backlog参数决定了SYN</em>RECV状态队列的数量,一般默认值为512或者1024,即超过这个数量,系统将不再接受新的TCP连接请求.</li><li><a href="http://blog.csdn.net/justlinux2010/article/details/12619761">sync cookie</a>,外网要注意,同一个集线器来的用户,可能导致大量不可以建立链接。</li><li>somaxconn决定了listen监听队列的大小</li><li>select有1024的限制,即使没有达到1024,但是分配的fd大于1024也会有问题。</li><li>epoll多用于单进程多线程</li></ul><p><strong>时间</strong></p><ul><li>gettimeofday根据硬件不同性能差距很大</li><li>usleep(1),睡眠不一定是1微妙,有可能是毫秒级的,也和硬件有关。</li></ul><p><strong>参考文献</strong></p><p>man 2</p><p>man 7</p><p>ULK</p><img src="http://counter.cnblogs.com/blog/rss/3878780" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/jacksu-tencent/p/3878780.html" target="_blank">Linux内核策略介绍学习笔记</a>,转载请注明。</p>http://www.cnblogs.com/zhukunrong/p/3879462.htmlYosemite 升级后第三方SSD TRIM失败不能进入系统处理 - 祝坤荣no zuo no die, 这把手欠升级到了Yosemite, 然后发现原来在Mavericks里已经激活的TRIM在这里不行了, 又提示trim enable软件不适于此版本。然后,悲剧就开始了, 随手找了一个老帖子,按照那个命令把内核文件改了, 也没管适不适用于Yosemite, 然后重启, ...2014-07-30T15:03:00Z2014-07-30T15:03:00Z祝坤荣http://www.cnblogs.com/zhukunrong/<p>no zuo no die, 这把手欠升级到了Yosemite, 然后发现原来在Mavericks里已经激活的TRIM在这里不行了, 又提示trim enable软件不适于此版本。</p><p>然后,悲剧就开始了, 随手找了一个老帖子,按照那个命令把内核文件改了, 也没管适不适用于Yosemite, 然后重启, 就进不去了。。。</p><p>一个禁止的标志横在那, 心哇凉啊, 我都还没做Mavericks的U盘安装盘,想降回去也不行啊。 拿着PC在那狂搜, 看来老外比我早, 有人解决了,遇到这个情况的朋友,输入以下救命神迹,大喊阿门后重启:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">rm</span> -rf /Volumes/&lt;<span style="color: #800080;">10.10</span> Partition&gt;/System/Library/Extensions/<span style="color: #000000;">IOAHCIFamily.kext<br/><br/></span><span style="color: #0000ff;">cp</span> -r /System/Library/Extensions/IOAHCIFamily.kext /Volumes/&lt;<span style="color: #800080;">10.10</span> Partition&gt;/System/Library/Extensions/<span style="color: #000000;">IOAHCIFamily.kext<br/><br/></span><span style="color: #0000ff;">touch</span> /Volumes/&lt;<span style="color: #800080;">10.10</span> Partition&gt;/System/Library/<span style="color: #000000;">Extensions<br/>kextcache </span>-u /Volumes/&lt;<span style="color: #800080;">10.10</span> Partition&gt;</div><p>就能进入系统了, 然后还是用那个老软件trim enable,配合命令激活TRIM,以下摘自论坛,亲测可用</p><p>1.如果 你电脑上已经使用过Trim Enabler,删除以下文件<br clear="none" />&nbsp; &nbsp;/Library/PrivilegedHelperTools/org.Cindori.AuthHelper</p><p>2.在终端打命令:sudo nvram boot-args="kext-dev-mode=1"<br clear="none" />3.重启电脑这个必须的。。。<br clear="none" />4.使用Trim Enabler软件开启trim,记住不要重启电脑<br clear="none" />5.等上1分钟时间<br clear="none" />6.在终端打命令:sudo kextcache -m /System/Library/Caches/com.apple.kext.caches/Startup/Extensions.mkext /System/Library/Extensions<br clear="none" />7.等待命令完成。。。<br clear="none" />8.重启电脑</p><p>查看SSD的TRIM已经开启。</p><p><img src="http://images.cnitblog.com/i/328530/201407/302302158996173.png" alt="" /></p><img src="http://counter.cnblogs.com/blog/rss/3879462" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/zhukunrong/p/3879462.html" target="_blank">Yosemite 升级后第三方SSD TRIM失败不能进入系统处理</a>,转载请注明。</p>http://www.cnblogs.com/jianyus/p/3877919.htmlSharePoint 2013 隐藏部分Ribbon菜单 - 霖雨SharePoint的使用中,因为用户经常不愿意看到那些不经常使用的操作,我们经常需要定制Ribbon菜单, 更多时候不是隐藏所有,而是隐藏掉我们不需要的那些;下面,我们一列表为例,简单介绍下如何部分隐藏Ribbon菜单 1、默认情况下,List的Ribbon菜单有Item和List两个部分,...2014-07-30T13:57:00Z2014-07-30T13:57:00Z霖雨http://www.cnblogs.com/jianyus/<p>  SharePoint的使用中,因为用户经常不愿意看到那些不经常使用的操作,我们经常需要定制Ribbon菜单, 更多时候不是隐藏所有,而是隐藏掉我们不需要的那些;下面,我们一列表为例,简单介绍下如何部分隐藏Ribbon菜单</p><p>  1、默认情况下,List的Ribbon菜单有Item和List两个部分,如下图:</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301408544157472.jpg"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image002" src="http://images.cnitblog.com/blog/256729/201407/301408551809072.jpg" alt="clip_image002" width="700" height="429" border="0" /></a></p><p>  2、List的Ribbon菜单,List选项卡,这里面普通用户基本不需要;</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409011027296.jpg"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image004" src="http://images.cnitblog.com/blog/256729/201407/301409052589092.jpg" alt="clip_image004" width="700" height="429" border="0" /></a></p><p>  3、创建解决方案,里面添加一个webpart,用来隐藏Ribbon菜单;</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409095404618.jpg"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image006" src="http://images.cnitblog.com/blog/256729/201407/301409128681301.jpg" alt="clip_image006" width="700" height="534" border="0" /></a></p><p>  需要引用Microsoft.Web.CommandUI,位置如下;</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">C:\Program Files\Common Files\microsoft shared\Web Server Extensions\<span style="color: #800080;">15</span>\ISAPI\Microsoft.Web.CommandUI.dll</div><p>  4、添加属性RibbonCollection,用来保存隐藏的Ribbon的id,多个用半角分号区分;然后添加隐藏菜单的方法,如下图:</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409167742342.jpg"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image008" src="http://images.cnitblog.com/blog/256729/201407/301409230089536.jpg" alt="clip_image008" width="700" height="560" border="0" /></a></p><p>  5、在需要隐藏的页面,添加我们的webpart,如下图:</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409269933619.jpg"><img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image010" src="http://images.cnitblog.com/blog/256729/201407/301409326021873.jpg" alt="clip_image010" width="700" height="187" border="0" /></a></p><p>  6、在 我们webpart的设置里,添加需要隐藏的Ribbon ID,如下图:</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409360085299.jpg"><img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image012" src="http://images.cnitblog.com/blog/256729/201407/301409396333425.jpg" alt="clip_image012" width="373" height="185" border="0" /></a></p><p>  7、详细看Ribbon ID,多个用半角分号分隔;</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409445081836.jpg"><img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image014" src="http://images.cnitblog.com/blog/256729/201407/301409500872631.jpg" alt="clip_image014" width="467" height="157" border="0" /></a></p><p>  8、查看隐藏以后的效果,只剩下列表项的基本操作,没有了List选项卡和工作流等菜单,如下图:</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409506652288.jpg"><img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image016" src="http://images.cnitblog.com/blog/256729/201407/301409554156969.jpg" alt="clip_image016" width="635" height="383" border="0" /></a></p><p>  9、查看Ribbon ID,通常我们可以通过微软的文档查看;同时,我们也可以使用F12在页面上查看,如下图:</p><p><a href="http://images.cnitblog.com/blog/256729/201407/301409559936626.jpg"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="clip_image018" src="http://images.cnitblog.com/blog/256729/201407/301409568831955.jpg" alt="clip_image018" width="635" height="383" border="0" /></a></p><p>  类似Ribbon.ListItem.Manage.ManagePermissions-Medium这样的,我们需要去掉最后的Medium,这个只是代表菜单的大小,前面的部分就是Ribbon Id;</p><p><strong>总 结</strong></p><p>  我这里是针对页面隐藏Ribbon,所以使用webpart,如果需要针对全局隐藏,可以写用户控件,然后放到母版页里,即可完成隐藏。</p><img src="http://counter.cnblogs.com/blog/rss/3877919" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/jianyus/p/3877919.html" target="_blank">SharePoint 2013 隐藏部分Ribbon菜单</a>,转载请注明。</p>http://www.cnblogs.com/52cik/p/chrome-base64.htmlchrome 隐藏技能之 base64 图片转换 - 乱码.有时候我们要转换图片为base64,或者将base64转回图片,可能都需要找一些在线工具或者软件类型的工具才行。当然 chrome 也算是软件,但是好在做前端的都有 chrome。好了,来看下简单的例子,我随便截了个桌面背景当例子。分别保存为 png, jpg 两种。然后用 chrome 打开图片,...2014-07-30T13:54:00Z2014-07-30T13:54:00Z乱码.http://www.cnblogs.com/52cik/<p class="nop"><img class="nop" src="http://files.cnblogs.com/52cik/caption.gif" alt="" border="0" /></p><p>有时候我们要转换图片为base64,或者将base64转回图片,可能都需要找一些在线工具或者软件类型的工具才行。<br />当然 chrome 也算是软件,但是好在做前端的都有 chrome。<br />好了,来看下简单的例子,我随便截了个桌面背景当例子。<br /><img src="http://images.cnitblog.com/i/477954/201407/302148426025526.jpg" alt="" /><br />分别保存为 png, jpg 两种。</p><p>然后用 chrome 打开图片,接着按 F12 打开控制台,在 Sources 或 Resources 都行。<br /><img src="http://images.cnitblog.com/i/477954/201407/302148590084714.jpg" alt="" /><br />看到了吧,在最后一个箭头处就是我们要的 base64 数据了,非常简单。<br />当然这里没有data头,用的时候再加上即可。</p><p>&nbsp;<span style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">data:image/jpg<span style="color: #008000;">;</span><span style="color: #008000;">base64,这里加上刚才的base64数据。</span></span>&nbsp;</p><p>当然 image/jpg 是图片类型,如果你懒,全用 image/jpg 也是可以的。<br />如果你要强迫症,那就写对应的类型吧,比如 image/png, image/gif 什么的。</p><p>图片转换为 base64 简单吧,分分钟搞定。<br />当然你要把 base64 数据转为图片,也是非常简单的,<br />直接在地址栏打开 &nbsp;<span style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">data:image/jpg<span style="color: #008000;">;</span><span style="color: #008000;">base64,图片base64数据</span></span>&nbsp;&nbsp;即可在浏览器上显示图片,然后右键保存图片即可。</p><p>好了,今天就分享这么个小知识点。</p><img src="http://counter.cnblogs.com/blog/rss/3879230" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/52cik/p/chrome-base64.html" target="_blank">chrome 隐藏技能之 base64 图片转换</a>,转载请注明。</p>http://www.cnblogs.com/tanlon/p/3879208.html[原]零基础学习视频解码之解码图像 - 雪夜&流星本文全部是在Ubuntu 14.04下使用Eclipse开发,本文假定你Eclipse已经安装好了CDT,以及安装好了FFmpeg和SDL。 解码流程详见上一篇博客[原]零基础学习视频解码之FFMpeg中比较重要的函数以及数据结构。 如何安?FFmpeg详见上一篇博客:[原]零基础学习视频解...2014-07-30T13:45:00Z2014-07-30T13:45:00Z雪夜&amp;流星http://www.cnblogs.com/tanlon/<p>  本文全部是在Ubuntu 14.04下使用Eclipse开发,本文假定你Eclipse已经安装好了CDT,以及安装好了FFmpeg和SDL。</p><p>  解码流程详见上一篇博客<a href="http://www.cnblogs.com/tanlon/p/3879081.html" target="_blank">[原]零基础学习视频解码之FFMpeg中比较重要的函数以及数据结构</a>。</p><p>  如何安?FFmpeg详见上一篇博客:<a id="cb_post_title_url" href="http://www.cnblogs.com/tanlon/p/3878783.html">[原]零基础学习视频解码之安装ffmpeg</a></p><p>  下图是解码流程图(全图):</p><p><img src="http://images.cnitblog.com/i/233920/201407/302057239778830.png" alt="" /></p><p>&nbsp; &nbsp; 本文详解如何在Eclipse中使用使用C来完成一个解码的过程并且将其解码出来的YUV图片保存下来:</p><p>  一、新建一个C Project,添加相关库的支持:</p><p>   Project-&gt;Properties-&gt;C/C++ General-&gt;Path and Symbols-&gt;Libraries-&gt;Add-&gt;avcode、avutil、avformat、swscale</p><p> 二、在工程中引用相应的头文件:</p><p>   </p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> puts(</span><span style="color: #800000;">"</span><span style="color: #800000;">!!!Hello World!!!</span><span style="color: #800000;">"</span>); <span style="color: #008000;">/*</span><span style="color: #008000;"> prints !!!Hello World!!! </span><span style="color: #008000;">*/</span><br/> <span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  三、定义AVFormatContext、AVCodecContext、AVCodec</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i;<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  四、注册所有格式以及初始化AVFormatContext</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  五、打开一个视频文件,获取视频文件的流信息,&nbsp;获取初始的视频流</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  六、获得视频流编码内容,获取视频编码格式,用一个编码格式打开一个编码文件</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pCodecCtx </span>= pFormatCtx-&gt;streams[videoStream]-&gt;<span style="color: #000000;">codec;<br/> pCodec </span>= avcodec_find_decoder(pCodecCtx-&gt;<span style="color: #000000;">codec_id);<br/><br/> </span><span style="color: #0000ff;">if</span> (pCodec ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Codec not found.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not open codec.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  七、申请内存用来作为图片文件(AVPicture)缓冲</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> AVFrame </span>*pFrame, *<span style="color: #000000;">pFrameYUV;<br/> uint8_t </span>*<span style="color: #000000;">out_buffer;<br/><br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i, numBytes;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pCodecCtx </span>= pFormatCtx-&gt;streams[videoStream]-&gt;<span style="color: #000000;">codec;<br/> pCodec </span>= avcodec_find_decoder(pCodecCtx-&gt;<span style="color: #000000;">codec_id);<br/><br/> </span><span style="color: #0000ff;">if</span> (pCodec ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Codec not found.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not open codec.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pFrame </span>=<span style="color: #000000;"> av_frame_alloc();<br/> pFrameYUV </span>=<span style="color: #000000;"> av_frame_alloc();<br/><br/> numBytes </span>= avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height);<br/> out_buffer </span>= (uint8_t *) av_malloc(numBytes * <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(uint8_t));<br/> avpicture_fill((AVPicture </span>*<span style="color: #000000;">) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,<br/> pCodecCtx</span>-&gt;width, pCodecCtx-&gt;<span style="color: #000000;">height);<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  八、为AVPacket申请内存</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> AVFrame </span>*pFrame, *<span style="color: #000000;">pFrameYUV;<br/> AVPacket </span>*<span style="color: #000000;">packet;<br/> uint8_t </span>*<span style="color: #000000;">out_buffer;</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i, numBytes;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ret, got_picture;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pCodecCtx </span>= pFormatCtx-&gt;streams[videoStream]-&gt;<span style="color: #000000;">codec;<br/> pCodec </span>= avcodec_find_decoder(pCodecCtx-&gt;<span style="color: #000000;">codec_id);<br/><br/> </span><span style="color: #0000ff;">if</span> (pCodec ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Codec not found.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not open codec.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pFrame </span>=<span style="color: #000000;"> av_frame_alloc();<br/> pFrameYUV </span>=<span style="color: #000000;"> av_frame_alloc();<br/><br/> numBytes </span>= avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height);<br/> out_buffer </span>= (uint8_t *) av_malloc(numBytes * <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(uint8_t));<br/> avpicture_fill((AVPicture </span>*<span style="color: #000000;">) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,<br/> pCodecCtx</span>-&gt;width, pCodecCtx-&gt;<span style="color: #000000;">height);<br/><br/> </span><span style="color: #0000ff;">int</span> y_size = pCodecCtx-&gt;width * pCodecCtx-&gt;<span style="color: #000000;">height;<br/><br/> packet </span>= (AVPacket *) malloc(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(AVPacket));<br/> av_new_packet(packet, y_size);<br/><br/> av_dump_format(pFormatCtx, </span><span style="color: #800080;">0</span>, filePath, <span style="color: #800080;">0</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  九、从AVFrame中读取AVPacket,解码video得到YUV图像AVPicture</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> AVFrame </span>*pFrame, *<span style="color: #000000;">pFrameYUV;<br/> AVPacket </span>*<span style="color: #000000;">packet;<br/> uint8_t </span>*<span style="color: #000000;">out_buffer;<br/><br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i, numBytes;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ret, got_picture;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pCodecCtx </span>= pFormatCtx-&gt;streams[videoStream]-&gt;<span style="color: #000000;">codec;<br/> pCodec </span>= avcodec_find_decoder(pCodecCtx-&gt;<span style="color: #000000;">codec_id);<br/><br/> </span><span style="color: #0000ff;">if</span> (pCodec ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Codec not found.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not open codec.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pFrame </span>=<span style="color: #000000;"> av_frame_alloc();<br/> pFrameYUV </span>=<span style="color: #000000;"> av_frame_alloc();<br/><br/> numBytes </span>= avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height);<br/> out_buffer </span>= (uint8_t *) av_malloc(numBytes * <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(uint8_t));<br/> avpicture_fill((AVPicture </span>*<span style="color: #000000;">) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,<br/> pCodecCtx</span>-&gt;width, pCodecCtx-&gt;<span style="color: #000000;">height);<br/><br/> </span><span style="color: #0000ff;">int</span> y_size = pCodecCtx-&gt;width * pCodecCtx-&gt;<span style="color: #000000;">height;<br/><br/> packet </span>= (AVPacket *) malloc(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(AVPacket));<br/> av_new_packet(packet, y_size);<br/><br/> av_dump_format(pFormatCtx, </span><span style="color: #800080;">0</span>, filePath, <span style="color: #800080;">0</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">while</span> (av_read_frame(pFormatCtx, packet) &gt;= <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (packet-&gt;stream_index ==<span style="color: #000000;"> videoStream) {<br/> ret </span>= avcodec_decode_video2(pCodecCtx, pFrame, &amp;<span style="color: #000000;">got_picture,<br/> packet);<br/><br/> </span><span style="color: #0000ff;">if</span> (ret &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">decode error.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (got_picture) {<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">TODO 保存解码出来的图片</span><br/><span style="color: #000000;"> }<br/> }<br/> av_free_packet(packet);<br/> }<br/><br/> av_free(out_buffer);<br/> av_free(pFrameYUV);<br/> avcodec_close(pCodecCtx);<br/> avformat_close_input(</span>&amp;<span style="color: #000000;">pFormatCtx);<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>   十、利用sws_scale?得到的YUV转换成Image data</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> AVFrame </span>*pFrame, *<span style="color: #000000;">pFrameYUV;<br/> AVPacket </span>*<span style="color: #000000;">packet;<br/> uint8_t </span>*<span style="color: #000000;">out_buffer;<br/><br/> </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">struct</span> SwsContext *<span style="color: #000000;">img_convert_ctx;<br/><br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i, numBytes;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ret, got_picture;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pCodecCtx </span>= pFormatCtx-&gt;streams[videoStream]-&gt;<span style="color: #000000;">codec;<br/> pCodec </span>= avcodec_find_decoder(pCodecCtx-&gt;<span style="color: #000000;">codec_id);<br/><br/> </span><span style="color: #0000ff;">if</span> (pCodec ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Codec not found.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not open codec.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pFrame </span>=<span style="color: #000000;"> av_frame_alloc();<br/> pFrameYUV </span>=<span style="color: #000000;"> av_frame_alloc();<br/><br/> numBytes </span>= avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height);<br/> out_buffer </span>= (uint8_t *) av_malloc(numBytes * <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(uint8_t));<br/> avpicture_fill((AVPicture </span>*<span style="color: #000000;">) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,<br/> pCodecCtx</span>-&gt;width, pCodecCtx-&gt;<span style="color: #000000;">height);<br/><br/> </span><span style="color: #0000ff;">int</span> y_size = pCodecCtx-&gt;width * pCodecCtx-&gt;<span style="color: #000000;">height;<br/><br/> packet </span>= (AVPacket *) malloc(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(AVPacket));<br/> av_new_packet(packet, y_size);<br/><br/> av_dump_format(pFormatCtx, </span><span style="color: #800080;">0</span>, filePath, <span style="color: #800080;">0</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">while</span> (av_read_frame(pFormatCtx, packet) &gt;= <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (packet-&gt;stream_index ==<span style="color: #000000;"> videoStream) {<br/> ret </span>= avcodec_decode_video2(pCodecCtx, pFrame, &amp;<span style="color: #000000;">got_picture,<br/> packet);<br/><br/> </span><span style="color: #0000ff;">if</span> (ret &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">decode error.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (got_picture) {<br/> img_convert_ctx </span>= sws_getContext(pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;height, pCodecCtx-&gt;pix_fmt, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height, AV_PIX_FMT_YUV420P,<br/> SWS_BICUBIC, NULL, NULL, NULL);<br/> sws_scale(img_convert_ctx,<br/> (</span><span style="color: #0000ff;">const</span> uint8_t* <span style="color: #0000ff;">const</span> *) pFrame-&gt;data, pFrame-&gt;<span style="color: #000000;">linesize,<br/> </span><span style="color: #800080;">0</span>, pCodecCtx-&gt;height, pFrameYUV-&gt;<span style="color: #000000;">data,<br/> pFrameYUV</span>-&gt;<span style="color: #000000;">linesize);<br/> sws_freeContext(img_convert_ctx);<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">TODO 保存解码出来的图片</span><br/><span style="color: #000000;"> }<br/> }<br/> av_free_packet(packet);<br/> }<br/><br/> av_free(out_buffer);<br/> av_free(pFrameYUV);<br/> avcodec_close(pCodecCtx);<br/> avformat_close_input(</span>&amp;<span style="color: #000000;">pFormatCtx);<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  十一、将解码的data保存为.ppm</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">void</span> SaveFrame(AVFrame *pFrame, <span style="color: #0000ff;">int</span> width, <span style="color: #0000ff;">int</span> height, <span style="color: #0000ff;">int</span><span style="color: #000000;"> i) {<br/> FILE </span>*<span style="color: #000000;"> pFile;<br/> </span><span style="color: #0000ff;">char</span> szFilename[<span style="color: #800080;">32</span><span style="color: #000000;">];<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> y;<br/><br/> sprintf(szFilename, </span><span style="color: #800000;">"</span><span style="color: #800000;">frame%d.ppm</span><span style="color: #800000;">"</span><span style="color: #000000;">, i);<br/><br/> pFile </span>= fopen(szFilename, <span style="color: #800000;">"</span><span style="color: #800000;">wb</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">if</span> (pFile ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">pFile is null</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br/> }<br/><br/> fprintf(pFile, </span><span style="color: #800000;">"</span><span style="color: #800000;">P6\n%d %d\n255\n</span><span style="color: #800000;">"</span><span style="color: #000000;">, width, height);<br/><br/> </span><span style="color: #0000ff;">for</span> (y = <span style="color: #800080;">0</span>; y &lt; height; y++<span style="color: #000000;">) {<br/> fwrite(pFrame</span>-&gt;data[<span style="color: #800080;">0</span>] + y * pFrame-&gt;linesize[<span style="color: #800080;">0</span>], <span style="color: #800080;">1</span>, width * <span style="color: #800080;">3</span><span style="color: #000000;">, pFile);<br/> }<br/><br/> fclose(pFile);<br/>}<br/><br/></span><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> AVFrame </span>*pFrame, *<span style="color: #000000;">pFrameYUV;<br/> AVPacket </span>*<span style="color: #000000;">packet;<br/> uint8_t </span>*<span style="color: #000000;">out_buffer;<br/><br/> </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">struct</span> SwsContext *<span style="color: #000000;">img_convert_ctx;<br/><br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i, numBytes;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ret, got_picture;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pCodecCtx </span>= pFormatCtx-&gt;streams[videoStream]-&gt;<span style="color: #000000;">codec;<br/> pCodec </span>= avcodec_find_decoder(pCodecCtx-&gt;<span style="color: #000000;">codec_id);<br/><br/> </span><span style="color: #0000ff;">if</span> (pCodec ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Codec not found.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not open codec.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pFrame </span>=<span style="color: #000000;"> av_frame_alloc();<br/> pFrameYUV </span>=<span style="color: #000000;"> av_frame_alloc();<br/><br/> numBytes </span>= avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height);<br/> out_buffer </span>= (uint8_t *) av_malloc(numBytes * <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(uint8_t));<br/> avpicture_fill((AVPicture </span>*<span style="color: #000000;">) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,<br/> pCodecCtx</span>-&gt;width, pCodecCtx-&gt;<span style="color: #000000;">height);<br/><br/> </span><span style="color: #0000ff;">int</span> y_size = pCodecCtx-&gt;width * pCodecCtx-&gt;<span style="color: #000000;">height;<br/><br/> packet </span>= (AVPacket *) malloc(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(AVPacket));<br/> av_new_packet(packet, y_size);<br/><br/> av_dump_format(pFormatCtx, </span><span style="color: #800080;">0</span>, filePath, <span style="color: #800080;">0</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">while</span> (av_read_frame(pFormatCtx, packet) &gt;= <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (packet-&gt;stream_index ==<span style="color: #000000;"> videoStream) {<br/> ret </span>= avcodec_decode_video2(pCodecCtx, pFrame, &amp;<span style="color: #000000;">got_picture,<br/> packet);<br/><br/> </span><span style="color: #0000ff;">if</span> (ret &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">decode error.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (got_picture) {<br/> img_convert_ctx </span>= sws_getContext(pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;height, pCodecCtx-&gt;pix_fmt, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height, AV_PIX_FMT_YUV420P,<br/> SWS_BICUBIC, NULL, NULL, NULL);<br/> sws_scale(img_convert_ctx,<br/> (</span><span style="color: #0000ff;">const</span> uint8_t* <span style="color: #0000ff;">const</span> *) pFrame-&gt;data, pFrame-&gt;<span style="color: #000000;">linesize,<br/> </span><span style="color: #800080;">0</span>, pCodecCtx-&gt;height, pFrameYUV-&gt;<span style="color: #000000;">data,<br/> pFrameYUV</span>-&gt;<span style="color: #000000;">linesize);<br/> sws_freeContext(img_convert_ctx);<br/><br/> </span><span style="color: #0000ff;">if</span> (i &lt; <span style="color: #800080;">20</span><span style="color: #000000;">) {<br/> SaveFrame(pFrameYUV, pCodecCtx</span>-&gt;width, pCodecCtx-&gt;height, ++<span style="color: #000000;">i);<br/> }<br/> }<br/> }<br/> av_free_packet(packet);<br/> }<br/><br/> av_free(out_buffer);<br/> av_free(pFrameYUV);<br/> avcodec_close(pCodecCtx);<br/> avformat_close_input(</span>&amp;<span style="color: #000000;">pFormatCtx);<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>  十二、将解码的数据使用SDL输出展现, 首先添加SDL库支持</p><p>    &nbsp;Project-&gt;Properties-&gt;C/C++ General-&gt;Path and Symbols-&gt;Libraries-&gt;Add-&gt;SDL</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008000;">/*</span><span style="color: #008000;"><br/> ============================================================================<br/> Name : VideoDecodeTutorial.c<br/> Author : clarck<br/> Version :<br/> Copyright : Your copyright notice<br/> Description : Hello World in C, Ansi-style<br/> ============================================================================<br/> </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br/><br/>#include </span>&lt;stdio.h&gt;<span style="color: #000000;"><br/>#include </span>&lt;stdlib.h&gt;<span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">SDL/SDL.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">SDL/SDL_thread.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavcodec/avcodec.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavformat/avformat.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libavutil/pixfmt.h</span><span style="color: #800000;">"</span><span style="color: #000000;"><br/>#include </span><span style="color: #800000;">"</span><span style="color: #800000;">libswscale/swscale.h</span><span style="color: #800000;">"</span><br/><br/><span style="color: #0000ff;">void</span> SaveFrame(AVFrame *pFrame, <span style="color: #0000ff;">int</span> width, <span style="color: #0000ff;">int</span> height, <span style="color: #0000ff;">int</span><span style="color: #000000;"> i) {<br/> FILE </span>*<span style="color: #000000;"> pFile;<br/> </span><span style="color: #0000ff;">char</span> szFilename[<span style="color: #800080;">32</span><span style="color: #000000;">];<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> y;<br/><br/> sprintf(szFilename, </span><span style="color: #800000;">"</span><span style="color: #800000;">frame%d.ppm</span><span style="color: #800000;">"</span><span style="color: #000000;">, i);<br/><br/> pFile </span>= fopen(szFilename, <span style="color: #800000;">"</span><span style="color: #800000;">wb</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">if</span> (pFile ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">pFile is null</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br/> }<br/><br/> fprintf(pFile, </span><span style="color: #800000;">"</span><span style="color: #800000;">P6\n%d %d\n255\n</span><span style="color: #800000;">"</span><span style="color: #000000;">, width, height);<br/><br/> </span><span style="color: #0000ff;">for</span> (y = <span style="color: #800080;">0</span>; y &lt; height; y++<span style="color: #000000;">) {<br/> fwrite(pFrame</span>-&gt;data[<span style="color: #800080;">0</span>] + y * pFrame-&gt;linesize[<span style="color: #800080;">0</span>], <span style="color: #800080;">1</span>, width * <span style="color: #800080;">3</span><span style="color: #000000;">, pFile);<br/> }<br/><br/> fclose(pFile);<br/>}<br/><br/></span><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>*<span style="color: #000000;"> args[]) {<br/> AVFormatContext </span>*<span style="color: #000000;">pFormatCtx;<br/> AVCodecContext </span>*<span style="color: #000000;">pCodecCtx;<br/> AVCodec </span>*<span style="color: #000000;">pCodec;<br/> AVFrame </span>*pFrame, *<span style="color: #000000;">pFrameYUV;<br/> AVPacket </span>*<span style="color: #000000;">packet;<br/> uint8_t </span>*<span style="color: #000000;">out_buffer;<br/><br/> </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">struct</span> SwsContext *<span style="color: #000000;">img_convert_ctx;<br/><br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> videoStream, i, numBytes;<br/> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ret, got_picture;<br/><br/> </span><span style="color: #0000ff;">char</span> filePath[] = <span style="color: #800000;">"</span><span style="color: #800000;">./src/a.mp4</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> av_register_all();<br/> pFormatCtx </span>=<span style="color: #000000;"> avformat_alloc_context();<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_open_input(&amp;pFormatCtx, filePath, NULL, NULL) != <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">can't open the file. \n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avformat_find_stream_info(pFormatCtx, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could't find stream infomation.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> videoStream </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i &lt; pFormatCtx-&gt;nb_streams; i++<span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type ==<span style="color: #000000;"> AVMEDIA_TYPE_VIDEO) {<br/> videoStream </span>=<span style="color: #000000;"> i;<br/> }<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (videoStream == -<span style="color: #800080;">1</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Didn't find a video stream.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pCodecCtx </span>= pFormatCtx-&gt;streams[videoStream]-&gt;<span style="color: #000000;">codec;<br/> pCodec </span>= avcodec_find_decoder(pCodecCtx-&gt;<span style="color: #000000;">codec_id);<br/><br/> </span><span style="color: #0000ff;">if</span> (pCodec ==<span style="color: #000000;"> NULL) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Codec not found.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span> (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not open codec.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> pFrame </span>=<span style="color: #000000;"> av_frame_alloc();<br/> pFrameYUV </span>=<span style="color: #000000;"> av_frame_alloc();<br/><br/> numBytes </span>= avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height);<br/> out_buffer </span>= (uint8_t *) av_malloc(numBytes * <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(uint8_t));<br/> avpicture_fill((AVPicture </span>*<span style="color: #000000;">) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,<br/> pCodecCtx</span>-&gt;width, pCodecCtx-&gt;<span style="color: #000000;">height);<br/><br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">==============SDL==================</span><span style="color: #008000;">//<br/></span> <span style="color: #0000ff;">if</span> (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO |<span style="color: #000000;"> SDL_INIT_TIMER)) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not initialize SDL - %s \n</span><span style="color: #800000;">"</span><span style="color: #000000;">, SDL_GetError());<br/> exit(</span><span style="color: #800080;">1</span><span style="color: #000000;">);<br/> }<br/><br/> SDL_Surface </span>*<span style="color: #000000;">screen;<br/> screen </span>= SDL_SetVideoMode(pCodecCtx-&gt;width, pCodecCtx-&gt;height, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">screen) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">SDL:could not set video mode - exiting\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> exit(</span><span style="color: #800080;">1</span><span style="color: #000000;">);<br/> }<br/><br/> SDL_Overlay </span>*<span style="color: #000000;">bmp;<br/> bmp </span>= SDL_CreateYUVOverlay(pCodecCtx-&gt;width, pCodecCtx-&gt;<span style="color: #000000;">height,<br/> SDL_YV12_OVERLAY, screen);<br/> SDL_Rect rect;<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">===================================</span><span style="color: #008000;">//<br/></span><br/> <span style="color: #0000ff;">int</span> y_size = pCodecCtx-&gt;width * pCodecCtx-&gt;<span style="color: #000000;">height;<br/><br/> packet </span>= (AVPacket *) malloc(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(AVPacket));<br/> av_new_packet(packet, y_size);<br/><br/> av_dump_format(pFormatCtx, </span><span style="color: #800080;">0</span>, filePath, <span style="color: #800080;">0</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #0000ff;">while</span> (av_read_frame(pFormatCtx, packet) &gt;= <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> </span><span style="color: #0000ff;">if</span> (packet-&gt;stream_index ==<span style="color: #000000;"> videoStream) {<br/> ret </span>= avcodec_decode_video2(pCodecCtx, pFrame, &amp;<span style="color: #000000;">got_picture,<br/> packet);<br/><br/> </span><span style="color: #0000ff;">if</span> (ret &lt; <span style="color: #800080;">0</span><span style="color: #000000;">) {<br/> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">decode error.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_FAILURE;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (got_picture) {<br/> SDL_LockYUVOverlay(bmp);<br/> pFrameYUV</span>-&gt;data[<span style="color: #800080;">0</span>] = bmp-&gt;pixels[<span style="color: #800080;">0</span><span style="color: #000000;">];<br/> pFrameYUV</span>-&gt;data[<span style="color: #800080;">1</span>] = bmp-&gt;pixels[<span style="color: #800080;">2</span><span style="color: #000000;">];<br/> pFrameYUV</span>-&gt;data[<span style="color: #800080;">2</span>] = bmp-&gt;pixels[<span style="color: #800080;">1</span><span style="color: #000000;">];<br/> pFrameYUV</span>-&gt;linesize[<span style="color: #800080;">0</span>] = bmp-&gt;pitches[<span style="color: #800080;">0</span><span style="color: #000000;">];<br/> pFrameYUV</span>-&gt;linesize[<span style="color: #800080;">1</span>] = bmp-&gt;pitches[<span style="color: #800080;">2</span><span style="color: #000000;">];<br/> pFrameYUV</span>-&gt;linesize[<span style="color: #800080;">2</span>] = bmp-&gt;pitches[<span style="color: #800080;">1</span><span style="color: #000000;">];<br/><br/> img_convert_ctx </span>= sws_getContext(pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;height, pCodecCtx-&gt;pix_fmt, pCodecCtx-&gt;<span style="color: #000000;">width,<br/> pCodecCtx</span>-&gt;<span style="color: #000000;">height, AV_PIX_FMT_YUV420P,<br/> SWS_BICUBIC, NULL, NULL, NULL);<br/> sws_scale(img_convert_ctx,<br/> (</span><span style="color: #0000ff;">const</span> uint8_t* <span style="color: #0000ff;">const</span> *) pFrame-&gt;data, pFrame-&gt;<span style="color: #000000;">linesize,<br/> </span><span style="color: #800080;">0</span>, pCodecCtx-&gt;height, pFrameYUV-&gt;<span style="color: #000000;">data,<br/> pFrameYUV</span>-&gt;<span style="color: #000000;">linesize);<br/> sws_freeContext(img_convert_ctx);<br/><br/> SDL_UnlockYUVOverlay(bmp);<br/> rect.x </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;<br/> rect.y </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;<br/> rect.w </span>= pCodecCtx-&gt;<span style="color: #000000;">width;<br/> rect.h </span>= pCodecCtx-&gt;<span style="color: #000000;">height;<br/> SDL_DisplayYUVOverlay(bmp, </span>&amp;<span style="color: #000000;">rect);<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">delay 400ms</span><br/> SDL_Delay(<span style="color: #800080;">400</span><span style="color: #000000;">);<br/> }<br/> }<br/> av_free_packet(packet);<br/> }<br/><br/> av_free(out_buffer);<br/> av_free(pFrameYUV);<br/> avcodec_close(pCodecCtx);<br/> avformat_close_input(</span>&amp;<span style="color: #000000;">pFormatCtx);<br/><br/> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;<br/>}</span></div><p>&nbsp;</p><p>   </p><p>    </p><img src="http://counter.cnblogs.com/blog/rss/3879208" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/tanlon/p/3879208.html" target="_blank">[原]零基础学习视频解码之解码图像</a>,转载请注明。</p>http://www.cnblogs.com/david1989/p/3879201.html慎用System.Web.HttpContext.Current - wongdavid每当控制流离开页面派生的Web表单上的代码的时候,HttpContext类的静态属性Current可能是有用的。 使用这个属性,我们可以获取当前请求(Request),响应(Response),会话(Session,)和应用程序对象(Application objects)以及请求更多服务。...2014-07-30T13:43:00Z2014-07-30T13:43:00Zwongdavidhttp://www.cnblogs.com/david1989/<p align="left">  </p><p align="left">&nbsp;</p><p align="left">  每当控制流离开页面派生的Web表单上的代码的时候,HttpContext类的静态属性Current可能是有用的。 使用这个属性,我们可以获取当前请求(Request),响应(Response),会话(Session,)和应用程序对象(Application objects)以及请求更多服务。 以下面的代码为例。</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Page_Load(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e)<br/>{<br/> MyClass myClass </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass();<br/> myClass.DoFoo();<br/>}<br/><br/></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass<br/>{<br/> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> DoFoo()<br/> {<br/> HttpContext.Current.Response.Write(</span><span style="color: #800000;">"</span><span style="color: #800000;">Doing Foo</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> }<br/>}</span></div><p>  Context在同一个应用程序域中请求当前上下文的能力是强大的,但也可能被滥用。你可以从业务对象使用HttpContext.Current打破你的架构层的界限,并且很轻松地将类与ASP.NET结合,而Windows Forms和the Compact Framework PDA则无法应用于此场景。</p><p align="left">  HttpContext.Current是如何找到上下文当前请求。 此外,它总是能找到当前请求? 例如,下面的代码的行为会是什么?</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Page_Load(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e)<br/>{<br/> ThreadPool.QueueUserWorkItem(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> WaitCallback(DoWork));<br/>}<br/><br/></span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork(<span style="color: #0000ff;">object</span><span style="color: #000000;"> state)<br/>{<br/> HttpContext context </span>=<span style="color: #000000;"> HttpContext.Current;<br/> context.Response.Write(</span><span style="color: #800000;">"</span><span style="color: #800000;">Do Work</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/>}</span></div><p>&nbsp;</p><p align="left">  答:上面的代码会生成一个System.NullReferenceException,因为HttpContext.Current返回null。从设计的角度来看,上面的代码至少存在两个问题,但是让我们讨论HttpContext.Current工作之前是如何工作的。</p><p align="left">  快速查看反编译器实现Current的属性看起来像下面这样。</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> HttpContext get_Current()<br/>{ <br/> </span><span style="color: #0000ff;">return</span> (CallContext.GetData(<span style="color: #800000;">"</span><span style="color: #800000;">HtCt</span><span style="color: #800000;">"</span>) <span style="color: #0000ff;">as</span><span style="color: #000000;"> HttpContext); <br/>}</span></div><p align="left">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; CallContext是类似于方法调用的线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽。CallContext 为调用路径提供数据槽。CallContext.SetData 和 CallContext.GetData 可用于管理应用程序代码中的调用上下文槽。每一调用路径都有唯一的数据槽;也就是说,调用路径之间不共享状态。这些数据槽是命名过的,名称用于访问数据槽,使用该名称可以显式地释放数据槽。线程本地存储是一个概念,其中在一个应用程序域中的每个逻辑线程都有一个唯一的数据槽,以保持特定于自身的数据。 线程不共享数据,一个线程不能修改本地数据到另一个线程中。ASP.NET中,选择一个线程来执行传入的请求后,在本地的线程存储参考当前请求的上下文。现在,无论线程在哪执行(一个业务对象,数据访问对象),上下文时时存在方便检索。</p><p align="left">&nbsp;</p><p align="left">  知道了上面我们可以声明如下:如果在处理请求时,执行移动到不同的线程(通过QueueUserWorkItem,或异步委托,作为两个例子),当前背景下HttpContext.Current将不知道如何检索,将返回null。 你可能会认为解决这个问题的一种方法是将引用传递给工作线程,就像下面的例子。</p><p align="left">&nbsp;</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Page_Load(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e) <br/>{ <br/> WorkerClass2 worker </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> WorkerClass2(); <br/> ThreadPool.QueueUserWorkItem(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> WaitCallback(worker.DoWork), HttpContext.Current); <br/>} <br/></span><span style="color: #808080;">///</span><span style="color: #008000;">&hellip;&hellip;&hellip;</span><br/><span style="color: #0000ff;">class</span><span style="color: #000000;"> WorkerClass2 <br/>{ <br/> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork(<span style="color: #0000ff;">object</span><span style="color: #000000;"> state) <br/> { <br/> HttpContext context </span>= state <span style="color: #0000ff;">as</span><span style="color: #000000;"> HttpContext; <br/> Thread.Sleep(</span><span style="color: #800080;">15000</span><span style="color: #000000;">); <br/> context.Response.Write(</span><span style="color: #800000;">"</span><span style="color: #800000;">Request.Url = </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> context.Request.Url); <br/> } <br/>}</span></div><p align="left">  </p><p align="left">  然而,在我的环境中,上面的代码还抛出一个异常,虽然此异常来源于mscoree类库。 无论是上面的代码,并与QueueUserWorkItem第一个例子从另一个缺陷患:两者都可以活得比的页面请求的生命周期,也分配给该页面请求的HttpContext对象的有效寿命。 虽然我们可以保持一个参考的任何要求,以防止垃圾回收器把它的HttpContext,ASP.NET运行时肯定是自由的,只要页面请求处理完成清除一些资源。 我不相信有反正说会与上面的代码会发生什么,在不同的情况下,代码可以实际工作的一些机器,但失败的几率确实存在,并应避免的条件。</p><p align="left">  有一些方法可以保证页面请求没有完成,直到工作线程完成其工作,例如,下面的代码。</p><p align="left">&nbsp;</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Page_Load(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e)<br/>{<br/> WorkerClass worker </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> WorkerClass(_resetEvent);<br/> ThreadPool.QueueUserWorkItem(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> WaitCallback(worker.DoWork),<br/> HttpContext.Current);<br/> </span><span style="color: #0000ff;">try</span><span style="color: #000000;"><br/> {<br/> _resetEvent.WaitOne();<br/> }<br/> </span><span style="color: #0000ff;">finally</span><span style="color: #000000;"><br/> {<br/> _resetEvent.Close();<br/> }<br/>}<br/>AutoResetEvent _resetEvent </span>= <span style="color: #0000ff;">new</span> AutoResetEvent(<span style="color: #0000ff;">false</span><span style="color: #000000;">);<br/>&hellip;<br/></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> WorkerClass<br/>{<br/> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> WorkerClass(AutoResetEvent resetEvent)<br/> { <br/> _resetEvent </span>=<span style="color: #000000;"> resetEvent;<br/> }<br/><br/> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork(<span style="color: #0000ff;">object</span><span style="color: #000000;"> state)<br/> {<br/> </span><span style="color: #0000ff;">try</span><span style="color: #000000;"><br/> {<br/> HttpContext context </span>= state <span style="color: #0000ff;">as</span><span style="color: #000000;"> HttpContext;<br/> Thread.Sleep(</span><span style="color: #800080;">500</span><span style="color: #000000;">);<br/> context.Response.Write(</span><span style="color: #800000;">"</span><span style="color: #800000;">Do work</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/> }<br/> </span><span style="color: #0000ff;">finally</span><span style="color: #000000;"><br/> {<br/> _resetEvent.Set();<br/> }<br/> }<br/><br/> AutoResetEvent _resetEvent </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;<br/>}</span></div><p align="left">  </p><p align="left">  上面的代码能正常的工作在浏览器中。 然而,设计仍然存在一些疑虑。 首先,ASP.NET运行时处理多个请求的时候,它的线程使用数量有限。 我们刚刚完成相同数量的工作,但我??们已经增加了一倍所需的线程数,产生额外的上下文切换,和现在有一个同步原语来管理。 当等待工作任务完成,在原始线程执行额外的工作,则可能是一个好处。 然而,一般来说,你更应该使用额外的线程在ASP.NET中使用一定量的保留的做法。</p><p align="left">&nbsp;</p><p align="left">  在这篇文章中,我们已经深入了解HttpContext.Current是如何工作的,并看到了一些场景,我们需要谨慎行事。 不要滥用HttpContext.Current,每当调用在代码调用它的时候,应多检查您的设计架构。</p><p>&nbsp;</p><p>&nbsp;</p><p align="left">&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3879201" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/david1989/p/3879201.html" target="_blank">慎用System.Web.HttpContext.Current</a>,转载请注明。</p>http://www.cnblogs.com/liangning/p/3879177.html浅谈linux内核栈(基于3.16-rc4) - Mr.Sting在3.16-rc4内核源码中,内核给每个进程分配的内核栈大小为8KB。这个内核栈被称为异常栈,在进程的内核空间运行时或者执行异常处理程序时,使用的都是异常栈,看下异常栈的代码(include/linux/sched.h):1 union thread_union {2 struct thre...2014-07-30T13:32:00Z2014-07-30T13:32:00ZMr.Stinghttp://www.cnblogs.com/liangning/<p>在3.16-rc4内核源码中,内核给每个进程分配的内核栈大小为8KB。这个内核栈被称为异常栈,在进程的内核空间运行时或者执行异常处理程序时,使用的都是异常栈,看下异常栈的代码(include/linux/sched.h):</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008080;">1</span> <span style="color: #000000;">union thread_union {<br/></span><span style="color: #008080;">2</span> <span style="color: #0000ff;">struct</span><span style="color: #000000;"> thread_info thread_info;<br/></span><span style="color: #008080;">3</span> unsigned <span style="color: #0000ff;">long</span> stack[THREAD_SIZE/<span style="color: #0000ff;">sizeof</span>(<span style="color: #0000ff;">long</span><span style="color: #000000;">)];<br/></span><span style="color: #008080;">4</span> };</div><p>THREAD_SIZE值为8KB,因此内核为进程的异常栈(内核栈)分配了两个页框大小(页框大小4KB)。另外,进程的thread_info结构体保存在栈顶部。</p><p>此外,内核为每个cpu分配一个硬中断栈和一个软中断栈(这两个栈也是内核栈),用来执行中断服务例程和下半部(软中断),看看代码(arch/x86/kernel/irq_32.c)。这两个栈属于cpu,不属于进程,这和异常栈是有区别的。</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008080;">1</span> DEFINE_PER_CPU(<span style="color: #0000ff;">struct</span> irq_stack *<span style="color: #000000;">, hardirq_stack);<br/></span><span style="color: #008080;">2</span> DEFINE_PER_CPU(<span style="color: #0000ff;">struct</span> irq_stack *, softirq_stack);</div><p>定义了两个数组hardirq_stack和softirq_stack,每个数组元素对应一个cpu,指向了该cpu的硬中断栈或者软中断栈。再来看下<span style="color: #0000ff;">struct irq_stack</span>结构体(arch/x86/include/asm/processor.h):</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008080;">1</span> <span style="color: #0000ff;">struct</span><span style="color: #000000;"> irq_stack {<br/></span><span style="color: #008080;">2</span> u32 stack[THREAD_SIZE/<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(u32)];<br/></span><span style="color: #008080;">3</span> } __aligned(THREAD_SIZE);</div><p>可见,硬中断栈和软中断栈的大小均为8KB。</p><p>内核在执行中断处理程序时,在do_IRQ函数中会调用handle_irq函数,在handle_irq函数中要进行堆栈切换,代码如下(arch/x86/kernel/irq_32.c):</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">bool</span> handle_irq(unsigned irq, <span style="color: #0000ff;">struct</span> pt_regs *<span style="color: #000000;">regs)<br/></span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{<br/></span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">struct</span> irq_desc *<span style="color: #000000;">desc;<br/></span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> overflow;<br/></span><span style="color: #008080;"> 5</span> <br/><span style="color: #008080;"> 6</span> overflow =<span style="color: #000000;"> check_stack_overflow();<br/></span><span style="color: #008080;"> 7</span> <br/><span style="color: #008080;"> 8</span> desc =<span style="color: #000000;"> irq_to_desc(irq);<br/></span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">if</span> (unlikely(!<span style="color: #000000;">desc))<br/></span><span style="color: #008080;">10</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;<br/></span><span style="color: #008080;">11</span> <br/><span style="color: #008080;">12</span> <span style="color: #0000ff;">if</span> (user_mode_vm(regs) || !<span style="color: #000000;">execute_on_irq_stack(overflow, desc, irq)) {<br/></span><span style="color: #008080;">13</span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (unlikely(overflow))<br/></span><span style="color: #008080;">14</span> <span style="color: #000000;"> print_stack_overflow();<br/></span><span style="color: #008080;">15</span> desc-&gt;<span style="color: #000000;">handle_irq(irq, desc);<br/></span><span style="color: #008080;">16</span> <span style="color: #000000;"> }<br/></span><span style="color: #008080;">17</span> <br/><span style="color: #008080;">18</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;<br/></span><span style="color: #008080;">19</span> }</div><p>第12行中执行<span style="color: #000000;">execute_on_irq_stack函数来判断是否需要堆栈切换,如果不需要,则执行if体,即在当前堆栈中执行中断服务例程,如果需要切换堆栈,则不执行if体。下面看下<span style="color: #000000;">execute_on_irq_stack</span>代码(arch/x86/kernel/irq_32.c):</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">static</span> inline <span style="color: #0000ff;">int</span><br/><span style="color: #008080;"> 2</span> execute_on_irq_stack(<span style="color: #0000ff;">int</span> overflow, <span style="color: #0000ff;">struct</span> irq_desc *desc, <span style="color: #0000ff;">int</span><span style="color: #000000;"> irq)<br/></span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{<br/></span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">struct</span> irq_stack *curstk, *<span style="color: #000000;">irqstk;<br/></span><span style="color: #008080;"> 5</span> u32 *isp, *<span style="color: #000000;">prev_esp, arg1, arg2;<br/></span><span style="color: #008080;"> 6</span> <br/><span style="color: #008080;"> 7</span> curstk = (<span style="color: #0000ff;">struct</span> irq_stack *<span style="color: #000000;">) current_stack();<br/></span><span style="color: #008080;"> 8</span> irqstk =<span style="color: #000000;"> __this_cpu_read(hardirq_stack);<br/></span><span style="color: #008080;"> 9</span> <br/><span style="color: #008080;">10</span> <span style="color: #008000;">/*</span><br/><span style="color: #008080;">11</span> <span style="color: #008000;"> * this is where we switch to the IRQ stack. However, if we are<br/></span><span style="color: #008080;">12</span> <span style="color: #008000;"> * already using the IRQ stack (because we interrupted a hardirq<br/></span><span style="color: #008080;">13</span> <span style="color: #008000;"> * handler) we can't do that and just have to keep using the<br/></span><span style="color: #008080;">14</span> <span style="color: #008000;"> * current stack (which is the irq stack already after all)<br/></span><span style="color: #008080;">15</span> <span style="color: #008000;">*/</span><br/><span style="color: #008080;">16</span> <span style="color: #0000ff;">if</span> (unlikely(curstk ==<span style="color: #000000;"> irqstk))<br/></span><span style="color: #008080;">17</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;<br/></span><span style="color: #008080;">18</span> <br/><span style="color: #008080;">19</span> isp = (u32 *) ((<span style="color: #0000ff;">char</span> *)irqstk + <span style="color: #0000ff;">sizeof</span>(*<span style="color: #000000;">irqstk));<br/></span><span style="color: #008080;">20</span> <br/><span style="color: #008080;">21</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> Save the next esp at the bottom of the stack </span><span style="color: #008000;">*/</span><br/><span style="color: #008080;">22</span> prev_esp = (u32 *<span style="color: #000000;">)irqstk;<br/></span><span style="color: #008080;">23</span> *prev_esp =<span style="color: #000000;"> current_stack_pointer;<br/></span><span style="color: #008080;">24</span> <br/><span style="color: #008080;">25</span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (unlikely(overflow))<br/></span><span style="color: #008080;">26</span> <span style="color: #000000;"> call_on_stack(print_stack_overflow, isp);<br/></span><span style="color: #008080;">27</span> <br/><span style="color: #008080;">28</span> asm <span style="color: #0000ff;">volatile</span>(<span style="color: #800000;">"</span><span style="color: #800000;">xchgl %%ebx,%%esp \n</span><span style="color: #800000;">"</span><br/><span style="color: #008080;">29</span> <span style="color: #800000;">"</span><span style="color: #800000;">call *%%edi \n</span><span style="color: #800000;">"</span><br/><span style="color: #008080;">30</span> <span style="color: #800000;">"</span><span style="color: #800000;">movl %%ebx,%%esp \n</span><span style="color: #800000;">"</span><br/><span style="color: #008080;">31</span> : <span style="color: #800000;">"</span><span style="color: #800000;">=a</span><span style="color: #800000;">"</span> (arg1), <span style="color: #800000;">"</span><span style="color: #800000;">=d</span><span style="color: #800000;">"</span> (arg2), <span style="color: #800000;">"</span><span style="color: #800000;">=b</span><span style="color: #800000;">"</span><span style="color: #000000;"> (isp)<br/></span><span style="color: #008080;">32</span> : <span style="color: #800000;">"</span><span style="color: #800000;">0</span><span style="color: #800000;">"</span> (irq), <span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span> (desc), <span style="color: #800000;">"</span><span style="color: #800000;">2</span><span style="color: #800000;">"</span><span style="color: #000000;"> (isp),<br/></span><span style="color: #008080;">33</span> <span style="color: #800000;">"</span><span style="color: #800000;">D</span><span style="color: #800000;">"</span> (desc-&gt;<span style="color: #000000;">handle_irq)<br/></span><span style="color: #008080;">34</span> : <span style="color: #800000;">"</span><span style="color: #800000;">memory</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">cc</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">ecx</span><span style="color: #800000;">"</span><span style="color: #000000;">);<br/></span><span style="color: #008080;">35</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">1</span><span style="color: #000000;">;<br/></span><span style="color: #008080;">36</span> }</div><p>第7行获取当前堆栈的指针,第8行获取本地cpu的硬中断栈指针,第16行对二者进行比较,如果相等,则不需要切换堆栈(说明当前堆栈就是硬中断栈,也说明是在中断处理程序中时又发生了中断)。如果不相等,就要进行堆栈切换,第22-23行将当前堆栈指针保存在将要切换到的堆栈中(用于返回)。第28-34行,在内联汇编中进行堆栈切换并执行相应的中断服务例程,第33行将中断服务例程函数名存放在%edi中,第29行跳转到中断服务例程中。</p><p>&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3879177" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/liangning/p/3879177.html" target="_blank">浅谈linux内核栈(基于3.16-rc4)</a>,转载请注明。</p>http://www.cnblogs.com/similar/p/3878850.html自己编写的轻量级Tabs切换插件 - similarjquery ui 带有Tabs切换插件,但其css样式太难维护,引用的东西太多,因此就自己写了个。起初我Html代码架子是这样的: 标签1 标签2 标签3 内容11111...2014-07-30T13:09:00Z2014-07-30T13:09:00Zsimilarhttp://www.cnblogs.com/similar/<p>jquery ui 带有Tabs切换插件,但其css样式太难维护,引用的东西太多,因此就自己写了个。</p><p>起初我Html代码架子是这样的:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="tabs"</span><span style="color: #0000ff;">&gt;</span> <br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="acss"</span><span style="color: #ff0000;"> data-box</span><span style="color: #0000ff;">="#panel-1"</span><span style="color: #0000ff;">&gt;</span>标签1<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> data-box</span><span style="color: #0000ff;">="#panel-2"</span><span style="color: #0000ff;">&gt;</span>标签2<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> data-box</span><span style="color: #0000ff;">="#panel-3"</span><span style="color: #0000ff;">&gt;</span>标签3<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-1"</span><span style="color: #0000ff;">&gt;</span>内容111111<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-2"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容222222<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-3"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容333333<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span></div><p>后来换成了下面这个:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dl </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="tabs"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dt</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="acss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-1"</span><span style="color: #0000ff;">&gt;</span>标签1<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-2"</span><span style="color: #0000ff;">&gt;</span>标签2<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-3"</span><span style="color: #0000ff;">&gt;</span>标签3<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dt</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-1"</span><span style="color: #0000ff;">&gt;</span>内容1<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-2"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容2<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-3"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容3<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dl</span><span style="color: #0000ff;">&gt;</span></div><p>之所以换成这个,是因为我觉得 dl dt dd 在页面布局中用的比 div ul li 要少,这样可以做到更好的隔离性。我们用js操作dl dt dd 对象,就会更少的影响到页面内其它元素</p><p>还有就是不用在li标签中自定义data-box属性,更符合页面书写标准。而且这个结构的整体感觉也比上面那个好。</p><p>&nbsp;</p><p>插件的实现代码如下:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">(<span style="color: #0000ff;">function</span><span style="color: #000000;"> ($) {<br/> $.fn.Tabs </span>= <span style="color: #0000ff;">function</span><span style="color: #000000;"> (options) {<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">默认参数设置</span><br/> <span style="color: #0000ff;">var</span> settings =<span style="color: #000000;"> {<br/> beforeCss: </span>"bcss", <span style="color: #008000;">//</span><span style="color: #008000;">激活前样式名</span><br/> afterCss: "acss", <span style="color: #008000;">//</span><span style="color: #008000;">激活后样式名</span><br/> model: "mouseover" <span style="color: #008000;">//</span><span style="color: #008000;">切换方式("mouseover"或者"click")</span><br/><span style="color: #000000;"> };<br/><br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">不为空,则合并参数</span><br/> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (options)<br/> $.extend(settings, options);<br/><br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">获取a标签集合</span><br/> <span style="color: #0000ff;">var</span> arr_a = $("&gt; dt &gt; a", <span style="color: #0000ff;">this</span><span style="color: #000000;">);<br/><br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">给a标签分别绑定事件</span><br/> arr_a.each(<span style="color: #0000ff;">function</span><span style="color: #000000;"> () {<br/> $(</span><span style="color: #0000ff;">this</span>).bind(settings.model, <span style="color: #0000ff;">function</span><span style="color: #000000;"> () {<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">样式控制</span><br/> $(<span style="color: #0000ff;">this</span><span style="color: #000000;">).removeClass().addClass(settings.afterCss)<br/> .siblings(</span>"a"<span style="color: #000000;">).removeClass().addClass(settings.beforeCss);<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">隐藏与显示控制</span><br/> <span style="color: #0000ff;">var</span> dd_id = $(<span style="color: #0000ff;">this</span>).attr("href"<span style="color: #000000;">);<br/> $(dd_id).show().siblings(</span>"dd"<span style="color: #000000;">).hide();<br/> });<br/> });<br/><br/> </span><span style="color: #008000;">//</span><span style="color: #008000;">遵循链式原则</span><br/> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.each(<span style="color: #0000ff;">function</span><span style="color: #000000;"> () { });<br/> };<br/>})(jQuery);</span></div><p>之所以说是轻量级,是因为代码量真的很少,也很简单。加了注释相信大家都可以看懂。当然,如果你有更简单的,那就分享吧&gt;-&lt;</p><p>&nbsp;</p><p>里面settings中的model是用来控制切换方式的:</p><p>当为"click"时,点击实现切换;</p><p>当为"mouseover"时,鼠标滑入实现切换。</p><p>&nbsp;</p><p>开始时是想用hover来实现鼠标滑入切换的,结果发现,hover不支持bind绑定。因为hover是jquery通过封装 mouseover事件 的产物,它并不是一个</p><p>正宗的事件,因此无法绑定。</p><p>&nbsp;</p><p>下面给出个DEMO:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">&lt;!</span><span style="color: #ff00ff;">DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">html </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://www.w3.org/1999/xhtml"</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">style </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/css"</span><span style="color: #0000ff;">&gt;</span><span style="background-color: #f5f5f5; color: #800000;"><br/> *<br/> </span><span style="background-color: #f5f5f5; color: #000000;">{</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 0</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> padding</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 0</span><span style="background-color: #f5f5f5; color: #000000;">;</span><br/> <span style="background-color: #f5f5f5; color: #000000;">}</span><span style="background-color: #f5f5f5; color: #800000;"> <br/> .tabs<br/> </span><span style="background-color: #f5f5f5; color: #000000;">{</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 504px</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 50px auto</span><span style="background-color: #f5f5f5; color: #000000;">;</span><br/> <span style="background-color: #f5f5f5; color: #000000;">}</span><span style="background-color: #f5f5f5; color: #800000;"><br/> <br/> .acss,.bcss<br/> </span><span style="background-color: #f5f5f5; color: #000000;">{</span><span style="background-color: #f5f5f5; color: #ff0000;"> <br/> text-decoration</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">none</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> line-height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 35px</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> font-size</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 14px</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> padding</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">8px 15px</span><span style="background-color: #f5f5f5; color: #000000;">;</span> <br/> <span style="background-color: #f5f5f5; color: #000000;">}</span><span style="background-color: #f5f5f5; color: #800000;"><br/> <br/> .bcss<br/> </span><span style="background-color: #f5f5f5; color: #000000;">{</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> background-color</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> #D4D4D4</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> border-bottom</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">1px solid white</span><span style="background-color: #f5f5f5; color: #000000;">;</span><br/> <span style="background-color: #f5f5f5; color: #000000;">}</span><span style="background-color: #f5f5f5; color: #800000;"><br/> .acss<br/> </span><span style="background-color: #f5f5f5; color: #000000;">{</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> background-color</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> orange</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> border-bottom</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">1px solid orange</span><span style="background-color: #f5f5f5; color: #000000;">;</span><br/> <span style="background-color: #f5f5f5; color: #000000;">}</span><span style="background-color: #f5f5f5; color: #800000;"> <br/> .tabs dd<br/> </span><span style="background-color: #f5f5f5; color: #000000;">{</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 500px</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 300px</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> border</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 1px solid orange</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> text-align</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> center</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;"><br/> line-height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 300px</span><span style="background-color: #f5f5f5; color: #000000;">;</span><br/> <span style="background-color: #f5f5f5; color: #000000;">}</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">style</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span><br/> <br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dl </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="tabs"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="tabs1"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dt</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="acss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-1"</span><span style="color: #0000ff;">&gt;</span>标签1<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-2"</span><span style="color: #0000ff;">&gt;</span>标签2<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-3"</span><span style="color: #0000ff;">&gt;</span>标签3<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dt</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-1"</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">&gt;</span>鼠标滑入切换<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-2"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容2<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-3"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容3<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dl</span><span style="color: #0000ff;">&gt;</span><br/><br/><br/> <br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dl </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="tabs"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="tabs2"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dt</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="acss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-4"</span><span style="color: #0000ff;">&gt;</span>标签1<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">默认第一个激活</span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-5"</span><span style="color: #0000ff;">&gt;</span>标签2<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bcss"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="#panel-6"</span><span style="color: #0000ff;">&gt;</span>标签3<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dt</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-4"</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">&gt;</span>鼠标点击切换<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">默认第一个显示</span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-5"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容2<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dd </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="panel-6"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="display:none;"</span><span style="color: #0000ff;">&gt;</span>内容3<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dd</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dl</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="../js/jquery-1.4.1.min.js"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">script</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="../js/jquery.similar.Tabs.js"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">script</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">&gt;</span><span style="background-color: #f5f5f5; color: #000000;"><br/> $(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">#tabs1</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).Tabs(); </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">默认鼠标滑入切换</span><br/><span style="background-color: #f5f5f5; color: #000000;"> $(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">#tabs2</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).Tabs({model:</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">click</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">}); </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">设置为点击切换</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">script</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span></div><p>&nbsp;</p><p>效果图片:</p><p><img src="http://images.cnitblog.com/i/464634/201407/302100049462354.jpg" alt="" /></p><p>&nbsp;</p><p>不足之处求指出,求指点,谢谢!</p><p>&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3878850" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/similar/p/3878850.html" target="_blank">自己编写的轻量级Tabs切换插件</a>,转载请注明。</p>http://www.cnblogs.com/WhyEngine/p/3879047.html数学图形之花儿 - 叶飞影前几天,我曾经发布过关于如何生成花形曲线的文章,参见数学图形(1.11) 玫瑰线数学图形(1.27) 花这一节中,会将二维的花形曲线变成三维的花形曲面,其样子会漂亮很多.相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.(1)玫瑰线vertices = D1:4000 D2:...2014-07-30T12:24:00Z2014-07-30T12:24:00Z叶飞影http://www.cnblogs.com/WhyEngine/<p><span style="font-size: 16px;">前几天,我曾经发布过关于如何生成花形曲线的文章,参见</span></p><p><span style="font-size: 16px;"><a id="CategoryEntryList1_EntryStoryList_ctl00_Entries_TitleUrl_60" class="entrylistItemTitle" href="http://www.cnblogs.com/WhyEngine/p/3824376.html">数学图形(1.11) 玫瑰线</a></span></p><p><span style="font-size: 16px;"><a id="CategoryEntryList1_EntryStoryList_ctl00_Entries_TitleUrl_44" class="entrylistItemTitle" href="http://www.cnblogs.com/WhyEngine/p/3831289.html">数学图形(1.27) 花</a></span></p><p><span style="font-size: 16px;">这一节中,会将二维的花形曲线变成三维的花形曲面,其样子会漂亮很多.</span></p><p><span style="font-size: 16px;">相关软件参见:<a id="CategoryEntryList1_EntryStoryList_ctl00_Entries_TitleUrl_1" class="entrylistItemTitle" href="http://www.cnblogs.com/WhyEngine/p/3854938.html">数学图形可视化工具</a>,使用自己定义语法的脚本代码生成数学图形.</span></p><p>&nbsp;</p><p><span style="font-size: 16px;">(1)玫瑰线</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">vertices = D1:<span style="color: #800080;">4000</span> D2:<span style="color: #800080;">6</span><span style="color: #000000;"><br/><br/>n </span>= <span style="color: #800080;">8</span><span style="color: #000000;"><br/><br/>u </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to (n*<span style="color: #000000;">PI) D1<br/>v </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to <span style="color: #800080;">1</span><span style="color: #000000;"> D2<br/><br/>a </span>= rand_int2(<span style="color: #800080;">2</span>, <span style="color: #800080;">16</span><span style="color: #000000;">)<br/>r </span>= <span style="color: #800080;">10</span>*sin(a*u)*<span style="color: #000000;">v<br/>k </span>= u/n/<span style="color: #800080;">2</span><span style="color: #000000;"><br/><br/>x </span>= r*cos(u)*<span style="color: #000000;">cos(k)<br/>z </span>= r*sin(u)*<span style="color: #000000;">cos(k)<br/>y </span>= GOLD* abs(r)*sin(k)</div><p><img src="http://images.cnitblog.com/i/571803/201407/302020381807113.png" alt="" /></p><p><span style="font-size: 16px;">(2)双层玫瑰</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">vertices = D1:<span style="color: #800080;">4000</span> D2:<span style="color: #800080;">6</span><span style="color: #000000;"><br/><br/>n </span>= rand_int2(<span style="color: #800080;">3</span>, <span style="color: #800080;">16</span><span style="color: #000000;">)<br/><br/>u </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to (n*<span style="color: #000000;">PI) D1<br/>v </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to <span style="color: #800080;">1</span><span style="color: #000000;"> D2<br/><br/>r </span>= <span style="color: #800080;">10</span>*(<span style="color: #800080;">1</span> + <span style="color: #800080;">3</span>*sin(n*u))*<span style="color: #000000;">v<br/>k </span>= u/n/<span style="color: #800080;">2</span><span style="color: #000000;"><br/><br/>x </span>= r*cos(u)*<span style="color: #000000;">cos(k)<br/>z </span>= r*sin(u)*<span style="color: #000000;">cos(k)<br/>y </span>= GOLD* abs(r)*sin(k)</div><p><img src="http://images.cnitblog.com/i/571803/201407/302020537125201.png" alt="" /></p><p><span style="font-size: 16px;">(3)超级玫瑰</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">#http:<span style="color: #008000;">//</span><span style="color: #008000;">www.2dcurves.com/roulette/rouletters.html</span><br/><span style="color: #000000;"><br/>vertices </span>= D1:<span style="color: #800080;">8000</span> D2:<span style="color: #800080;">6</span><span style="color: #000000;"><br/><br/>n </span>= <span style="color: #800080;">16</span><span style="color: #000000;"><br/><br/>u </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to (n*<span style="color: #000000;">PI) D1<br/>v </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to <span style="color: #800080;">1</span><span style="color: #000000;"> D2<br/><br/>a </span>= rand_int2(<span style="color: #800080;">1</span>, <span style="color: #800080;">100</span>) / <span style="color: #800080;">8</span><span style="color: #000000;"><br/>b </span>= rand_int2(<span style="color: #800080;">1</span>, <span style="color: #800080;">100</span>) / <span style="color: #800080;">8</span><span style="color: #000000;"><br/>c </span>= rand_int2(-<span style="color: #800080;">10</span>, <span style="color: #800080;">100</span>) / <span style="color: #800080;">8</span><span style="color: #000000;"><br/>d </span>= rand_int2(<span style="color: #800080;">1</span>, <span style="color: #800080;">100</span>) / <span style="color: #800080;">8</span><span style="color: #000000;"><br/>f </span>= rand_int2(<span style="color: #800080;">1</span>, <span style="color: #800080;">100</span>) / <span style="color: #800080;">8</span><span style="color: #000000;"><br/><br/>w </span>= pow(abs(cos(d*u)), a) + pow(abs(sin(d*<span style="color: #000000;">u)), b)<br/>r </span>= <span style="color: #800080;">10</span>*v*sin(f*u)*<span style="color: #000000;">pow(w, c)<br/>w </span>= u/n/<span style="color: #800080;">2</span><span style="color: #000000;"><br/><br/>x </span>= r*sin(u)*<span style="color: #000000;">cos(w)<br/>z </span>= r*cos(u)*<span style="color: #000000;">cos(w)<br/>y </span>= GOLD*abs(r)*sin(w)</div><p><img src="http://images.cnitblog.com/i/571803/201407/302021123054988.png" alt="" /></p><p><span style="font-size: 16px;">(4)N叶草</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">#http:<span style="color: #008000;">//</span><span style="color: #008000;">www.mathcurve.com/courbes2d/biquartic/biquartic.shtml</span><br/><span style="color: #000000;"><br/>vertices </span>= D1:<span style="color: #800080;">4000</span> D2:<span style="color: #800080;">6</span><span style="color: #000000;"><br/><br/>m </span>= <span style="color: #800080;">8</span><span style="color: #000000;"><br/><br/>u </span>= <span style="color: #0000ff;">from</span> (-PI) to (m*<span style="color: #000000;">PI) D1<br/>v </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to <span style="color: #800080;">1</span><span style="color: #000000;"> D2<br/><br/>n </span>= rand_int2(<span style="color: #800080;">3</span>, <span style="color: #800080;">10</span><span style="color: #000000;">)<br/>p </span>= (<span style="color: #800080;">1</span> + cos(n*u) + sin(n*u)^<span style="color: #800080;">2</span>) *<span style="color: #000000;"> v<br/>k </span>= u/m/<span style="color: #800080;">2</span><span style="color: #000000;"><br/><br/>x </span>= p*cos(u)*<span style="color: #000000;">cos(k)<br/>z </span>= p*sin(u)*<span style="color: #000000;">cos(k)<br/>y </span>= abs(p)*sin(k)</div><p><img src="http://images.cnitblog.com/i/571803/201407/302021333054372.png" alt="" /></p><p><span style="font-size: 16px;">(5)folioide</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">vertices = D1:<span style="color: #800080;">6</span> D2:<span style="color: #800080;">4000</span><span style="color: #000000;"><br/><br/>m </span>= <span style="color: #800080;">32</span><span style="color: #000000;"><br/><br/>u </span>= <span style="color: #0000ff;">from</span> (-PI/<span style="color: #800080;">2</span>) to (m*<span style="color: #000000;">PI) D2<br/>v </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to <span style="color: #800080;">1</span><span style="color: #000000;"> D1<br/><br/>e </span>= rand2(<span style="color: #800080;">0.1</span>, <span style="color: #800080;">10</span><span style="color: #000000;">)<br/>a </span>= <span style="color: #800080;">10</span> /<span style="color: #000000;"> e<br/>i </span>= rand_int2(<span style="color: #800080;">2</span>, <span style="color: #800080;">10</span><span style="color: #000000;">)<br/>j </span>= rand_int2(<span style="color: #800080;">1</span>, <span style="color: #800080;">10</span><span style="color: #000000;">)<br/>n </span>= i/<span style="color: #000000;">j<br/><br/>p </span>= a*(e*cos(n*u) + sign(u)*e*sqrt(<span style="color: #800080;">1</span> - pow(cos(n*u), <span style="color: #800080;">2</span>)))*<span style="color: #000000;">v<br/>w </span>= u/m/<span style="color: #800080;">2</span><span style="color: #000000;"><br/><br/>x </span>= p*cos(u)*<span style="color: #000000;">cos(w)<br/>z </span>= p*sin(u)*<span style="color: #000000;">cos(w)<br/>y </span>= GOLD * abs(p)*sin(w)</div><p><img src="http://images.cnitblog.com/i/571803/201407/302021529309743.png" alt="" /></p><p><img src="http://images.cnitblog.com/i/571803/201407/302021593688409.png" alt="" /></p><p><span style="font-size: 16px;">(6)botanic</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">#http:<span style="color: #008000;">//</span><span style="color: #008000;">www.2dcurves.com/roulette/rouletteb.html</span><br/><span style="color: #000000;"><br/>vertices </span>= D1:<span style="color: #800080;">8000</span> D2:<span style="color: #800080;">6</span><span style="color: #000000;"><br/><br/><br/>n </span>= <span style="color: #800080;">32</span><span style="color: #000000;"><br/><br/>u </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to (n*<span style="color: #000000;">PI) D1<br/>v </span>= <span style="color: #0000ff;">from</span> <span style="color: #800080;">0</span> to <span style="color: #800080;">1</span><span style="color: #000000;"> D2<br/><br/>r </span>= <span style="color: #800080;">10</span>*<span style="color: #000000;">v<br/><br/>c </span>= rand2(<span style="color: #800080;">0.1</span>, <span style="color: #800080;">10</span><span style="color: #000000;">)<br/>d </span>= rand2(<span style="color: #800080;">1</span>, <span style="color: #800080;">10</span><span style="color: #000000;">)<br/><br/>p </span>= r*(<span style="color: #800080;">1</span> + d*sin(c*<span style="color: #000000;">u))<br/>w </span>= u/n/<span style="color: #800080;">2</span><span style="color: #000000;"><br/><br/>x </span>= p*cos(u)*<span style="color: #000000;">cos(w)<br/>z </span>= p*sin(u)*<span style="color: #000000;">cos(w)<br/>y </span>= <span style="color: #800080;">0.6</span>*abs(p)*sin(w)</div><p><img src="http://images.cnitblog.com/i/571803/201407/302022148992199.png" alt="" /></p><p><img src="http://images.cnitblog.com/i/571803/201407/302022245878805.png" alt="" /></p><p><img src="http://images.cnitblog.com/i/571803/201407/302022343053870.png" alt="" /></p><p><img src="http://images.cnitblog.com/i/571803/201407/302022505879328.png" alt="" /></p><p><img src="http://images.cnitblog.com/i/571803/201407/302023027277220.png" alt="" /></p><p><img src="http://images.cnitblog.com/i/571803/201407/302023111188555.png" alt="" /></p><p><img src="http://images.cnitblog.com/i/571803/201407/302023203999921.png" alt="" /></p><p>&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3879047" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/WhyEngine/p/3879047.html" target="_blank">数学图形之花儿</a>,转载请注明。</p>http://www.cnblogs.com/opencascade/p/3879015.htmlOpenCascade Draw Test Harness - opencascadeAbstract. Draw is a command interpreter based on Tcl/Tk and a graphical system used to test and demonstrate OpenCascade modeling libraries. It provide...2014-07-30T12:04:00Z2014-07-30T12:04:00Zopencascadehttp://www.cnblogs.com/opencascade/<h4 align="center"><span style="font-size: 18pt;">OpenCascade Draw Test Harness</span></strong></p><p align="center"><a href="mailto:eryar@163.com">eryar@163.com</a></p><p><span style="font-size: 12pt;"><strong>Abstract</strong>. Draw is a command interpreter based on Tcl/Tk and a graphical system used to test and demonstrate OpenCascade modeling libraries. It provides a flexible and easy to use means of testing and demonstrating the OCCT modeling libraries. By a given command of Tcl, you can find the implentation code for that command by OpenCascade, the code could be as a demo to use of OCC modeling libraries. So this is the most effectively way of use OpenCascade.&nbsp; </span></p><p><span style="font-size: 12pt;"><strong>Key words.</strong> OpenCascade, Tcl/Tk, Testing, Scripts </span></p><p><span style="font-size: 14pt;"><strong>1. Introduction </strong> </span></p><p>Draw Test Harness提供了灵活和易于使用的方式来测试、演示OpenCascade的建模算法。可用来交互的创建、显示和修改曲线、曲面及拓朴形状。你也可以自己写Tcl脚本来自定义Draw或使用脚本来实现自动化测试。也可以为Draw添加新的自定义的命令。&nbsp;</p><p>因为Draw是基于Tcl/Tk脚本的,所以可以根据OpenCascade自定义的命令名称来找到相关的实现,这个实现应该是使用OpenCascade最有效的方式,也是官方使用OpenCascade的方式,具有很好的参考价值。&nbsp;</p><p>还可在Draw 中使用Tcl脚本来实现概念建模,如果结果和预期一致,可再把Tcl命令相对应的C++代码写到程序中,提高编程效率。&nbsp;</p><p>本文主要介绍如何根据OpenCascade中实现的命令找到相关的实现代码,作为编程时的参考,避免了用C++代码来实现一些功能时自己来摸索,从而浪费不必要的时间。&nbsp;</p><p><span style="font-size: 14pt;"><strong>2. Using Tcl/Tk </strong> </span></p><p>先给出Draw中使用脚本出来的效果的例子,再介绍找出命令实现代码的方法。如下图所示为用脚本在Draw中产生的效果:&nbsp;</p><p><a href="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-14743_2.png"><img style="margin: 0px; border: 0px currentColor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="wps_clip_image-14743" src="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-14743_thumb.png" alt="wps_clip_image-14743" width="425" height="447" border="0" /></a></p><p>Figure 2.1 A blend test in Draw Test Harness&nbsp;</p><p>其实现的脚本如下所示:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #000000;">pload ALL<br/><br/>vinit<br/>vgrid<br/><br/>vsetgradientbg </span><span style="color: #800000;">0</span> <span style="color: #800000;">0</span> <span style="color: #800000;">180</span> <span style="color: #800000;">255</span> <span style="color: #800000;">255</span> <span style="color: #800000;">255</span> <span style="color: #800000;">2</span><span style="color: #000000;"><br/><br/>vzbufftrihedron<br/><br/></span><span style="color: #008000;">#</span><span style="color: #008000;"> run a test</span><br/>source tests/blend/<span style="color: #000000;">begin<br/>source tests</span>/blend/buildevol/<span style="color: #000000;">a2<br/><br/>vsetdispmode </span><span style="color: #800000;">1</span><span style="color: #000000;"><br/>vdisplay result<br/>vfit</span></div><p><a href="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-23795_2.png"><img style="margin: 0px; border: 0px currentColor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="wps_clip_image-23795" src="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-23795_thumb.png" alt="wps_clip_image-23795" width="554" height="376" border="0" /></a></p><p>Figure 2.2 Tcl script&nbsp;</p><p>将上述脚本代码保存到samples/tcl文件夹下,文件名为init.tcl,再启动Draw,输入命令:&nbsp;</p><p><a href="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-30471_2.png"><img style="margin: 0px; border: 0px currentColor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="wps_clip_image-30471" src="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-30471_thumb.png" alt="wps_clip_image-30471" width="541" height="187" border="0" /></a></p><p>Figure 2.3 Use the Tcl file&nbsp;</p><p><span style="font-size: 14pt;"><strong>3. Find code from Draw Test </strong> </span></p><p>因为Draw是基于Tcl/Tk实现的解释器,所以根据Tcl中自定义命令的方法,可以找出OpenCascade中这些命令的实现代码。更方便的做法是直接输入命令名称,再在所有的源代码中搜索相关文件即可。如上述脚本中的vgrid命令,是打开、关闭栅格,可以直接搜索vgrid,得到结果如下所示:&nbsp;</p><p><a href="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-22146_2.png"><img style="margin: 0px; border: 0px currentColor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="wps_clip_image-22146" src="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-22146_thumb.png" alt="wps_clip_image-22146" width="554" height="376" border="0" /></a></p><p>Figure 3.1 Find command in Files&nbsp;</p><p><a href="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-16479_2.png"><img style="margin: 0px; border: 0px currentColor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="wps_clip_image-16479" src="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-16479_thumb.png" alt="wps_clip_image-16479" width="549" height="372" border="0" /></a></p><p>Figure 3.2 Find Result of the Command&nbsp;</p><p>得到相关的代码使用方法如下图所示:&nbsp;</p><p><a href="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-30092_2.png"><img style="border: 0px currentColor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="wps_clip_image-30092" src="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/wps_clip_image-30092_thumb.png" alt="wps_clip_image-30092" width="553" height="435" border="0" /></a></p><p>Figure 3.3 The Command function code&nbsp;</p><p>依此类推,其他Draw中的命令都可以找到相关的实现代码,这些代码都可作为编程的依据,有助于OpenCascade的正确、高效使用。&nbsp;</p><p><span style="font-size: 14pt;"><strong>4. Conclusion </strong> </span></p><p>从上可知,如果需要OpenCascade的什么功能,可以先在Draw Test Harness中试试看,如果在Draw Test Harness中可以实现,再找出其实现那个命令的代码,再参考代码实现所需要功能即可。这样编程使用OpenCascade应该是轻松和高效的,因为Tcl是解释执行,不像C++那样需要编译时间。&nbsp;</p><p><span style="font-size: 14pt;"><strong>5. References </strong> </span></p><p>1. Tcl and the Tk Toolkit&nbsp;</p><p>2. Practical Programming in Tcl and Tk&nbsp;</p><p>3. Tcl/Tk A Developer&rsquo;s Guide&nbsp;</p><p>4. OpenCascade Test Harness User&rsquo;s Guide&nbsp;&nbsp;</p><p>5. <a href="http://sourceforge.net/projects/tcl/">http://sourceforge.net/projects/tcl/</a></p><p>5. <a href="http://www.tcl.tk/">http://www.tcl.tk/</a></p><div id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:8215eb65-ea81-4e0b-94b2-e9bc7437fe2b" class="wlWriterEditableSmartContent" style="margin: 0px; padding: 0px; float: none; display: inline;"><p>PDF Version: <a href="http://www.cppblog.com/images/cppblog_com/eryar/Windows-Live-Writer/OpenCascade-Draw-Test-Harness_11922/OpenCascade%20Draw%20Test%20Harness.pdf" target="_blank">OpenCascade Draw Test Harness</a></p></div><img src="http://counter.cnblogs.com/blog/rss/3879015" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/opencascade/p/3879015.html" target="_blank">OpenCascade Draw Test Harness</a>,转载请注明。</p>http://www.cnblogs.com/f1194361820/p/3878855.html标准AOP与Spring AOP - 螺 丝 钉AOP介绍在AOP联盟的官方网站里(http://aopalliance.sourceforge.net/)AOP是一种能够增强多种已存在的中间件环境(such as J2EE)或者开发环境(e.g. Eclipse)功能的编程技术。AOP 实现方式与相关项目关于AOP在很多项目中都有实现,实现的方...2014-07-30T11:59:00Z2014-07-30T11:59:00Z螺 丝 钉http://www.cnblogs.com/f1194361820/<p><strong>AOP介绍</strong></p><p>在AOP联盟的官方网站里(http://aopalliance.sourceforge.net/)</p><p>AOP是一种能够增强多种已存在的中间件环境(such as J2EE)或者开发环境(e.g. Eclipse)功能的编程技术。</p><p>&nbsp;</p><p><strong>AOP </strong><strong>实现方式与相关项目</strong></p><p>关于AOP在很多项目中都有实现,实现的方式也有不同,目前主要有三种处理方式:Proxy(代理), Interceptor(拦截器), bytecode translator(字节码翻译)。</p><p>&nbsp;</p><p>AOP相关的项目有:</p><ul><li><a href="http://asm.objectweb.org/">ASM</a>: a lightweight bytecode translator.</li><li><a href="http://www.eclipse.org/aspectj/">AspectJ</a>: an AO source-level weaver. New Language.</li><li><a href="http://aspectwerkz.sourceforge.net/">AspectWerkz</a>: an AO framework (bytecode-level dynamic weaver+configuration).</li><li><a href="http://jakarta.apache.org/bcel/">BCEL</a>: a bytecode translator.</li><li><a href="http://cglib.sf.net/">CGLIB</a>: high-level API for class artifact manipulation and method interception.</li><li><a href="http://jac.aopsys.com/">JAC</a>: an AO middleware (bytecode-level dynamic weaver+configuration+aspects). Framework.</li><li><a href="http://www.csg.is.titech.ac.jp/~chiba/javassist/">Javassist</a>: a bytecode translator with a high-level API.</li><li><a href="http://www.jboss.org/index.html?module=html&amp;op=userdisplay&amp;id=developers/projects/jboss/aop">JBoss-AOP</a>: interception and metadata-based AO framework.</li><li><a href="http://javalab.cs.uni-bonn.de/research/jmangler/">JMangler</a>: a bytecode translator with a composition framework for translations.</li><li><a href="http://nanning.sf.net/">Nanning</a>: an AO weaver (framework).</li><li><a href="http://prose.ethz.ch/">Prose</a>: an AO bytecode-level dynamic weaver (framework).</li><li>... and many others (<a href="mailto:renaud@aopsys.com">email me</a>&nbsp;to add a new one)</li></ul><p>&nbsp;</p><p>而在.Net平台下, PostSharp也是一个有名的AOP框架,它采用的方式与AspectJ类似,AspectJ是通过字节码织入方式,PostSharp是通过修改编译后的文件方式。</p><p>&nbsp;</p><p><strong>AOP分层架构</strong></p><p><img src="http://images.cnitblog.com/i/512650/201407/301953536184149.x-png" alt="" /></p><p>&nbsp;</p><p>AOP分层架构中,从概念到实现,分为3个层次:</p><p>level 1: 是一种AOP框架的底层处理,是处理Aspect与Target的织入过程,处理方式有:反射、元数据处理、拦截器、代理等等。也就是说,上面提到的几种方式都可以用于</p><p>aspect织入到Target上。</p><p>level 2: 是AO(面向切面)系统的实现过程。可以在编译期,也可以在运行期。</p><p>level 3: 是最高层,这一层中,把面向切面的编程看作是:base + aspect + config。这一层是开发人员直接使用的。</p><p>&nbsp;&nbsp;&nbsp; base: 就是target,就是待增加的目标对象。</p><p>&nbsp;</p><p><strong>AOP要用到的技术说明</strong></p><p>AOP并不是一门新的技术,而是在已有技术上实现的新功能。</p><p>在Level1层:</p><p>&nbsp;&nbsp;&nbsp; &middot;反射:没有反射就没有各种框架,现在的AOP框架已有很多,上面已列出常用的。这些框架的底层,都使用到了反射技术。</p><p>&nbsp;&nbsp;&nbsp; &middot;拦截器框架:拦截器框架也已有很多,Struts2中有,不是采用动态代理实现的。Java提供了一种标准的拦截器框架,是使用动态代理技术实现的。</p><p>&nbsp;&nbsp;&nbsp; &middot;元数据处理:一般来说会和拦截器一起使用,允许扩展类(例如实现新接口)。</p><p>&nbsp;&nbsp;&nbsp; &middot;类加载框架:或者通过拦截器框架织入、或者通过字节码织入,都是需要类加载框架的支持。</p><p>&nbsp;</p><p>&nbsp;</p><p><strong>AOP标准接口</strong></p><p>尽管各个AOP框架的实现有所不同,但又一些通用的东西。AOP联盟就把这些通用的东西制定成为AOP标准接口,在aopalliance.1.0.jar里。</p><p>&nbsp;</p><p>这个jar包中的制定的接口很少,但却很重要:</p><p><img src="http://images.cnitblog.com/i/512650/201407/301955017436169.x-png" alt="" /></p><p>&nbsp;</p><p>从之前的level 1的组件说明(技术说明)中,了解到:AOP在多处运用到了拦截器,因此AOP联盟也将拦截器封装进这个jar中。</p><p><img src="http://images.cnitblog.com/i/512650/201407/301955417902269.x-png" alt="" /></p><p>AOP的要义:在JoinPoint加入Advice。</p><p>&nbsp;</p><p>&nbsp;</p><p><span style="color: #ff0000; font-size: 16px;"><strong>Spring AOP:</strong></span></p><p>&nbsp;</p><p>&nbsp;</p><p>AOP 面向切面编程。AOP是Spring的两大核心之一,在Spring中,AOP的典型应用是事务管理。</p><p>&nbsp;</p><p>&nbsp;&nbsp;&nbsp; AOP中有很多概念需要了解:</p><p>看官方参考文档中的说明:</p><p>&nbsp;</p><ul><li><em>Aspect</em>: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented using regular classes (the <a title="8.3&nbsp;Schema-based AOP support" href="mk:@MSITStore:D:\xx\my_u_pan\chm\SpringFramework3.1-Reference_en.chm::/aop.html#aop-schema">schema-based approach</a>) or regular classes annotated with the <code>@Aspect</code> annotation (the <a title="8.2&nbsp;@AspectJ support" href="mk:@MSITStore:D:\xx\my_u_pan\chm\SpringFramework3.1-Reference_en.chm::/aop.html#aop-ataspectj"><code>@AspectJ</code> style</a>).</li></ul><p><em>Aspect</em><em>:切面,是AOP模块的核心,在Spring中,AOP以有规则的类或者注解完成。</em></p><p><em>&nbsp;</em></p><p><em>Join point</em>: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point <em>always</em> represents a method execution.</p><p>连接点,就是sayFoo,sayHello等实际要执行的方法。</p><ul><li><em>Advice</em>: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. (Advice types are discussed below.) Many AOP frameworks, including Spring, model an advice as an <em>interceptor</em>, maintaining a chain of interceptors <em>around</em> the join point.</li></ul><p>建议就是在sayFoo前后执行的方法,例如之前的befor(),after();</p><ul><li><em>Pointcut</em>: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.</li></ul><p>切点:就是指定在那些地方切入。切点是一个表达式,与这个表达式匹配的方法就是join point。例如表达式为:**.*.say*就是任意packget下任意class的sayXxx方法。方法名以say开头。这个表达式就是pointCut,匹配到的sayHello, sayFoo就是join Point。</p><ul><li><em>Introduction</em>: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an <code>IsModified</code> interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)</li></ul><p>说明,就可以让一个被advised对象(target)实现某个接口。添加额外功能。</p><ul><li><em>Target object</em>: object being advised by one or more aspects. Also referred to as the <em>advised</em> object. Since Spring AOP is implemented using runtime proxies, this object will always be a <em>proxied</em> object.</li></ul><p>target,就是要被advised的对象(),也就是说他是被代理的对象。它就相当于上面例子中的FooImpl,HelloImpl对象。</p><ul><li><em>AOP proxy</em>: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.</li></ul><p>他是根据AOP代理有两种:一种是Java的动态代理,一种是CgLib代理。</p><ul><li><em>Weaving</em>: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.</li></ul><p>织入:这是一个动词,在target(FooImpl)中与point cut (切点表达式, xxx.x.say**)匹配的join point(sayFoo) 的前后加入advice(before(), after(),并且可以另外附加实现<em>Introduction</em><em>(</em>新的接口),最终形成AOP Proxy(Dynamic Proxy, CGLIB Proxy)的过程,称为weaving(织入)。</p><img src="http://counter.cnblogs.com/blog/rss/3878855" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/f1194361820/p/3878855.html" target="_blank">标准AOP与Spring AOP</a>,转载请注明。</p>http://www.cnblogs.com/aaronguo/p/3878430.htmltempo 2.0 学习记录 - Aaronguo最近在做项目时使用了tempo,感觉还不错,但是发现网上对于tempo 2.0 的介绍比较少,我也是在GitHub才找到了比较完整的使用说明,我也简单记录一下自己的使用过程,重新学习一下tempo 2.0 ,不喜勿喷,喜欢看英文的朋友请移步tempo 2.0 英文说明 .1.引入tempo.js2....2014-07-30T11:22:00Z2014-07-30T11:22:00ZAaronguohttp://www.cnblogs.com/aaronguo/<p>最近在做项目时使用了tempo,感觉还不错,但是发现网上对于tempo 2.0 的介绍比较少,我也是在GitHub才找到了比较完整的使用说明,我也简单记录一下自己的使用过程,重新学习一下tempo 2.0 ,</p><p>不喜勿喷,喜欢看英文的朋友请移步<a href="http://www.cnblogs.com/aaronguo/p/3877692.html" target="_blank">tempo 2.0 英文说明</a> .</p><p>&nbsp;</p><p><span style="background-color: #ffff00;">1.引入tempo.js</span></p><p style="margin-left: 30px;">&nbsp;<span style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">&lt;script src="js/tempo.js" type="text/javascript"&gt;&lt;/script&gt;</span>&nbsp;</p><p style="text-align: left;">&nbsp;</p><p style="text-align: left;"><span style="color: #000000; background-color: #ffff00;">2.准备数据Data(标准的json数据)</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="width: 68.11%; height: 194px; margin-left: 30px;"><span style="color: #0000ff;">var</span> data =<span style="color: #000000;"> [<br/>{</span>'name':{'first':'Leonard','last':'Marx'},'nickname':'Chico','born':'March 21, 1887','actor': <span style="color: #0000ff;">true</span>,'solo_endeavours':[{'title':'Papa Romani'<span style="color: #000000;">}]},<br/>{</span>'name':{'first':'Adolph','last':'Marx'},'nickname':'Harpo','born':'November 23, 1888','actor':<span style="color: #0000ff;">true</span>,'solo_endeavours':[{'title':'Too Many Kisses',<br />'rating':'favourite'},{'title':'Stage Door Canteen'<span style="color: #000000;">}]},<br/>{</span>'name':{'first':'Julius Henry','last':'Marx'},'nickname':'Groucho','born': 'October 2, 1890','actor':<span style="color: #0000ff;">true</span>,'solo_endeavours':[{'title':'Copacabana'},<br />{'title':'Mr. Music','rating':'favourite'},{'title':'Double Dynamite'<span style="color: #000000;">}]},<br/>{</span>'name':{'first':'Milton','last':'Marx'},'nickname':'Gummo','born':'October 23, 1892'<span style="color: #000000;">},<br/>{</span>'name':{'first':'Herbert','last':'Marx'},'nickname':'Zeppo','born':'February 25, 1901','actor':<span style="color: #0000ff;">true</span>,'solo_endeavours':[{'title':'A Kiss in the Dark'<span style="color: #000000;">}]}<br/>];</span></div><p style="text-align: left;">&nbsp;&nbsp;</p><p style="text-align: left;"><span style="background-color: #ffff00;">3.Tempo.prepare().render()使用方式(需要找到承载数据的容器)</span></p><p style="text-align: left; margin-left: 30px;"><span style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">&lt;script language="javascript" type="text/javascript"&gt;<span style="color: #000000;"> $(</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){ Tempo.prepare(</span>'呈现数据的容器ID'<span style="color: #000000;">).render(数据源); }); </span>&lt;/script&gt;</span>&nbsp;</p><p>&nbsp;</p><p><span style="background-color: #ffff00;">4.data-template(html数据展示)</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="width: 68.24%; height: 584px; margin-left: 30px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">&gt;</span><span style="background-color: #f5f5f5; color: #000000;"><br/>$(</span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> () {<br/> </span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> data1 </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> { </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">leonard</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">: </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Leonard Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">adolph</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">: </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Adolph Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">julius</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">: </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Julius Henry Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">milton</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">: </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Milton Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">herbert</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">: </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Herbert Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;"> };<br/> </span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> data2 </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> [{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">name</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">first</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Leonard</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">last</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">},</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">nickname</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Chico</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">born</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">March 21, 1887</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">actor</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">: </span><span style="background-color: #f5f5f5; color: #0000ff;">true</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">solo_endeavours</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:[{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">title</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Papa Romani</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">}]},<br/> {</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">name</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">first</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Adolph</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">last</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">},</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">nickname</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Harpo</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">born</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">November 23, 1888</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">actor</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">true</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">solo_endeavours</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:[{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">title</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Too Many Kisses</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">rating</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">favourite</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">},{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">title</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Stage Door Canteen</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">}]},<br/> </span><span style="background-color: #f5f5f5; color: #000000;"> {</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">name</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">first</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Milton</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">last</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">},</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">nickname</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Gummo</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">born</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">October 23, 1892</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">},<br/> {</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">name</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">first</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Herbert</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">last</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Marx</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">},</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">nickname</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">Zeppo</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">born</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">February 25, 1901</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">actor</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">true</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">solo_endeavours</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:[{</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">title</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">A Kiss in the Dark</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">}]}];<br/><br/> Tempo.prepare(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">list1</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).render(data1);<br/> Tempo.prepare(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">list2</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).render(data2);<br/> <br/>});<br/></span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">script</span><span style="color: #0000ff;">&gt;</span><br/><br/><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">legend</span><span style="color: #0000ff;">&gt;</span>简单数据展示<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">legend</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ol </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="list1"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li </span><span style="color: #ff0000;">data-template data-from-map</span><span style="color: #0000ff;">&gt;</span>{{value}} - {{key | append '@marx.com'}}<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ol</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">legend</span><span style="color: #0000ff;">&gt;</span>嵌套数据展示<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">legend</span><span style="color: #0000ff;">&gt;</span> <br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ol </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="list2"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li </span><span style="color: #ff0000;">data-template</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br/> {{nickname}} {{name.last}}<br/> </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li </span><span style="color: #ff0000;">data-template-for</span><span style="color: #0000ff;">="solo_endeavours"</span><span style="color: #0000ff;">&gt;</span>{{title}}<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ol</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">&gt;</span></div><p><br /><span style="background-color: #ffff00;">5.对字段数据格式化</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="width: 68.16%; height: 632px; margin-left: 30px;"><span style="color: #000000;">{{ field | truncate 25[, 'optional_suffix'] }}<br/>截取字符串 使用方法:{{字段名|truncate 长度}}<br/><br/>{{ field | format[, numberOfDecimals] }}<br/>保留小数后的位数 使用方法:{{字段名|format 位数}}<br/><br/>{{ field | default 'default value' }}<br/>如果字段未定义和值为NULL时显示的默认值 使用方式:{{字段名| default '默认值'}}<br/><br/>{{ field | date 'preset or pattern like HH:mm, DD-MM-YYYY'[, 'UTC'] }}<br/>日期格式化 使用方式{{字段名 | date 'YYYY-MM-DD HH:mm:ss'}}<br/><br/>{{ field | trim }}<br/>清除开始和结尾的空格<br/><br/>{{ field | upper }}<br/>改变任何小写字符的值为大写。<br/><br/>{{ field | lower }}<br/>改变任何小写字符的值为小写。<br/><br/>{{ field | titlecase[, '需要过滤的字符串'] }}<br/>对标题进行过滤不显示的字符<br/><br/>{{ field | append '需要添加的字符串' }}<br/>如果字段非空,添加后缀和其它字符串<br/><br/>{{ field | prepend 'some prefix</span><span style="color: #ff0000;">&amp;nbsp;</span><span style="color: #000000;">' }}<br/>如果字段非空,添加前缀或者其它字符串<br/><br/>{{ field | join 'separator' }}<br/>如果此字段是一个数组,则往该数组里添加一个项</span></div><p style="text-align: left;"><br /><span style="background-color: #ffff00;">6.字段值转义</span> </p><p>  &nbsp;<span style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;">Tempo.prepare('marx-brothers', {'escape': false}).render(data);</span>&nbsp;</p><p>&nbsp;</p><p style="text-align: left;"><span style="background-color: #ffff00;">7.<a class="anchor" name="user-content-templatewhentype-handler" href="https://github.com/twigkit/tempo#templatewhentype-handler"></a>template.when(type, handler)</span></p><p style="margin-left: 30px;">Type 事件类型 的值&nbsp;</p><ul><li style="list-style-type: none;"><ul><li> TempoEvent.Types.RENDER_STARTING :模板替换开始&nbsp;</li><li> TempoEvent.Types.ITEM_RENDER_STARTING :数据项替换开始&nbsp;</li><li> TempoEvent.Types.ITEM_RENDER_COMPLETE : 数据项替换完成&nbsp;</li><li> TempoEvent.Types.RENDER_COMPLETE :模板替换完成&nbsp;</li><li> TempoEvent.Types.BEFORE_CLEAR : 在清理数据之前&nbsp;</li><li> TempoEvent.Types.AFTER_CLEAR : 在清理数据之后 ?</li></ul></li></ul><p style="margin-left: 30px;">handler (function(){})&nbsp;</p><p>&nbsp;</p><p style="text-align: left;"><span style="background-color: #ffff00;">8.加载数据时前后事件的处理,事件注册</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="width: 62.06%; height: 464px; margin-left: 30px;">1<span style="color: #000000;">.<br/>Tempo.prepare(</span>'tweets').when(TempoEvent.Types.RENDER_STARTING, <span style="color: #0000ff;">function</span><span style="color: #000000;"> (event) {<br/> $(</span>'#tweets').addClass('loading'<span style="color: #000000;">);<br/> }).when(TempoEvent.Types.RENDER_COMPLETE, </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (event) {<br/> $(</span>'#tweets').removeClass('loading'<span style="color: #000000;">);<br/> }).render(data);<br/><br/><br/></span>2<span style="color: #000000;">.template.starting()手动调用开始事件<br/></span><span style="color: #0000ff;">var</span> template = Tempo.prepare('tweets').when(TempoEvent.Types.RENDER_STARTING, <span style="color: #0000ff;">function</span><span style="color: #000000;"> (event) {<br/> $(</span>'#tweets').addClass('loading'<span style="color: #000000;">);<br/> }).when(TempoEvent.Types.RENDER_COMPLETE, </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (event) {<br/> $(</span>'#tweets').removeClass('loading'<span style="color: #000000;">);<br/> });<br/>template.starting();<br/>$.getJSON(url, </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(data) {<br/> template.render(data.results);<br/>});<br/><br/></span>3<span style="color: #000000;">. jQuery live() 事件<br/>$(</span>'ol li').live('click', <span style="color: #0000ff;">function</span><span style="color: #000000;">() {<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Do something with the clicked element</span><br/> alert(<span style="color: #0000ff;">this</span><span style="color: #000000;">);<br/>});</span></div><p style="text-align: left;"><br /><span style="background-color: #ffff00;">9.条件选择模板 if....else...</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="width: 67.73%; height: 50px; margin-left: 30px;">{% <span style="color: #0000ff;">if</span> javascript-expression %} ... {% <span style="color: #0000ff;">else</span> %} ... {% endif %} <span style="color: #008000;">//</span><span style="color: #008000;">{% else %} 为可选块</span></div><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="width: 67.73%; height: 176px; margin-left: 30px;">Data-<span style="color: #0000ff;">if</span>-字段名="值"<br/> &lt;li data-template data-<span style="color: #0000ff;">if</span>-sex="母"&gt;{{Name}}&lt;/a&gt;&lt;/li&gt;<span style="color: #000000;"><br/><br/>Data</span>-has="是否存在的字段"<br/> &lt;li data-template data-has="ifBianZhong"&gt;{{Name}}&lt;/a&gt;&lt;/li&gt;<span style="color: #000000;"><br/><br/>Data</span>-src="{{字段名|append '.png'}}"<br/> &lt;img src="1.gif" data-src="{{Image| append '.png'}}" /&gt;</div><p>&nbsp;</p><p>ok.大功告成...如果有理解不对的地方,还请朋友及时提出,此文为个人学习总结,英文较好的朋友可直接移步<a href="http://www.cnblogs.com/aaronguo/p/3877692.html" target="_blank">tempo 2.0 英文说明</a> .</p><p>&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3878430" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/aaronguo/p/3878430.html" target="_blank">tempo 2.0 学习记录</a>,转载请注明。</p>http://www.cnblogs.com/juepei/p/3873596.htmlthink in java 手记(一) - 薛定谔的猫_工作之余,不知道要做些什么。该做的事情都完成的差不多了,想看一下spring的东西,毕竟这些东西用的多。但是又想看一下关于javaee的东西,这个里面的设计模式多。想了一会儿,觉得这些无非都是工具,java才是基础。突然想起了大学时候,老师推荐的《think in java》,之前看了一遍,懵懵懂懂...2014-07-30T11:10:00Z2014-07-30T11:10:00Z薛定谔的猫_http://www.cnblogs.com/juepei/<p>工作之余,不知道要做些什么。该做的事情都完成的差不多了,想看一下spring的东西,毕竟这些东西用的多。但是又想看一下关于javaee的东西,这个里面的设计模式多。想了一会儿,觉得这些无非都是工具,java才是基础。突然想起了大学时候,老师推荐的《think in java》,之前看了一遍,懵懵懂懂,现在再看看,结合工作学习中的经验,或许能收获一些不同的东西。</p><p>(一)关于类和成员的访问权限</p><p>开篇写到了java类和成员的范围和访问权限,public, private ,protected, friendly。</p><p>前不久,在工作中,编写一个工具类,其中用到某个model的时候,无论如何,都会报错,提示此类不存在,建议建立一个新的类。当初以为编辑器除了问题,后来查看model类,才发现,原来类是这么定义的:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">class</span><span style="color: #000000;"> mymodel{<br/>....<br/>}</span></div><p>这是个默认friendly的类,和我的工具类不在一个包下,所以访问不到这个类的,由此报错。<br /><br /></p><p>(二)关于继承</p><blockquote><p>我们费尽心思做出一种数据类型后,假如不得不又新建一种类型,令其实现大致相同的功能,那会是一件非常令人灰心的事情。但若能利用现成的数据类型,对其进行&ldquo;克隆&rdquo;,再根据情况进行添加和修改,情况就显得理想多了。&ldquo;继承&rdquo;正是针对这个目标而设计的。但继承并不完全等价于克隆。</p><p>有两种做法可将新得的衍生类与原来的基础类区分开。第一种做法十分简单:为衍生类添加新函数(功能)。尽管extends关键字暗示着我们要为接口&ldquo;扩展&rdquo;新功能,但实情并非肯定如此。为区分我们的新类,第二个办法是改变基础类一个现有函数的行为。我们将其称作&ldquo;改善&rdquo;那个函数。</p></blockquote><p>(三)向上转型()</p><p>这个东西,曾经困惑了很久,主要是因为这个东西用的实在是太少了。</p><p>结合书中的代码,我这里也写了一段,关于形状(Shape)和圆形的(Circle)。</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.thinkinjava.unit1;<br/><br/></span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Shape {<br/> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> move(Shape shape){<br/> System.out.println(shape.getClass().getName()</span>+", move ..."<span style="color: #000000;">);<br/> }<br/> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> draw(Shape shape){<br/> System.out.println(shape.getClass().getName()</span>+", draw ...."<span style="color: #000000;">);<br/> }<br/> <br/> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doStuff(Shape shape){<br/> shape.move(shape);<br/> shape.draw(shape);<br/> }<br/> <br/>}</span></div><p>这里规定了,通用的形状所具有的方法。</p><p>写一个Circle继承我们刚刚写的Shape:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.thinkinjava.unit1;<br/><br/></span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Circle <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Shape {<br/> <br/>}</span></div><p>什么都没做,就是衍生了一个基础类(Shape)。</p><p>然后写一个main方法:</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.thinkinjava.unit1;<br/><br/></span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Main {<br/> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {<br/> Circle circle</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Circle();<br/> circle.doStuff(circle);<br/> }<br/>}</span></div><p>这样,实现上转型,即是书中说的 &ldquo;上溯造型&rdquo;。</p><p>这里的Circle circle=new Circle()可以写成Shape circle=new Circle(),一样的。但是后者很容易让人迷糊。</p><p>运行结果为:</p><p>com.thinkinjava.unit1.Circle, move ...<br />com.thinkinjava.unit1.Circle, draw ....</p><p>这个结构很鲜明,就是在方法中传递基础类类型,放入的参数类型,必须是继承自该基础类的。</p><p>说实话,这个技术的应用场景,至今也没有接触过。书中所说的,传递该基础类作为参数类型,是为了防止传入具体子类,要重新写更多的子类代码。</p><p>具体更加形象的应用场景,希望有明白的,能指教一下。</p><p>&nbsp;</p><img src="http://counter.cnblogs.com/blog/rss/3873596" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/juepei/p/3873596.html" target="_blank">think in java 手记(一)</a>,转载请注明。</p>http://www.cnblogs.com/sonic1abc/p/3868729.html如何将 Cortana 与 Windows Phone 8.1 应用集成 ( Voice command - Natural language recognition ) - 王博_Nick随着 Windows Phone 8.1 GDR1 + Cortana 中文版的发布,相信有很多用户或开发者都在调戏 Windows Phone 的语音私人助理 Cortana 吧,在世界杯的时候我亲测 Cortana 预测德国和阿根廷的比赛很准的。(题外话扯远了),可是作为开发者我们怎么将Cort...2014-07-30T09:49:00Z2014-07-30T09:49:00Z王博_Nickhttp://www.cnblogs.com/sonic1abc/<p>随着 Windows Phone 8.1 GDR1 + Cortana 中文版的发布,相信有很多用户或开发者都在调戏 Windows Phone 的语音私人助理 Cortana 吧,在世界杯的时候我亲测 Cortana 预测德国和阿根廷的比赛很准的。(题外话扯远了),可是作为开发者我们怎么将Cortana集成到应用中呢,今天我用一点时间给大家介绍一下如何使用 voice command 集成 Windows Phone 8.1 的应用。</p><p>首先要明确两个名词 <span style="background-color: #ffff00;">Voice command</span> &amp; <span style="background-color: #ffff00;">Voice Command Definition</span> 即 VCD文件,相信做过windows Phone 8.0 开发的朋友应该有所了解,通过注册VCD文件 Windows phone 8.0 的应用当中就可以实现 voice command 的功能,如果你不了解请先读一下我之前的文章(这里我就不在过多介绍 8.0 Voice command 的重复内容了),<a href="http://www.cnblogs.com/sonic1abc/archive/2012/11/18/2775153.html" target="_blank">Windows Phone 8 语音 - Speech for Windows Phone 8</a> 快速了解一下Windows Phone 开发语音功能的前期准备工作。</p><p>&nbsp;</p><p>简单的说在 Windows Phone 8.0 voice command 功能比较简单,主要是通过 Voice Command Name 判断预制在VCD文件中的几个命令。</p><p>在 Windows Phone 8.1 应用中 Cortana 提供了更强的自然语言识别(Natural language recognition)</p><p><a href="http://images.cnitblog.com/blog/33251/201407/251830207758010.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="Cortana1" src="http://images.cnitblog.com/blog/33251/201407/251830232135066.png" alt="Cortana1" width="929" height="397" border="0" /></a></p><p>当然 VCD 文件的中的 grammars&nbsp; 也得到了扩充,并且区别两个OS版本的</p><p><a href="http://schemas.microsoft.com/voicecommands/1.0">http://schemas.microsoft.com/voicecommands/1.0</a> for Windows Phone 8.0 Voice Command and Cortana compatible.</p><p><strong><a href="http://schemas.microsoft.com/voicecommands/1.1">http://schemas.microsoft.com/voicecommands/1.1</a>&nbsp;<em><span style="text-decoration: underline;">only for Widnows Phone 8.1 Cortnan.</span></em></strong></p><p><a href="http://images.cnitblog.com/blog/33251/201407/251830258225135.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="Cortana2" src="http://images.cnitblog.com/blog/33251/201407/251830306195803.png" alt="Cortana2" width="928" height="508" border="0" /></a></p><p>详细内容请参考</p><p><strong>Windows Phone 8.0:&nbsp; <a href="http://msdn.microsoft.com/library/windows/apps/jj207041(v=vs.105).aspx" target="_blank">Voice command element and attribute reference for Windows Phone 8</a></strong></p><p><strong>Windows Phone 8.1:&nbsp; <a href="http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn722331.aspx" target="_blank">Voice command elements and attributes</a></strong></p><p>通过 8.0 和 8.1 VCD 文件属性支持情况来看有一个最主要的区别在8.1 VCD中支持 <span style="background-color: #ffff00;">PhraseTopic</span> 这个属性。</p><p><a href="http://images.cnitblog.com/blog/33251/201407/251830324322515.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="image" src="http://images.cnitblog.com/blog/33251/201407/251830340105999.png" alt="image" width="859" height="592" border="0" /></a></p><p>文字说的太抽象了还是贴出代码给大家说说吧:</p><p>这里我主要强调说一<span style="color: #000000;">下 ListenFor&nbsp; 结点和 PhraseTopic 结点。 注意在 Listenfor 结点中的中括号 {dictatedSearchTerms} 是对应的 PhraseTopic&nbsp; 结点中的 Label 属性。同时我们可以把 PhraseTopic 理解成<span style="background-color: #ffff00;">任意内容</span>。最后都可以从Cortana回传到我们的应用当中来。</span></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">VoiceCommands </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/voicecommands/1.1"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> The CommandSet Name is used to programmatically access the CommandSet </span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">CommandSet </span><span style="color: #ff0000;">xml:lang</span><span style="color: #0000ff;">="zh-CN"</span><span style="color: #ff0000;"> Name</span><span style="color: #0000ff;">="chineseCommands"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> The CommandPrefix provides an alternative to your full app name for invocation </span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">CommandPrefix</span><span style="color: #0000ff;">&gt;</span> 微软 文档 <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">CommandPrefix</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> The CommandSet Example appears in the global help alongside your app name </span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Example</span><span style="color: #0000ff;">&gt;</span> 搜索 构造 函数 <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Example</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Command </span><span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="MSDNSearch"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> The Command example appears in the drill-down help page for your app </span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Example</span><span style="color: #0000ff;">&gt;</span> 搜索 构造 函数' <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Example</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> ListenFor elements provide ways to say the command, including references to <br/> {PhraseLists} and {PhraseTopics} as well as [optional] words </span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span> 查找 {dictatedSearchTerms} <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span> 搜 {dictatedSearchTerms} <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span> 搜索 {dictatedSearchTerms} <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span> 查 {dictatedSearchTerms} <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span> 找 {dictatedSearchTerms} <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">Feedback provides the displayed and spoken text when your command is triggered </span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Feedback</span><span style="color: #0000ff;">&gt;</span> 查找 MSDN... <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Feedback</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> Navigate specifies the desired page or invocation destination for the Command</span><span style="color: #008000;">--&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Navigate </span><span style="color: #ff0000;">Target</span><span style="color: #0000ff;">="MainPage.xaml"</span> <span style="color: #0000ff;">/&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Command</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Command </span><span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="MSDNNaturalLanguage"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Example</span><span style="color: #0000ff;">&gt;</span> 我 想 去 Windows 手机 开发 中心 <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Example</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span> {naturalLanguage} <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ListenFor</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Feedback</span><span style="color: #0000ff;">&gt;</span> 启动 MSDN... <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Feedback</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Navigate </span><span style="color: #ff0000;">Target</span><span style="color: #0000ff;">="MainPage.xaml"</span> <span style="color: #0000ff;">/&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Command</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">PhraseTopic </span><span style="color: #ff0000;">Label</span><span style="color: #0000ff;">="dictatedSearchTerms"</span><span style="color: #ff0000;"> Scenario</span><span style="color: #0000ff;">="Search"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Subject</span><span style="color: #0000ff;">&gt;</span> MSDN <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Subject</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">PhraseTopic</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">PhraseTopic </span><span style="color: #ff0000;">Label</span><span style="color: #0000ff;">="naturalLanguage"</span><span style="color: #ff0000;"> Scenario</span><span style="color: #0000ff;">="Natural Language"</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">Subject</span><span style="color: #0000ff;">&gt;</span> MSDN <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">Subject</span><span style="color: #0000ff;">&gt;</span><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">PhraseTopic</span><span style="color: #0000ff;">&gt;</span><br/><br/> <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">CommandSet</span><span style="color: #0000ff;">&gt;</span><br/><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">VoiceCommands</span><span style="color: #0000ff;">&gt;</span></div><p>&nbsp;</p><p>了解完新的VCD文件,在这里我提醒下大家,<span style="background-color: #ffff00;">其实在Windows Phone 8.0的应用中也可以兼容 Cortana的功能的</span>,在8.0的应用当中我们只需要判断一下操作系统的版本然后选择不同的VCD文件向系统注册即可。</p><p>首先我们需要把两个版本的VCD文件都存放到项目中来</p><p><a href="http://images.cnitblog.com/blog/33251/201407/251830354632457.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="image" src="http://images.cnitblog.com/blog/33251/201407/251830364328826.png" alt="image" width="389" height="438" border="0" /></a></p><p>其次在注册VCD文件的时候根据<span style="background-color: #ffff00;">系统版本</span>进行一下判断即可。</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><span style="color: #808080;"> ///</span> <span style="color: #808080;">&lt;summary&gt;</span><br/> <span style="color: #808080;">///</span><span style="color: #008000;"> Installs the Voice Command Definition (VCD) file associated with the application.<br/> </span><span style="color: #808080;">///</span><span style="color: #008000;"> Based on OS version, installs a separate document based on version 1.0 of the schema or version 1.1.<br/> </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span><br/> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">async</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> InstallVoiceCommands()<br/> {<br/> </span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> wp80vcdPath = <span style="color: #800000;">"</span><span style="color: #800000;">ms-appx:///VoiceCommandDefinition_8.0.xml</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> wp81vcdPath = <span style="color: #800000;">"</span><span style="color: #800000;">ms-appx:///VoiceCommandDefinition_8.1.xml</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> chineseWp80vcdPath = <span style="color: #800000;">"</span><span style="color: #800000;">ms-appx:///ChineseVoiceCommandDefinition_8.0.xml</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> chineseWp81vcdPath = <span style="color: #800000;">"</span><span style="color: #800000;">ms-appx:///ChineseVoiceCommandDefinition_8.1.xml</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br/><br/> </span><span style="color: #0000ff;">try</span><span style="color: #000000;"><br/> {<br/> </span><span style="color: #0000ff;">bool</span> using81orAbove = ((Environment.OSVersion.Version.Major &gt;= <span style="color: #800080;">8</span><span style="color: #000000;">)<br/> </span>&amp;&amp; (Environment.OSVersion.Version.Minor &gt;= <span style="color: #800080;">10</span><span style="color: #000000;">));<br/><br/> </span><span style="color: #0000ff;">string</span> vcdPath = using81orAbove ?<span style="color: #000000;"> wp81vcdPath : wp80vcdPath;<br/> </span><span style="color: #0000ff;">if</span> (InstalledSpeechRecognizers.Default.Language.Equals(<span style="color: #800000;">"</span><span style="color: #800000;">zh-CN</span><span style="color: #800000;">"</span><span style="color: #000000;">, StringComparison.InvariantCultureIgnoreCase))<br/> {<br/> vcdPath </span>= using81orAbove ?<span style="color: #000000;"> chineseWp81vcdPath : chineseWp80vcdPath;<br/> }<br/><br/> Uri vcdUri </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Uri(vcdPath);<br/> </span><span style="color: #0000ff;">await</span><span style="color: #000000;"> VoiceCommandService.InstallCommandSetsFromFileAsync(vcdUri);<br/> }<br/> </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception vcdEx)<br/> {<br/> Dispatcher.BeginInvoke(() </span>=&gt;<span style="color: #000000;"><br/> {<br/> MessageBox.Show(String.Format(<br/> AppResources.VoiceCommandInstallErrorTemplate, vcdEx.HResult, vcdEx.Message));<br/> });<br/> }<br/> }</span></div><p>最后在应用当中获取用户的语音输入方法,注意这里也是需要通过 <span style="background-color: #ffff00;">PhraseTopic</span> 结点的 <span style="background-color: #ffff00;">Label </span>名称获取的。</p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;" style="background-color: #f5f5f5; border: #cccccc 1px solid; padding: 5px;"><span style="color: #808080;"> ///</span> <span style="color: #808080;">&lt;summary&gt;</span><br/> <span style="color: #808080;">///</span><span style="color: #008000;"> Takes specific action for a retrieved VoiceCommand name.<br/> </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span><br/> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="voiceCommandName"&gt;</span><span style="color: #008000;"> the command name triggered to activate the application </span><span style="color: #808080;">&lt;/param&gt;</span><br/> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> HandleVoiceCommand(<span style="color: #0000ff;">string</span><span style="color: #000000;"> voiceCommandName)<br/> {<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Voice Commands can be typed into Cortana; when this happens, "voiceCommandMode" is populated with the<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> "textInput" value. In these cases, we'll want to behave a little differently by not speaking back.</span><br/> <span style="color: #0000ff;">bool</span> typedVoiceCommand = (NavigationContext.QueryString.ContainsKey(<span style="color: #800000;">"</span><span style="color: #800000;">commandMode</span><span style="color: #800000;">"</span><span style="color: #000000;">) <br/> </span>&amp;&amp; (NavigationContext.QueryString[<span style="color: #800000;">"</span><span style="color: #800000;">commandMode</span><span style="color: #800000;">"</span>] == <span style="color: #800000;">"</span><span style="color: #800000;">text</span><span style="color: #800000;">"</span><span style="color: #000000;">));<br/><br/> </span><span style="color: #0000ff;">string</span> phraseTopicContents = <span style="color: #0000ff;">null</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">bool</span> doSearch = <span style="color: #0000ff;">false</span><span style="color: #000000;">;<br/><br/> </span><span style="color: #0000ff;">switch</span><span style="color: #000000;"> (voiceCommandName)<br/> {<br/> </span><span style="color: #0000ff;">case</span> <span style="color: #800000;">"</span><span style="color: #800000;">MSDNNaturalLanguage</span><span style="color: #800000;">"</span><span style="color: #000000;">:<br/> </span><span style="color: #0000ff;">if</span> (NavigationContext.QueryString.TryGetValue(<span style="color: #800000;">"</span><span style="color: #800000;">naturalLanguage</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">out</span><span style="color: #000000;"> phraseTopicContents)<br/> </span>&amp;&amp; !<span style="color: #000000;">String.IsNullOrEmpty(phraseTopicContents))<br/> {<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> We'll try to process the input as a natural language query; if we're successful, we won't<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> fall back into searching, since the query will have already been handled.</span><br/> doSearch =<span style="color: #000000;"> TryHandleNlQuery(phraseTopicContents, typedVoiceCommand);<br/> }<br/> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">case</span> <span style="color: #800000;">"</span><span style="color: #800000;">MSDNSearch</span><span style="color: #800000;">"</span><span style="color: #000000;">:<br/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> The user explicitly asked to search, so we'll attempt to retrieve the query.</span><br/> NavigationContext.QueryString.TryGetValue(<span style="color: #800000;">"</span><span style="color: #800000;">dictatedSearchTerms</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">out</span><span style="color: #000000;"> phraseTopicContents);<br/> doSearch </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">;<br/> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br/> }<br/><br/> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (doSearch)<br/> {<br/> HandleSearchQuery(phraseTopicContents, typedVoiceCommand);<br/> }<br/> }</span></div><p><a href="http://images.cnitblog.com/blog/33251/201407/251830371668669.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;" title="image" src="http://images.cnitblog.com/blog/33251/201407/251830380578296.png" alt="image" width="696" height="180" border="0" /></a></p><p>整个过程就这么简单,心动不如行动,赶快把你的应用加入Cortana 功能让小伙伴儿们调戏一番。</p><p>更多参考资料:</p><p><a href="http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn630430.aspx" target="_blank">Quickstart: Voice commands (XAML)</a></p><p><a href="http://msdn.microsoft.com/library/windows/apps/jj206958(v=vs.105).aspx" target="_blank">Speech for Windows Phone 8</a></p><p><a href="http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/dn630430.aspx" target="_blank">快速入门:语音命令 (XAML)</a></p><p>源码下载:</p><p><a href="http://code.msdn.microsoft.com/windowsapps/MSDN-Voice-Search-for-95c16d92">MSDN Voice Search for Windows Phone 8.1</a></p><img src="http://counter.cnblogs.com/blog/rss/3868729" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/sonic1abc/p/3868729.html" target="_blank">如何将 Cortana 与 Windows Phone 8.1 应用集成 ( Voice command - Natural language recognition )</a>,转载请注明。</p>http://www.cnblogs.com/Enumz/p/3878509.html算法总结之欧拉函数&中国剩余定理 - Enumz算法总结之欧拉函数&中国剩余定理1.欧拉函数 概念:在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目。 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn) 其中p1, p2……pn为x的所有质因数,x是不为0的整数 ...2014-07-30T09:37:00Z2014-07-30T09:37:00ZEnumzhttp://www.cnblogs.com/Enumz/<p><span style="color: #ff0000; font-size: 14pt;"><strong>算法总结之欧拉函数&amp;中国剩余定理</strong></span></p><p><strong><span style="font-size: 16px;">1.欧拉函数</span></strong></p><p>  <strong>概念:</strong>在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目。</p><p>  <strong>通式:<span style="color: #ff0000;">&phi;(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)&hellip;..(1-1/pn)&nbsp;</span></strong></p><p><strong><span style="color: #ff0000;">    </span></strong>其中p1, p2&hellip;&hellip;pn为x的所有质因数,x是不为0的整数&nbsp;</p><p>  <strong>注意:</strong></p><p>    1)&nbsp;&phi;(1)=1.</p><p>    2)每种质因数只一个。比如12=2*2*3那么&phi;(12)=12*(1-1/2)*(1-1/3)=4</p><p>    3)若n是质数p的k次幂,&phi;(n)=p^k-p^(k-1)=(p-1)p^(k-1),因为除了p的倍数外,其他数都跟n互质。</p><p>    4)&phi;(mn)=&phi;(m)&phi;(n)</p><p>    5)当n为奇数时,&phi;(2n)=&phi;(n)</p><p>  <strong>代码实现:</strong></p><p><strong>1)直接求欧拉数:</strong></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008080;"> 1</span> <span style="color: #008000;">/*</span><span style="color: #008000;">函数返回值为n的欧拉函数值</span><span style="color: #008000;">*/</span><br/><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">int</span> euler(<span style="color: #0000ff;">int</span><span style="color: #000000;"> n)<br/></span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{<br/></span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">int</span> s=<span style="color: #000000;">n,i,m;<br/></span><span style="color: #008080;"> 5</span> m=<span style="color: #000000;">sqrt(n);<br/></span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">for</span>(i=<span style="color: #800080;">2</span>;i&lt;=m;i++<span style="color: #000000;">){<br/></span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">if</span>(n%i==<span style="color: #800080;">0</span><span style="color: #000000;">)<br/></span><span style="color: #008080;"> 8</span> s=s/i*(i-<span style="color: #800080;">1</span><span style="color: #000000;">);<br/></span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">while</span>(n%i==<span style="color: #800080;">0</span><span style="color: #000000;">)<br/></span><span style="color: #008080;">10</span> n/=<span style="color: #000000;">i;<br/></span><span style="color: #008080;">11</span> <span style="color: #000000;"> }<br/></span><span style="color: #008080;">12</span> <span style="color: #0000ff;">if</span>(n&gt;<span style="color: #800080;">1</span><span style="color: #000000;">)<br/></span><span style="color: #008080;">13</span> s=s/n*(n-<span style="color: #800080;">1</span><span style="color: #000000;">);<br/></span><span style="color: #008080;">14</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> s;<br/></span><span style="color: #008080;">15</span> }</div><p><strong>2)打表</strong></p><div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"><span style="color: #008080;"> 1</span> <span style="color: #008000;">/*</span><span style="color: #008000;">打印1-MAXN的欧拉函数表</span><span style="color: #008000;">*/</span><br/><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">int</span> a[MAXN]= {<span style="color: #800080;">1</span>,<span style="color: #800080;">1</span>,<span style="color: #800080;">0</span><span style="color: #000000;">};<br/></span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> euler()<br/></span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{<br/></span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> i,j;<br/></span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">for</span>(i=<span style="color: #800080;">2</span>; i&lt;=MAXN; i++<span style="color: #000000;">)<br/></span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">if</span>(!<span style="color: #000000;">a[i])<br/></span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">for</span>(j=i; j&lt;=MAXN; j+=<span style="color: #000000;">i)<br/></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> {<br/></span><span style="color: #008080;">10</span> <span style="color: #0000ff;">if</span>(a[j]==<span style="color: #800080;">0</span>) a[j]=<span style="color: #000000;">j;<br/></span><span style="color: #008080;">11</span> a[j]=a[j]/i*(i-<span style="color: #800080;">1</span><span style="color: #000000;">);<br/></span><span style="color: #008080;">12</span> <span style="color: #000000;"> }<br/></span><span style="color: #008080;">13</span> }</div><p><strong><span style="font-size: 16px;">&nbsp;2.中国剩余定理</span></strong></p><p><strong>  原文:</strong><br />    《孙子算经》中的题目:有物不知其数,三个一数余二,五个一数余三,七个一数又余二,问该物总数几何?<br />    《孙子算经》中的解法:三三数之,取数七十,与余数二相乘;五五数之,取数二十一,与余数三相乘;七七数之,取数十五,与余数二相乘。将诸乘积相加,然后减去一百零五的倍数。</p><p><strong>  结论:</strong></p><p><strong>    </strong>令任意固定整数为M,当M/A余a,M/B余b,M/C余c,M/D余d,&hellip;,M/Z余z时,这里的A,B,C,D,&hellip;,Z为除数,除数为任意自然数时;余数a,b,c,d,&hellip;&hellip;,z为自然整数时。</p><p>    1)当命题正确时,在这些除数的最小公倍数内有解,有唯一的解,每一个最小公倍数内都有唯一的解。</p><p>    2)当M在两个或两个以上的除数的最小公倍数内时,这两个或两个以上的除数和余数可以定位M在最小公倍数内的具体位置,也就是M的大小。</p><p>    3)正确的命题:分别除以A,B,C,D,&hellip;,Z不同的余数组合个数=A,B,C,D,&hellip;,Z的最小公倍数=不同的余数组合的循环周期。</p><p>  <strong>具体步骤(以《孙子算经》中的题目为例):</strong></p><p>    1)找出三个数:从3和5的公倍数中找出被7除余1的最小数15,从3和7的公倍数中找出被5除余1 的最小数21,最后从5和7的公倍数中找出除3余1的最小数70。</p><p>    2)用15乘以2(2为最终结果除以7的余数),用21乘以3(3为最终结果除以5的余数),同理,用70乘以2(2为最终结果除以3的余数),然后把三个乘积相加(15*2+21*3+70*2)得到和233。</p><p>    3)用233除以3,5,7三个数的最小公倍数105,得到余数23,即233%105=23。这个余数23就是符合条件的最小数。</p><img src="http://counter.cnblogs.com/blog/rss/3878509" width="1" height="1" alt=""/><br/><p>本文链接:<a href="http://www.cnblogs.com/Enumz/p/3878509.html" target="_blank">算法总结之欧拉函数&amp;中国剩余定理</a>,转载请注明。</p>

高级...
微信扫一扫
关注无线梦工厂
返回主页 手机网推广 加入Timewe 反馈意见 关于我们 加入收藏 点击这里给我发消息加关注加微信好友

触屏版,3G,Wap,手机网站,浏览器,pc版,在线,嵌入式,Wap浏览器,电脑上的Wap浏览器 各主流搜索引擎收录Timewe
(R)2009-2019 Timewe 时维网络科技有限公司 (86)0755-36601136
简体/繁体