<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vifix.cn &#187; Coding</title>
	<atom:link href="http://vifix.cn/blog/category/coding/feed" rel="self" type="application/rss+xml" />
	<link>http://vifix.cn/blog</link>
	<description>compile the dream, compile tomorrow</description>
	<lastBuildDate>Sun, 05 Sep 2010 12:43:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Python S60蓝牙终端与PC交互</title>
		<link>http://vifix.cn/blog/python-s60-bluetooth-console-with-xp-hyper-terminal.html</link>
		<comments>http://vifix.cn/blog/python-s60-bluetooth-console-with-xp-hyper-terminal.html#comments</comments>
		<pubDate>Tue, 15 Jun 2010 16:00:28 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[S60]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=1468</guid>
		<description><![CDATA[嘛……Python S60的一个比较Geeker的玩法。通过蓝牙串口通信连接到PC的串口通信的终端软件（已经差不多都快被遗忘了吧，这类软件），从而在PC上使用实际上运行在手机中的Python Shell并达到控制手机的目的。 在Windows XP中，可以使用XP内建的“超级终端”（HyperTerminal）来进行串口的点对点通信。 首先要在PC上开启一个蓝牙串口，我用的是蓝牙驱动程序是BlueSoleil，安装后就已经默认打开了COM6和COM8两个端口，我这里使用的是COM6。 然后启动超级终端，然后建立一个串口的连接，修改其属性和使用的端口为需要使用的COM6分端口。接下去开启手机的Python Shell，启用它的Bluetooth Console，然后选择PC和对应的端口即可完成连接。 接下去PC上的超级终端内就已经是那个熟悉的Python Shell了，可以输入print(&#8220;hello, world&#8221;)试试和世界打个招呼，当然也可以使用其它所有的Python S60提供的API，正如上面第一张截图的： import audio audio.say&#40;&#34;hello, master&#34;&#41; 手机当场会以它特有的电子音说出上面这句话来和它的主人问候 囧 另外也可以实现在终端里面发送短信，代码： import scriptext messaging_handle = scriptext.load&#40;'Service.Messaging', 'IMessaging'&#41; messaging_handle.call&#40;'Send', &#123;'MessageType': u'SMS', 'To': u'12345678', 'BodyText': u'Here is a message from python s60 shell.'&#125;&#41; 除此之外还有许多好玩的东西，可以去docs里面慢慢挖掘。 总体来说Python S60可用性真的非常高，基本上手机能支持的功能它都有API提供了，接下去可以好好在这方面研究一下。]]></description>
			<content:encoded><![CDATA[<p><a href="http://vifix.cn/blog/wp-content/uploads/2010/06/hyper-terminal.jpg" rel="shadowbox[post-1468];player=img;" title="hyper-terminal"><img class="alignnone size-medium wp-image-1470" title="hyper-terminal" src="http://vifix.cn/blog/wp-content/uploads/2010/06/hyper-terminal-300x197.jpg" alt="" width="300" height="197" /></a></p>
<p>嘛……Python S60的一个比较Geeker的玩法。通过蓝牙串口通信连接到PC的串口通信的终端软件（已经差不多都快被遗忘了吧，这类软件），从而在PC上使用实际上运行在手机中的Python Shell并达到控制手机的目的。</p>
<p><span id="more-1468"></span>在Windows XP中，可以使用XP内建的“超级终端”（HyperTerminal）来进行串口的点对点通信。</p>
<p>首先要在PC上开启一个蓝牙串口，我用的是蓝牙驱动程序是BlueSoleil，安装后就已经默认打开了COM6和COM8两个端口，我这里使用的是COM6。</p>
<p><a href="http://vifix.cn/blog/wp-content/uploads/2010/06/bluesoleil.jpg" rel="shadowbox[post-1468];player=img;" title="bluesoleil"><img class="alignnone size-medium wp-image-1472" title="bluesoleil" src="http://vifix.cn/blog/wp-content/uploads/2010/06/bluesoleil-300x255.jpg" alt="" width="300" height="255" /></a></p>
<p>然后启动超级终端，然后建立一个串口的连接，修改其属性和使用的端口为需要使用的COM6分端口。接下去开启手机的Python Shell，启用它的Bluetooth Console，然后选择PC和对应的端口即可完成连接。</p>
<p><a href="http://vifix.cn/blog/wp-content/uploads/2010/06/Screenshot0007.jpg" rel="shadowbox[post-1468];player=img;" title="Screenshot0007"><img class="alignnone size-medium wp-image-1471" title="Screenshot0007" src="http://vifix.cn/blog/wp-content/uploads/2010/06/Screenshot0007-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>接下去PC上的超级终端内就已经是那个熟悉的Python Shell了，可以输入print(&#8220;hello, world&#8221;)试试和世界打个招呼，当然也可以使用其它所有的Python S60提供的API，正如上面第一张截图的：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> audio
audio.<span style="color: black;">say</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;hello, master&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>手机当场会以它特有的电子音说出上面这句话来和它的主人问候 囧<br />
另外也可以实现在终端里面发送短信，代码：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> scriptext
messaging_handle = scriptext.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Service.Messaging'</span>, <span style="color: #483d8b;">'IMessaging'</span><span style="color: black;">&#41;</span>
messaging_handle.<span style="color: black;">call</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Send'</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'MessageType'</span>: u<span style="color: #483d8b;">'SMS'</span>, <span style="color: #483d8b;">'To'</span>: u<span style="color: #483d8b;">'12345678'</span>, <span style="color: #483d8b;">'BodyText'</span>: u<span style="color: #483d8b;">'Here is a message from python s60 shell.'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>除此之外还有许多好玩的东西，可以去docs里面慢慢挖掘。<br />
总体来说Python S60可用性真的非常高，基本上手机能支持的功能它都有API提供了，接下去可以好好在这方面研究一下。</p>
]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/python-s60-bluetooth-console-with-xp-hyper-terminal.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Rhino: Javascript for Java</title>
		<link>http://vifix.cn/blog/rhino-javascript-for-java.html</link>
		<comments>http://vifix.cn/blog/rhino-javascript-for-java.html#comments</comments>
		<pubDate>Sun, 21 Jun 2009 15:44:34 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=950</guid>
		<description><![CDATA[好几天没更新了，过去的一周里忙着补作业（一个学期根本没上课，自然作业也就……），半夜送室友去120（室友哮喘发作），下周则要开始考试，整个人也有点疲劳也没时间顾及这儿 orz 那么今次要介绍的东西是Mozilla旗下的一个名为Rhino的项目，纯Java实现的Javascript解释器，很适合希望在自己的Java程序中内嵌Scripting功能的同学。当然了，也可以用它在J2EE中实现用Javascript作为后台语言的功能。 官方网站为：http://www.mozilla.org/rhino/ PS: Mozilla旗下还有一个更知名的C写的Spider Monkey，被广泛应用于Mozilla的产品中，比如Firefox的Javascript引擎就是它，官方网站为：http://www.mozilla.org/js/spidermonkey/ 下面是一个简单的执行js文件的示例代码。 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; &#160; public class RhinoTest &#123; public static void main&#40;String&#91;&#93; args&#41; &#123; /* 创建一个Javascript的上下文环境，用来存储Javascript的环境信息 */ Context cx = Context.enter&#40;&#41;; try &#123; /* 初始化Javascript标准对象（例如Object, Function, Array等） */ Scriptable scope = cx.initStandardObjects&#40;&#41;; &#160; /* 读取一个.js文件 */ String script = &#34;&#34;; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://vifix.cn/blog/wp-content/uploads/2009/06/rhino50.jpg" rel="shadowbox[post-950];player=img;" title="rhino50"><img class="alignnone size-medium wp-image-951" title="rhino50" src="http://vifix.cn/blog/wp-content/uploads/2009/06/rhino50-300x150.jpg" alt="rhino50" width="300" height="150" /></a></p>
<p>好几天没更新了，过去的一周里忙着补作业（一个学期根本没上课，自然作业也就……），半夜送室友去120（室友哮喘发作），下周则要开始考试，整个人也有点疲劳也没时间顾及这儿 orz</p>
<p>那么今次要介绍的东西是Mozilla旗下的一个名为Rhino的项目，纯Java实现的Javascript解释器，很适合希望在自己的Java程序中内嵌Scripting功能的同学。当然了，也可以用它在J2EE中实现用Javascript作为后台语言的功能。</p>
<p>官方网站为：<a href="http://www.mozilla.org/rhino/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.mozilla.org/rhino/?referer=');">http://www.mozilla.org/rhino/</a></p>
<p>PS: Mozilla旗下还有一个更知名的C写的Spider Monkey，被广泛应用于Mozilla的产品中，比如Firefox的Javascript引擎就是它，官方网站为：<a href="http://www.mozilla.org/js/spidermonkey/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.mozilla.org/js/spidermonkey/?referer=');">http://www.mozilla.org/js/spidermonkey/</a></p>
<p>下面是一个简单的执行js文件的示例代码。</p>
<p><span id="more-950"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mozilla.javascript.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mozilla.javascript.Scriptable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RhinoTest
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">/* 创建一个Javascript的上下文环境，用来存储Javascript的环境信息 */</span>
        <span style="color: #003399;">Context</span> cx <span style="color: #339933;">=</span> <span style="color: #003399;">Context</span>.<span style="color: #006633;">enter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/* 初始化Javascript标准对象（例如Object, Function, Array等） */</span>
            Scriptable scope <span style="color: #339933;">=</span> cx.<span style="color: #006633;">initStandardObjects</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/* 读取一个.js文件 */</span>
            <span style="color: #003399;">String</span> script <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// 如果有参数，则读入第一个参数中指定的js文件</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #009900;">&#123;</span>
                file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;script.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 如果没有参数，则读入script.js</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #003399;">BufferedReader</span> in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileReader</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>s <span style="color: #339933;">=</span> in.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                script <span style="color: #339933;">+=</span> s <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/* 执行代码 */</span>
            cx.<span style="color: #006633;">evaluateString</span><span style="color: #009900;">&#40;</span>scope, script, <span style="color: #0000ff;">&quot;[&quot;</span> <span style="color: #339933;">+</span> file.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            ex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">finally</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Context</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>测试用的script.js文件：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> lang <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> JavaImporter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
lang.<span style="color: #660066;">importPackage</span><span style="color: #009900;">&#40;</span>java.<span style="color: #660066;">lang</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
lang.<span style="color: #660066;">System</span>.<span style="color: #660066;">out</span>.<span style="color: #660066;">println</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello, World!~&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> swing <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> JavaImporter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
swing.<span style="color: #660066;">importPackage</span><span style="color: #009900;">&#40;</span>javax.<span style="color: #660066;">swing</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">with</span><span style="color: #009900;">&#40;</span>swing<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> frame <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> JFrame<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Swing Application&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	frame.<span style="color: #660066;">setDefaultCloseOperation</span><span style="color: #009900;">&#40;</span>JFrame.<span style="color: #660066;">EXIT_ON_CLOSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> button <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> JButton<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello, Rhino!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	button.<span style="color: #660066;">addActionListener</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		lang.<span style="color: #660066;">System</span>.<span style="color: #660066;">out</span>.<span style="color: #660066;">println</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;This is a text from swing button&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	frame.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>button<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	frame.<span style="color: #660066;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	frame.<span style="color: #660066;">setResizable</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	frame.<span style="color: #660066;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/rhino-javascript-for-java.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>利用Google Map API获取地址坐标</title>
		<link>http://vifix.cn/blog/convert-address-to-coordinates-google-map-api.html</link>
		<comments>http://vifix.cn/blog/convert-address-to-coordinates-google-map-api.html#comments</comments>
		<pubDate>Sat, 06 Jun 2009 14:41:29 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Atelier]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=941</guid>
		<description><![CDATA[朋友拜托改的一段PHP小代码，其实就是Google MAP API FAQ里面提到的例子稍作修改，原文例子还用到了MySQL作为存储因此扯了一堆MySQL和PHPMyAdmin，只是想简单查下的就显得没必要了。 修改 @ 2009-06-07 : 于是今天写了一个Javascript版本的：http://vifix.cn/atelier/address_to_coordinates/ ，代码都在HTML里面了，感兴趣可以右键查看源码。 下面是PHP版本的代码： define&#40;&#34;MAPS_HOST&#34;, &#34;maps.google.com&#34;&#41;; define&#40;&#34;KEY&#34;, &#34;更换成你的KEY，可以去 http://code.google.com/intl/zh-CN/apis/maps/signup.html 申请&#34;&#41;; $base_url = &#34;http://&#34; . MAPS_HOST . &#34;/maps/geo?output=xml&#34; . &#34;&#38;amp;key=&#34; . KEY; $address = &#34;上海东方明珠塔&#34;; // 需要查询的地址 $request_url = $base_url . &#34;&#38;amp;q=&#34; . urlencode&#40;$address&#41;; $xml = simplexml_load_file&#40;$request_url&#41; or die&#40;&#34;url not loading&#34;&#41;; &#160; $status = $xml-&#38;gt;Response-&#38;gt;Status-&#38;gt;code; if &#40;strcmp&#40;$status, &#34;200&#34;&#41; == [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-196" href="http://vifix.cn/blog/google%e5%a8%98%ef%bd%9e.html/googlecom" title="Google.com"><img class="alignnone size-full wp-image-196" title="Google.com" src="http://vifix.cn/blog/wp-content/uploads/2008/11/googlecom.png" alt="Google.com" width="135" height="200" /></a></p>
<p>朋友拜托改的一段PHP小代码，其实就是<a href="http://code.google.com/intl/zh-CN/apis/maps/articles/phpsqlgeocode.html" target="_blank" onclick="pageTracker._trackPageview('/outgoing/code.google.com/intl/zh-CN/apis/maps/articles/phpsqlgeocode.html?referer=');">Google MAP API FAQ里面提到的例子</a>稍作修改，原文例子还用到了MySQL作为存储因此扯了一堆MySQL和PHPMyAdmin，只是想简单查下的就显得没必要了。</p>
<h3>修改 @ 2009-06-07 :</h3>
<p>于是今天写了一个Javascript版本的：<a href="http://vifix.cn/atelier/address_to_coordinates/">http://vifix.cn/atelier/address_to_coordinates/<br />
</a>，代码都在HTML里面了，感兴趣可以右键查看源码。</p>
<p>下面是PHP版本的代码：<br />
<span id="more-941"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MAPS_HOST&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;maps.google.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;KEY&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;更换成你的KEY，可以去 http://code.google.com/intl/zh-CN/apis/maps/signup.html 申请&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$base_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://&quot;</span> <span style="color: #339933;">.</span> MAPS_HOST <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/maps/geo?output=xml&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;amp;key=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">KEY</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$address</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;上海东方明珠塔&quot;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// 需要查询的地址</span>
<span style="color: #000088;">$request_url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$base_url</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;amp;q=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$address</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_url</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;url not loading&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Response<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Status<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>code<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;200&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Successful geocode</span>
	<span style="color: #000088;">$geocode_pending</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$coordinates</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Response<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Placemark<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Point<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>coordinates<span style="color: #339933;">;</span>
	<span style="color: #000088;">$coordinatesSplit</span> <span style="color: #339933;">=</span> <span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$coordinates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Format: Longitude, Latitude, Altitude</span>
	<span style="color: #000088;">$lat</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$coordinatesSplit</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$lng</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$coordinatesSplit</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;坐标: <span style="color: #006699; font-weight: bold;">$lat</span>,<span style="color: #006699; font-weight: bold;">$lng</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;620&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;请求频率过快&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;请求失败&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/convert-address-to-coordinates-google-map-api.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>WordPress同步QZone插件·改</title>
		<link>http://vifix.cn/blog/wordpress-plugin-post-to-qzone-mod.html</link>
		<comments>http://vifix.cn/blog/wordpress-plugin-post-to-qzone-mod.html#comments</comments>
		<pubDate>Fri, 05 Jun 2009 02:22:50 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[QQ]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=932</guid>
		<description><![CDATA[这两天在想写一个WordPress的插件练练手，因为正好看到了QZone也支持了用邮件发表的文章，因此在想是不是就写个插件让WordPress发表文章自动同步到QQ。一来可以吸引QQ上的好友来阅读我发表的内容，增进交互，二来也可以算是一定程度上的SEO。 但写完了SMTP连接部分时在找WordPress插件的文档时找到了已经有现成的插件，测试了一下已经把我想做的都做出来了，还支持添加额外的电子信箱，用来支持其它所有支持电子邮件发表的BSP的发表（不过Live.com的博客服务因为暂时被功·夫·网所以无法使用这个） 不过实际尝试了一下发现这个插件还很有问题： 保存密码的表单密码将被重复加密导致不可用 对于引号和斜线的转义过头导致数据丢失（似乎这个是WordPress本身转义提交信息带来的问题） 因此对这个插件进行了一番修改： 纠正错误的转义 放弃原版使用的PHPMailer（杀鸡用牛刀的感觉），换上一个较为简单的Socket SMTP发送函数修改的类 修正密码被重复加密的Bug 我修改的版本在此下载：wp-qzone.0.4-mod.zip 原始的WP-Post2Qzone获取地址：http://code.google.com/p/qzone/ 作者的博客：http://liguangming.com/ 更新@2010-07-26 不明原因的插件可能暂时无法使用中，待检查]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-935" href="http://vifix.cn/blog/wordpress-plugin-post-to-qzone-mod.html/qq" title="qq"><img class="alignnone size-thumbnail wp-image-935" title="qq" src="http://vifix.cn/blog/wp-content/uploads/2009/06/qq-150x150.png" alt="qq" width="150" height="150" /></a></p>
<p>这两天在想写一个WordPress的插件练练手，因为正好看到了QZone也支持了用邮件发表的文章，因此在想是不是就写个插件让WordPress发表文章自动同步到QQ。一来可以吸引QQ上的好友来阅读我发表的内容，增进交互，二来也可以算是一定程度上的SEO。</p>
<p>但写完了SMTP连接部分时在找WordPress插件的文档时找到了已经有现成的插件，测试了一下已经把我想做的都做出来了，还支持添加额外的电子信箱，用来支持其它所有支持电子邮件发表的BSP的发表（不过Live.com的博客服务因为暂时被功·夫·网所以无法使用这个）</p>
<p>不过实际尝试了一下发现这个插件还很有问题：</p>
<ul>
<li>保存密码的表单密码将被重复加密导致不可用</li>
<li>对于引号和斜线的转义过头导致数据丢失（似乎这个是WordPress本身转义提交信息带来的问题）</li>
</ul>
<p>因此对这个插件进行了一番修改：</p>
<ul>
<li>纠正错误的转义</li>
<li>放弃原版使用的PHPMailer（杀鸡用牛刀的感觉），换上一个较为简单的Socket SMTP发送函数修改的类</li>
<li>修正密码被重复加密的Bug</li>
</ul>
<p>我修改的版本在此下载：<a href="http://vifix.cn/blog/wp-content/uploads/2009/06/wp-qzone04-mod.zip">wp-qzone.0.4-mod.zip</a></p>
<p>原始的WP-Post2Qzone获取地址：<a href="http://code.google.com/p/qzone/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/qzone/?referer=');">http://code.google.com/p/qzone/</a></p>
<p>作者的博客：<a href="http://liguangming.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/liguangming.com/?referer=');">http://liguangming.com/</a></p>
<h3>更新@2010-07-26</h3>
<p>不明原因的插件可能暂时无法使用中，待检查</p>
]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/wordpress-plugin-post-to-qzone-mod.html/feed</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>一个PHP简易访问计数</title>
		<link>http://vifix.cn/blog/ezcounter.html</link>
		<comments>http://vifix.cn/blog/ezcounter.html#comments</comments>
		<pubDate>Sun, 31 May 2009 12:52:18 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Atelier]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=922</guid>
		<description><![CDATA[嘛，受朋友所托写的，不是什么大不了的东西，贴上来就当给初学者参考吧 只有计数功能，利用session防止重复计数 注意只能运行在PHP5+上 代码见页内： /** * ezcounter * http://vifix.cn * MIT Lisence */ &#160; class ezcounter&#123; &#160; const display_only = 1; const display_and_count = 0; &#160; private $file; private $hits; &#160; // 构造函数 public function __construct&#40;$name = 'ezcounter'&#41;&#123; &#160; // 存放计数的文件 $this-&#62;file = dirname&#40;__FILE__&#41; . &#34;/$name.txt&#34;; &#160; // 判断文件是否存在，不存在则创建 if&#40;file_exists&#40;$this-&#62;file&#41;&#41; &#123; $this-&#62;hits = file_get_contents&#40;$this-&#62;file&#41;; &#125; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://vifix.cn/blog/wp-content/uploads/2009/05/source_php.png" rel="shadowbox[post-922];player=img;" title="source_php"><img class="alignnone size-full wp-image-923" title="source_php" src="http://vifix.cn/blog/wp-content/uploads/2009/05/source_php.png" alt="source_php" width="128" height="128" /></a></p>
<p>嘛，受朋友所托写的，不是什么大不了的东西，贴上来就当给初学者参考吧<br />
只有计数功能，利用session防止重复计数</p>
<p>注意只能运行在PHP5+上</p>
<p>代码见页内：</p>
<p><span id="more-922"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 *  ezcounter
 *  http://vifix.cn
 *  MIT Lisence
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ezcounter<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">const</span> display_only <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">const</span> display_and_count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$hits</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 构造函数</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ezcounter'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 存放计数的文件</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span> <span style="color: #339933;">=</span>  <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/<span style="color: #006699; font-weight: bold;">$name</span>.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 判断文件是否存在，不存在则创建</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 获取计数</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> gethits<span style="color: #009900;">&#40;</span><span style="color: #000088;">$flag</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">display_and_count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$flag</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">display_and_count</span><span style="color: #339933;">:</span>
				<span style="color: #339933;">@</span><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ezcounter'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span><span style="color: #339933;">++;</span>
					<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ezcounter'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">display_only</span><span style="color: #339933;">:</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 重定向计数</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> redirect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #339933;">@</span><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ezcounter'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span><span style="color: #339933;">++;</span>
			<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hits</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ezcounter'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: <span style="color: #006699; font-weight: bold;">$url</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>用法1，只是显示访问计数</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ezcounter.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ezcounter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$counter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gethits</span><span style="color: #009900;">&#40;</span>ezcounter<span style="color: #339933;">::</span><span style="color: #004000;">display_and_count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>用法2，转发计数（比如可以用于下载计数）</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//download.php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ezcounter.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ezcounter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'another_file_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$counter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/target'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//view.php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ezcounter.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ezcounter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'another_file_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=&quot;</span>download<span style="color: #339933;">.</span>php<span style="color: #0000ff;">&quot;&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$counter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gethits</span><span style="color: #009900;">&#40;</span>ezcounter<span style="color: #339933;">::</span><span style="color: #004000;">display_only</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/ezcounter.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>开源键-值数据库笔记</title>
		<link>http://vifix.cn/blog/opensource-key-value-database.html</link>
		<comments>http://vifix.cn/blog/opensource-key-value-database.html#comments</comments>
		<pubDate>Wed, 20 May 2009 11:58:07 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Freeware]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=907</guid>
		<description><![CDATA[前段时间玩了Google App Engine之后一直对开源的键-值数据库比较关注，因为其开发使用方便，也有了在研究在自己的服务器上部署的可能性，于是自己Google了一些，同时Flying殿也有推荐了一下，于是发文简单记录一下。 眼下关注的有： MongoDB Hypertable CouchDB Tokyo Cabinet Project Voldemort 1. MongoDB 今天测试了一个白天的数据库，使用JSON格式存储数据，并且用Java小测试了一下它的性能，还算不错，1000000条记录里面检索500条记录，耗时0.078秒（有索引）。不过遗憾的是，没有编译成功它的Python API，PHP API的编译也因为文档不全的问题暂时搁置。不然的话真准备在这个上面做些Project试试看。另外它的控制台客户端的命令是ECMAScript的，对我来说觉得十分亲切。 官网：http://www.mongodb.org/ 2. Hypertable 根据Google的Bigtable的论文开始的一个项目，因此可以说这是一个开源的Google Bigtable的实现，通过Thrift这个框架（Facebook开发然后捐赠给Apache的工具）为其它语言提供API（不过官网上暂时仅看到Ruby的参考）。有趣的是，官方网站下面的赞助商那里看到了百度的Logo。 官网：http://hypertable.org/ 3. CouchDB Apache旗下的项目，使用Erlang/OTP开发，同样使用JSON存储数据。眼下的版本看了网上的评测性能还很一般。期待后续的发展。 官网：http://couchdb.apache.org/ 4. Tokyo Cabinet Tokyo Cabinet 是日本人平林幹雄开发的一款DBM数据库，该数据库读写非常快，哈希模式写入100万条数据只需0.643秒，读取100万条数据只需0.773秒，是 Berkeley DB 等DBM的几倍。 暂时只有Perl, Ruby, Java, Lua的API，不过可以通过Tokyo Tyrant来支持其它语言（通过Memcached协议） 官网：http://tokyocabinet.sourceforge.net/ 5. Project Voldemort 貌似可以使用MySQL（还有其他）作为永久存储介质的分布式数据库。 官网：http://project-voldemort.com/]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-908" title="server" src="http://vifix.cn/blog/wp-content/uploads/2009/05/server.png" alt="server" width="128" height="128" /></p>
<p>前段时间玩了Google App Engine之后一直对开源的键-值数据库比较关注，因为其开发使用方便，也有了在研究在自己的服务器上部署的可能性，于是自己Google了一些，同时Flying殿也有推荐了一下，于是发文简单记录一下。</p>
<p>眼下关注的有：</p>
<ol>
<li>MongoDB</li>
<li>Hypertable</li>
<li>CouchDB</li>
<li>Tokyo Cabinet</li>
<li>Project Voldemort</li>
</ol>
<p><span id="more-907"></span></p>
<h3>1. MongoDB</h3>
<p>今天测试了一个白天的数据库，使用JSON格式存储数据，并且用Java小测试了一下它的性能，还算不错，1000000条记录里面检索500条记录，耗时0.078秒（有索引）。不过遗憾的是，没有编译成功它的Python API，PHP API的编译也因为文档不全的问题暂时搁置。不然的话真准备在这个上面做些Project试试看。另外它的控制台客户端的命令是ECMAScript的，对我来说觉得十分亲切。</p>
<p>官网：<a href="http://www.mongodb.org/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.mongodb.org/?referer=');">http://www.mongodb.org/</a></p>
<h3>2. Hypertable</h3>
<p>根据Google的Bigtable的论文开始的一个项目，因此可以说这是一个开源的Google Bigtable的实现，通过Thrift这个框架（Facebook开发然后捐赠给Apache的工具）为其它语言提供API（不过官网上暂时仅看到Ruby的参考）。有趣的是，官方网站下面的赞助商那里看到了百度的Logo。</p>
<p>官网：<a href="http://hypertable.org/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/hypertable.org/?referer=');">http://hypertable.org/</a></p>
<h3>3. CouchDB</h3>
<p>Apache旗下的项目，使用Erlang/OTP开发，同样使用JSON存储数据。眼下的版本看了网上的评测性能还很一般。期待后续的发展。</p>
<p>官网：<a href="http://couchdb.apache.org/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/couchdb.apache.org/?referer=');">http://couchdb.apache.org/</a></p>
<h3>4. Tokyo Cabinet</h3>
<p>Tokyo Cabinet 是日本人平林幹雄开发的一款DBM数据库，该数据库读写非常快，哈希模式写入100万条数据只需0.643秒，读取100万条数据只需0.773秒，是 Berkeley DB 等DBM的几倍。<br />
暂时只有Perl, Ruby, Java, Lua的API，不过可以通过Tokyo Tyrant来支持其它语言（通过Memcached协议）<br />
官网：<a href="http://tokyocabinet.sourceforge.net/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/tokyocabinet.sourceforge.net/?referer=');">http://tokyocabinet.sourceforge.net/</a></p>
<h3>5. Project Voldemort</h3>
<p>貌似可以使用MySQL（还有其他）作为永久存储介质的分布式数据库。</p>
<p>官网：<a href="http://project-voldemort.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/project-voldemort.com/?referer=');">http://project-voldemort.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/opensource-key-value-database.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google App Engine中datastore的Key</title>
		<link>http://vifix.cn/blog/google-app-engine-datastore-key.html</link>
		<comments>http://vifix.cn/blog/google-app-engine-datastore-key.html#comments</comments>
		<pubDate>Thu, 14 May 2009 14:56:03 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=898</guid>
		<description><![CDATA[Google App Engine使用了一种与关系数据库完全不同的数据库，称为分布式键-值数据库（Key-Value-Database），所有的&#8221;条目&#8221;，都是一个对象。这种数据库拥有代码简单，开发效率高，高并发访问效率等特性，但同时也有不支持模糊查询等缺点。 在使用上，键-值数据库和之前熟悉的MySQL等所有的关系数据库都有点不太一样，因此在开发vxtodo时走了一些弯路，比如这个等同于关系数据库的&#8221;主键&#8221;的问题就让我奇怪了很久（当然，对于这种的键-值数据库中，是否能以主键来称呼应该值得商榷），刚才终于摸索清楚了，记录下来。 Google App Engine的datastore的每个条目的唯一的键是自动生成的，无需人工干预。因为一开始并不清楚这一点，所以定义&#8221;表&#8221;的结构时找不到类似于自动索引的功能，结果开始写vxtodo时我用的是时间戳 + 用户名邮箱地址然后Hash再取绝对值出来的一个唯一值的诡异方法，虽然也能用，但是毕竟不够优雅（这样的Hash值实在是太长了）。仔细翻阅文档后，终于把ID/Name部分给看明白了。 首先，定义一个数据类型： class Task&#40;db.Model&#41;: user = db.UserProperty&#40;&#41; title = db.StringProperty&#40;&#41; content = db.TextProperty&#40;&#41; 这个就不用多说了，一目了然，GAE的入门教程里面也说的很清楚。一个&#8221;字段&#8221;一个属性，Google提供了一批数据类型可以使用。 获取某个条目的ID： query = Task.all&#40;&#41; tasks = query.fetch&#40;50&#41; for task in tasks: print task.key&#40;&#41;.id&#40;&#41; 与别的&#8221;字段&#8221;不同的是，无法直接以task.id来获取它，而是通过调用它的key()方法返回一个Key对象然后再执行这个Key对象的id()方法来获取到这个id。 用一个给定的ID来进行查找： key = Key.from_path&#40;&#34;Task&#34;, 3&#41; #Task类别里面的ID3的对象的键 —— 翻译成关系数据库的话，就是Task表里面的主键为3的项目的主键 query = Task.all&#40;&#41; query.ancestor&#40;key&#41; task = query.get&#40;&#41; #获取对象 id这个&#8221;字段&#8221;和别的属性不一样，是用Query类专门的ancestor()的方法来处理的，虽然一开始看文档的时候就注意到了这个方法，但是由于并不知道如何给定key的值（因为不知道上面的这个获取方法）所以不明如何使用。在看了Key的文档的from_path()方法恍然大悟。 去年在Java上接触到过一种对象数据库db4o其实差不多就是这样的类型，当时也花了一些时间来研究，然而当时这个db4o并不是很成熟（印象中存储中文似乎有乱码问题），最终不了了之，最近在Google App [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://vifix.cn/blog/wp-content/uploads/2009/05/google_appengine.png" rel="shadowbox[post-898];player=img;" title="google_appengine"><img class="alignnone size-thumbnail wp-image-899" title="google_appengine" src="http://vifix.cn/blog/wp-content/uploads/2009/05/google_appengine-150x150.png" alt="google_appengine" width="150" height="150" /></a></p>
<p>Google App Engine使用了一种与关系数据库完全不同的数据库，称为分布式键-值数据库（Key-Value-Database），所有的&#8221;条目&#8221;，都是一个对象。这种数据库拥有代码简单，开发效率高，高并发访问效率等特性，但同时也有不支持模糊查询等缺点。</p>
<p>在使用上，键-值数据库和之前熟悉的MySQL等所有的关系数据库都有点不太一样，因此在开发<a href="http://vifix.cn/blog/vxtodo.html">vxtodo</a>时走了一些弯路，比如这个等同于关系数据库的&#8221;主键&#8221;的问题就让我奇怪了很久（当然，对于这种的键-值数据库中，是否能以主键来称呼应该值得商榷），刚才终于摸索清楚了，记录下来。</p>
<p>Google App Engine的datastore的每个条目的唯一的键是自动生成的，无需人工干预。因为一开始并不清楚这一点，所以定义&#8221;表&#8221;的结构时找不到类似于自动索引的功能，结果开始写vxtodo时我用的是时间戳 + 用户名邮箱地址然后Hash再取绝对值出来的一个唯一值的诡异方法，虽然也能用，但是毕竟不够优雅（这样的Hash值实在是太长了）。仔细翻阅文档后，终于把ID/Name部分给看明白了。</p>
<p><span id="more-898"></span></p>
<p>首先，定义一个数据类型：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Task<span style="color: black;">&#40;</span>db.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">user</span> = db.<span style="color: black;">UserProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    title = db.<span style="color: black;">StringProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    content = db.<span style="color: black;">TextProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>这个就不用多说了，一目了然，GAE的入门教程里面也说的很清楚。一个&#8221;字段&#8221;一个属性，Google提供了一批数据类型可以使用。</p>
<p>获取某个条目的ID：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">query = Task.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
tasks = query.<span style="color: black;">fetch</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> task <span style="color: #ff7700;font-weight:bold;">in</span> tasks:
    <span style="color: #ff7700;font-weight:bold;">print</span> task.<span style="color: black;">key</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>与别的&#8221;字段&#8221;不同的是，无法直接以task.id来获取它，而是通过调用它的key()方法返回一个Key对象然后再执行这个Key对象的id()方法来获取到这个id。</p>
<p>用一个给定的ID来进行查找：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">key = Key.<span style="color: black;">from_path</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Task&quot;</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;">#Task类别里面的ID3的对象的键 —— 翻译成关系数据库的话，就是Task表里面的主键为3的项目的主键</span>
query = Task.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
query.<span style="color: black;">ancestor</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
task = query.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#获取对象</span></pre></div></div>

<p>id这个&#8221;字段&#8221;和别的属性不一样，是用Query类专门的ancestor()的方法来处理的，虽然一开始看文档的时候就注意到了这个方法，但是由于并不知道如何给定key的值（因为不知道上面的这个获取方法）所以不明如何使用。在看了Key的文档的from_path()方法恍然大悟。</p>
<p>去年在Java上接触到过一种对象数据库<a href="http://www.db4o.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.db4o.com/?referer=');">db4o</a>其实差不多就是这样的类型，当时也花了一些时间来研究，然而当时这个db4o并不是很成熟（印象中存储中文似乎有乱码问题），最终不了了之，最近在Google App Engine重新碰到这样的数据库类型，算是对这种键-值数据库的重新认识了吧。</p>
]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/google-app-engine-datastore-key.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>vxtodo</title>
		<link>http://vifix.cn/blog/vxtodo.html</link>
		<comments>http://vifix.cn/blog/vxtodo.html#comments</comments>
		<pubDate>Tue, 12 May 2009 14:39:28 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Atelier]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=895</guid>
		<description><![CDATA[这段时间研究Google App Engine的成果，今天发布。 一个个人TODO-List管理程序，支持邮件提醒。 服务器端是Python，客户端自然还是 HTML + CSS + JS，话说Javascript的代码比Python还多一倍，没jQuery的话代码量应该会更加冗长……不过用Google App Engine来写程序还是挺幸福的。 欢迎各位使用（会有人用么？），以及帮忙测试和报告Bug 在GPL第二版协议下开源。 地址： http://vxtodo.ihfs.net/ http://vxtodo.appspot.com/ 获取源代码前往： http://code.google.com/p/vxtodo/ PS.I: google app engine还无法支持.cn域名，因此只好用这个ihfs.net来进行绑定了…… PS.II: 话说appspot偶尔会被GFW掉，无奈…… PS.III: 于是咱也玩了把云计算了啊～ 更新@2009-05-14: 修正了无法输入\和回车的bug 加入Prism的说明 制作了LOGO 更新@2009-05-15: 修改了数据模型，删除了一些无用的测试数据（没办法的选择，这种键-值数据库无法修改“表结构”，只能清空数据库然后重新添加 更新@2009-05-17: 对状态图标加上了事件，点击可以直接转换状态 将代码导入Google Code，以GPLv2开源 更新@2010-01-17: 解决一个换行的问题]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-896" href="http://vifix.cn/blog/vxtodo.html/vxtodo" title="vxtodo"><img class="alignnone size-medium wp-image-896" title="vxtodo" src="http://vifix.cn/blog/wp-content/uploads/2009/05/vxtodo-290x300.png" alt="vxtodo" width="290" height="300" /></a></p>
<p>这段时间研究Google App Engine的成果，今天发布。</p>
<p>一个个人TODO-List管理程序，支持邮件提醒。</p>
<p>服务器端是Python，客户端自然还是 HTML + CSS + JS，话说Javascript的代码比Python还多一倍，没jQuery的话代码量应该会更加冗长……不过用Google App Engine来写程序还是挺幸福的。</p>
<p>欢迎各位使用（会有人用么？），以及帮忙测试和报告Bug</p>
<p>在GPL第二版协议下开源。</p>
<p>地址：<br />
<a href="http://vxtodo.ihfs.net/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/vxtodo.ihfs.net/?referer=');">http://vxtodo.ihfs.net/</a><br />
<a href="http://vxtodo.appspot.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/vxtodo.appspot.com/?referer=');">http://vxtodo.appspot.com/</a></p>
<p>获取源代码前往：<br />
<a href="http://code.google.com/p/vxtodo/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/vxtodo/?referer=');">http://code.google.com/p/vxtodo/</a></p>
<p>PS.I: google app engine还无法支持.cn域名，因此只好用这个ihfs.net来进行绑定了……<br />
PS.II: 话说appspot偶尔会被GFW掉，无奈……<br />
PS.III: 于是咱也玩了把云计算了啊～</p>
<h3>更新@2009-05-14:</h3>
<ol>
<li>修正了无法输入\和回车的bug</li>
<li>加入Prism的说明</li>
<li>制作了LOGO</li>
</ol>
<h3>更新@2009-05-15:</h3>
<ol>
<li>修改了数据模型，删除了一些无用的测试数据（没办法的选择，这种键-值数据库无法修改“表结构”，只能清空数据库然后重新添加</li>
</ol>
<h3>更新@2009-05-17:</h3>
<ol>
<li>对状态图标加上了事件，点击可以直接转换状态</li>
<li>将代码导入Google Code，以GPLv2开源</li>
</ol>
<h3>更新@2010-01-17:</h3>
<ol>
<li>解决一个换行的问题</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/vxtodo.html/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Java中的支持重试，有限任务数量线程池</title>
		<link>http://vifix.cn/blog/java-limited-task-amount-threads-pool.html</link>
		<comments>http://vifix.cn/blog/java-limited-task-amount-threads-pool.html#comments</comments>
		<pubDate>Tue, 24 Mar 2009 09:18:47 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Atelier]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=788</guid>
		<description><![CDATA[昨天学校网络没有续费没法上网，晚上找事情干，就翻着O&#8217;Reilly的《Java线程》（第三版）看了一些内容然后写的实验性质的代码，主要是对于Konachan的线程池代码很不满意而写的。 这些代码主要包括： 三个线程复用的线程池（利用ThreadPoolExecutor） 通过判断线程池现有的活动线程数量限制ThreadPoolExecutor执行新任务 如果执行出错，可以自动重试（这里的“任务”有80%的成功率） 利用Hashtable&#60;Integer, String&#62;来记录“任务”的运行状态，如果不在Hashtable中，表示任务已经结束 每个“任务”耗时0到10000毫秒（利用Thread.sleep模拟） 然而这个实现也让人觉得不够优雅，也许自己实现一个RejectedExecutionHandler接口的类，然后绑定为ThreadPoolExecutor的弹出策略才是王道吧。另外，觉得应该还需要自己还要再写一个队列。 完整的代码（Eclipse工程在文末）： Main: package cn.vifix.concurrentLab; import java.util.Hashtable; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; &#160; public class ThreadPoolDemo &#123; public static Hashtable&#60;Integer,String&#62; jobs; &#160; public static void main&#40;String&#91;&#93; args&#41; &#123; new ThreadPoolDemo&#40;&#41;; &#125; &#160; public ThreadPoolDemo&#40;&#41; &#123; jobs = new Hashtable&#60;Integer, String&#62;&#40;&#41;; int taskCount = 30; for &#40;int [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://vifix.cn/blog/wp-content/uploads/2009/03/threadpooldemo_output.png" rel="shadowbox[post-788];player=img;" title="threadpooldemo_output"><img src="http://vifix.cn/blog/wp-content/uploads/2009/03/threadpooldemo_output-300x123.png" alt="threadpooldemo_output" title="threadpooldemo_output" width="300" height="123" class="alignnone size-medium wp-image-789" /></a></p>
<p>昨天学校网络没有续费没法上网，晚上找事情干，就翻着O&#8217;Reilly的《Java线程》（第三版）看了一些内容然后写的实验性质的代码，主要是对于<a href="http://vifix.cn/blog/konachan.html">Konachan</a>的线程池代码很不满意而写的。</p>
<p>这些代码主要包括：</p>
<ol>
<li>三个线程复用的线程池（利用ThreadPoolExecutor）</li>
<li>通过判断线程池现有的活动线程数量限制ThreadPoolExecutor执行新任务</li>
<li>如果执行出错，可以自动重试（这里的“任务”有80%的成功率）</li>
<li>利用Hashtable&lt;Integer, String&gt;来记录“任务”的运行状态，如果不在Hashtable中，表示任务已经结束</li>
<li>每个“任务”耗时0到10000毫秒（利用Thread.sleep模拟）</li>
</ol>
<p>然而这个实现也让人觉得不够优雅，也许自己实现一个RejectedExecutionHandler接口的类，然后绑定为ThreadPoolExecutor的弹出策略才是王道吧。另外，觉得应该还需要自己还要再写一个队列。</p>
<p>完整的代码（Eclipse工程在文末）：</p>
<p><span id="more-788"></span></p>
<p><strong>Main:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.vifix.concurrentLab</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Hashtable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.concurrent.SynchronousQueue</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.concurrent.ThreadPoolExecutor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.concurrent.TimeUnit</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThreadPoolDemo
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Hashtable<span style="color: #339933;">&lt;</span>Integer,String<span style="color: #339933;">&gt;</span> jobs<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">new</span> ThreadPoolDemo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ThreadPoolDemo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		jobs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Hashtable<span style="color: #339933;">&lt;</span>Integer, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> taskCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>taskCount<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			jobs.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>i, <span style="color: #0000ff;">&quot;todo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		SynchronousQueue<span style="color: #339933;">&lt;</span>Runnable<span style="color: #339933;">&gt;</span> queue <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SynchronousQueue<span style="color: #339933;">&lt;</span>Runnable<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ThreadPoolExecutor exec <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ThreadPoolExecutor<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">20</span>, TimeUnit.<span style="color: #006633;">SECONDS</span>, queue, <span style="color: #000000; font-weight: bold;">new</span> ThreadPoolExecutor.<span style="color: #006633;">DiscardPolicy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>jobs.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">try</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>exec.<span style="color: #006633;">getActiveCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Thread count == 3; Skip this loop...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> taskCount <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jobs.<span style="color: #006633;">containsKey</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jobs.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;todo&quot;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						jobs.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>i, <span style="color: #0000ff;">&quot;doing&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						exec.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ThreadPoolDemoWorker<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>jobs.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; Task Left:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; List:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>jobs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;All done.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Worker:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.vifix.concurrentLab</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThreadPoolDemoWorker <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> idx<span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">new</span> ThreadPoolDemoJob<span style="color: #009900;">&#40;</span>idx<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ThreadPoolDemoWorker<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		idx <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Task:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.vifix.concurrentLab</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Random</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThreadPoolDemoTask
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> idx<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Random</span> random<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ThreadPoolDemoTask<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		idx <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
		random <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> ex<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			ex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">doTask</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> doTask<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextFloat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0.8</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			ThreadPoolDemo.<span style="color: #006633;">jobs</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>idx, <span style="color: #0000ff;">&quot;todo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; Task &quot;</span> <span style="color: #339933;">+</span> idx <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; failed.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Task &quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>idx<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; failed, retry.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; Task &quot;</span> <span style="color: #339933;">+</span> idx <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; done.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			ThreadPoolDemo.<span style="color: #006633;">jobs</span>.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>idx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href='http://vifix.cn/blog/wp-content/uploads/2009/03/threadpooldemo.zip'>Eclipse工程</a>，欢迎共同研究！</p>
]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/java-limited-task-amount-threads-pool.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java实现图像的高斯模糊滤镜</title>
		<link>http://vifix.cn/blog/java-gaussian-blur-filter.html</link>
		<comments>http://vifix.cn/blog/java-gaussian-blur-filter.html#comments</comments>
		<pubDate>Sat, 07 Mar 2009 14:41:40 +0000</pubDate>
		<dc:creator>Mac</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://vifix.cn/blog/?p=768</guid>
		<description><![CDATA[继续研究那本《Java动画、图形和极富客户端效果开发》，下午到现在搞的东西，高斯模糊滤镜，其原理（高斯分布）可以用一个矩阵方程来表示： 这个公式中，u是一个像素到中心的水平距离，v是一个像素到中心的垂直距离，σ（sigma）是标准偏差。标准偏差可以设置为任意的数，但是设置成半径的1/3会生成好的结果 看来要补充些线性代数的知识了 orz 我的高数课本都已经卖给二手书收购站了啊！ orzorzorz 那么滤镜核心部分的代码见内页，测试用的代码部分就不贴了，感兴趣的点击文后的压缩包看源代码（Eclipse工程）&#38;可执行JAR吧。 来自书本上的核心代码： package cn.vifix.uilab; &#160; import java.awt.image.ConvolveOp; import java.awt.image.Kernel; &#160; public class GaussianBlurFilter &#123; public static ConvolveOp getGaussianBlurFilter&#40;int radius, boolean horizontal&#41;&#123; if &#40;radius &#60; 1&#41;&#123; throw new IllegalArgumentException&#40;&#34;Radius must be &#62;= 1&#34;&#41;; &#125; &#160; int size = radius * 2 + 1; float&#91;&#93; data = new float&#91;size&#93;; &#160; float [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://vifix.cn/blog/wp-content/uploads/2009/03/gaussianblurfiltertest.jpg" rel="shadowbox[post-768];player=img;" title="gaussianblurfiltertest"><img class="alignnone size-medium wp-image-769" title="gaussianblurfiltertest" src="http://vifix.cn/blog/wp-content/uploads/2009/03/gaussianblurfiltertest-300x245.jpg" alt="gaussianblurfiltertest" width="300" height="245" /></a></p>
<p>继续研究那本<a onclick="pageTracker._trackPageview('/outgoing/www.douban.com/subject/3076198/?referer=');pageTracker._trackPageview('/outgoing/www.douban.com/subject/3076198/?referer=http://vifix.cn/blog/wp-admin/index.php');" href="http://www.douban.com/subject/3076198/" target="_blank">《Java动画、图形和极富客户端效果开发》</a>，下午到现在搞的东西，高斯模糊滤镜，其原理（高斯分布）可以用一个矩阵方程来表示：</p>
<blockquote><p>
<img src="http://vifix.cn/blog/wp-content/uploads/2009/03/gaussian-formula.gif" alt="gaussian-formula" title="gaussian-formula" width="433" height="112" class="alignnone size-full wp-image-774" /></p>
<p>这个公式中，u是一个像素到中心的水平距离，v是一个像素到中心的垂直距离，σ（sigma）是标准偏差。标准偏差可以设置为任意的数，但是设置成半径的1/3会生成好的结果
</p></blockquote>
<p>看来要补充些线性代数的知识了 orz</p>
<p>我的高数课本都已经卖给二手书收购站了啊！ orzorzorz</p>
<p>那么滤镜核心部分的代码见内页，测试用的代码部分就不贴了，感兴趣的点击文后的压缩包看源代码（Eclipse工程）&amp;可执行JAR吧。</p>
<p><span id="more-768"></span><br />
来自书本上的核心代码：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.vifix.uilab</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.image.ConvolveOp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.image.Kernel</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GaussianBlurFilter
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">ConvolveOp</span> getGaussianBlurFilter<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> radius, <span style="color: #000066; font-weight: bold;">boolean</span> horizontal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>radius <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Radius must be &gt;= 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> size <span style="color: #339933;">=</span> radius <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span>size<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">float</span> sigma <span style="color: #339933;">=</span> radius <span style="color: #339933;">/</span> 3.0f<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">float</span> twoSigmaSquare <span style="color: #339933;">=</span> 2.0f <span style="color: #339933;">*</span> sigma <span style="color: #339933;">*</span> sigma<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">float</span> sigmaRoot <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">sqrt</span><span style="color: #009900;">&#40;</span>twoSigmaSquare <span style="color: #339933;">*</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">PI</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">float</span> total <span style="color: #339933;">=</span> 0.0f<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>radius<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> radius<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">float</span> distance <span style="color: #339933;">=</span> i <span style="color: #339933;">*</span> i<span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> radius<span style="color: #339933;">;</span>
			data<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">exp</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>distance<span style="color: #339933;">/</span>twoSigmaSquare<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> sigmaRoot<span style="color: #339933;">;</span>
			total <span style="color: #339933;">+=</span> data<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> data.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/=</span> total<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003399;">Kernel</span> kernel <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>horizontal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			kernel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Kernel</span><span style="color: #009900;">&#40;</span>size,<span style="color: #cc66cc;">1</span>,data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			kernel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Kernel</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>,size,data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ConvolveOp</span><span style="color: #009900;">&#40;</span>kernel, <span style="color: #003399;">ConvolveOp</span>.<span style="color: #006633;">EDGE_NO_OP</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> GaussianBlurFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href="http://vifix.cn/blog/wp-content/uploads/2009/03/java-gaussian-blur-filter-test.zip">源代码（Eclipse工程）&amp;可执行JAR</a></p>
]]></content:encoded>
			<wfw:commentRss>http://vifix.cn/blog/java-gaussian-blur-filter.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
