<?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>pikopong &#187; programming</title>
	<atom:link href="http://www.pikopong.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pikopong.com/blog</link>
	<description>it&#039;s all about knowledge</description>
	<lastBuildDate>Tue, 13 Jul 2010 10:18:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to enable MySQL support in Qt SDK for Windows</title>
		<link>http://www.pikopong.com/blog/2010/04/11/how-to-enable-mysql-support-in-qt-sdk-for-windows/</link>
		<comments>http://www.pikopong.com/blog/2010/04/11/how-to-enable-mysql-support-in-qt-sdk-for-windows/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 08:38:30 +0000</pubDate>
		<dc:creator>amree</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[guides]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.pikopong.com/blog/?p=308</guid>
		<description><![CDATA[Download Qt SDK for Windows at Qt Software and install. Download MySQL Community Server (MSI Installer) from MySQL and install. Open Qt Command Prompt from the Start Menu Run these commands, modify it if you install your MySQL at different path. You just have to set the path to your MySQL library since Qt&#8217;s paths [...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>Download <strong>Qt SDK</strong> for Windows at <a title="http://qt.nokia.com/downloads" href="http://qt.nokia.com/downloads" target="_self">Qt Software</a> and install.</li>
<li>Download <strong>MySQL Community Server</strong> (MSI Installer) from <a title="http://dev.mysql.com/downloads/mysql/" href="http://dev.mysql.com/downloads/mysql/" target="_self">MySQL</a> and install.<br />
<img class="alignnone size-full wp-image-310" style="border: 0px initial initial;" title="mysql" src="http://www.pikopong.com/blog/wp-content/uploads/2010/04/20100411mysql.png" alt="mysql" width="503" height="377" /></li>
<li>Open <strong>Qt Command Prompt</strong> from the Start Menu<br />
<img class="alignnone" style="border: 1px solid black;" title="Start.Menu" src="http://www.pikopong.com/blog/wp-content/uploads/2010/04/20100411StartMenu_.png" alt="" width="409" height="90" /><br />
<a href="http://www.pikopong.com/blog/wp-content/uploads/2010/04/20100411StartMenu_.png" rel="lightbox[308]"></a></li>
<li>Run these commands, modify it if you install your MySQL at different path. You just have to set the path to your MySQL library since Qt&#8217;s paths will be set automatically based on your installation if you use <strong>Qt Command Prompt</strong>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">Setting up a MinGW/Qt only environment...
-- QTDIR <span style="color: #b1b100; font-weight: bold;">set</span> to C:\Qt\2010.02.1\qt
-- PATH <span style="color: #b1b100; font-weight: bold;">set</span> to C:\Qt\2010.02.1\qt\bin
-- Adding C:\Qt\2010.02.1\bin to PATH
-- Adding C:\WINDOWS\System32 to PATH
-- QMAKESPEC <span style="color: #b1b100; font-weight: bold;">set</span> to win32-g++
C:\Qt\2010.02.1\qt&gt;set <span style="color: #448844;">mySQLDIR</span>=C:\PROGRA~<span style="color: #cc66cc;">1</span>\MySQL\MYSQLS~<span style="color: #cc66cc;">1.1</span>
C:\Qt\2010.02.1\qt&gt;cd <span style="color: #33cc33;">%</span><span style="color: #448888;">QTDIR</span><span style="color: #33cc33;">%</span>\src\plugins\sqldrivers\mysql
C:\Qt\2010.02.1\qt&gt;qmake &quot;INCLUDEPATH+=<span style="color: #33cc33;">%</span><span style="color: #448888;">mySQLDIR</span><span style="color: #33cc33;">%</span>\include&quot; &quot;LIBS+=<span style="color: #33cc33;">%</span><span style="color: #448888;">mySQLDIR</span><span style="color: #33cc33;">%</span>\lib\opt\libmysql.lib&quot; -o Makefile mysql.pro
C:\Qt\2010.02.1\qt&gt;mingw32-make</pre></div></div>

</li>
<li>Create new project and put these codes to test your new plugin

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include</span>
<span style="color: #339900;">#include </span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    QCoreApplication a<span style="color: #008000;">&#40;</span>argc, argv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    qDebug<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> QSqlDatabase<span style="color: #008080;">::</span><span style="color: #007788;">drivers</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> a.<span style="color: #007788;">exec</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Make sure you have <strong>QtSql Module</strong> in your project configuration file <strong>(.pro file)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">QT += sql</pre></div></div>

<p>You’ll get these outputs showing that your Qt has supports for MySQL:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">(&quot;QSQLITE&quot;, &quot;QMYSQL3&quot;, &quot;QMYSQL&quot;, &quot;QODBC3&quot;, &quot;QODBC&quot;)</pre></div></div>

<p>What matters is that you have <strong>QMYSQL </strong>in the output.</li>
</ol>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.pikopong.com/blog/2009/07/22/how-to-enable-mysql-support-in-qt-sdk-for-linux/" title="How to enable MySQL support in Qt SDK for Linux">How to enable MySQL support in Qt SDK for Linux</a></li><li><a href="http://www.pikopong.com/blog/2010/07/13/install-tweetdeck-on-slackware-13-1-with-xfce/" title="Install TweetDeck on Slackware 13.1 with Xfce">Install TweetDeck on Slackware 13.1 with Xfce</a></li><li><a href="http://www.pikopong.com/blog/2010/05/18/streamyx-cool-uni-pack/" title="Streamyx Cool Uni Pack">Streamyx Cool Uni Pack</a></li><li><a href="http://www.pikopong.com/blog/2010/05/08/fix-dell-studio-xps-laptop-hanging-problem/" title="Fix Dell Studio XPS Laptop Hanging Problem">Fix Dell Studio XPS Laptop Hanging Problem</a></li><li><a href="http://www.pikopong.com/blog/2010/04/11/fixing-blank-help-in-qt-creator/" title="Fixing Blank Help in Qt Creator">Fixing Blank Help in Qt Creator</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.pikopong.com/blog/2010/04/11/how-to-enable-mysql-support-in-qt-sdk-for-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to enable MySQL support in Qt SDK for Linux</title>
		<link>http://www.pikopong.com/blog/2009/07/22/how-to-enable-mysql-support-in-qt-sdk-for-linux/</link>
		<comments>http://www.pikopong.com/blog/2009/07/22/how-to-enable-mysql-support-in-qt-sdk-for-linux/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 07:21:47 +0000</pubDate>
		<dc:creator>amree</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[guides]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://www.pikopong.com/blog/?p=231</guid>
		<description><![CDATA[Download Qt SDK for Linux/X11 at Qt Software. Install it anywhere you want, just make sure you remember the path. To build MySQL as a plugin, you need to know two other paths: Your MySQL header files directory. It should be something like this: decimal.h m_string.h my_dbug.h my_list.h my_sys.h mysql_embed.h mysqld_error.h sql_state.h typelib.h errmsg.h my_alloc.h [...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>Download Qt SDK for Linux/X11 at <a href="http://www.qtsoftware.com/downloads">Qt Software</a>.</li>
<li>Install it anywhere you want, just make sure you remember the path.</li>
<li>To build MySQL as a plugin, you need to know two other paths:
<ul>
<li>Your MySQL header files directory. It should be something like this:

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">decimal.h   m_string.h      my_dbug.h    my_list.h        my_sys.h     mysql_embed.h    mysqld_error.h  sql_state.h        typelib.h
errmsg.h    my_alloc.h      my_dir.h     my_net.h         my_xml.h     mysql_time.h     raid.h          sslopt-case.h
keycache.h  my_attribute.h  my_getopt.h  my_no_pthread.h  mysql.h      mysql_version.h  readline.h      sslopt-longopts.h
m_ctype.h   my_config.h     my_global.h  my_pthread.h     mysql_com.h  mysqld_ername.h  sql_common.h    sslopt-vars.h</pre></div></div>

</li>
<li>Your MySQL lib files directory. Something like this:

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">libdbug.a    libmyisammrg.a      libmysqlclient.so@         libmysqlclient_r.a    libmysqlclient_r.so.15@      libmysys.a
libheap.a    libmysqlclient.a    libmysqlclient.so.15@      libmysqlclient_r.la*  libmysqlclient_r.so.15.0.0*  libvio.a
libmyisam.a  libmysqlclient.la*  libmysqlclient.so.15.0.0*  libmysqlclient_r.so@  libmystrings.a</pre></div></div>

</li>
</ul>
</li>
<li>For Slackware 12.2, it should be:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">/usr/include/mysql	# MySQL header directory
/usr/lib/mysql		# MySQL libraries</pre></td></tr></table></div>

</li>
<li>Go to your Qt SDK installation directory, mine is: <b>/opt/qtsdk-2009.03/</b>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>qtsdk-<span style="color: #000000;">2009.03</span><span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> qt<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>sqldrivers<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #666666; font-style: italic;"># Replace all the path based on your computer environment. </span>
<span style="color: #666666; font-style: italic;"># Make sure 'qmake' can be run from anywhere or you'd have to specify the full path for it.</span>
qmake <span style="color: #660033;">-o</span> Makefile <span style="color: #ff0000;">&quot;INCLUDEPATH+=/usr/include/mysql&quot;</span> <span style="color: #ff0000;">&quot;LIBS+=-L/usr/lib/mysql -lmysqlclient&quot;</span> mysql.pro
<span style="color: #c20cb9; font-weight: bold;">make</span></pre></td></tr></table></div>

</li>
<li>You should have new files created for you:

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Makefile
README
libqsqlmysql.so*
main.cpp
main.o
moc_qsql_mysql.cpp
moc_qsql_mysql.o
mysql.pro
qsql_mysql.moc
qsql_mysql.o</pre></div></div>

</li>
<li>Copy MySQL plugin to your Qt&#8217;s plugins directory,

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> libqsqlmysql.so <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>qtsdk-<span style="color: #000000;">2009.03</span><span style="color: #000000; font-weight: bold;">/</span>qt<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>sqldrivers</pre></div></div>

</li>
<li>Create new project and put these codes to test your new plugin

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;QCoreApplication&gt;</span>
<span style="color: #339933;">#include &lt;QtSql&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    QCoreApplication a<span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">,</span> argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    qDebug<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> QSqlDatabase<span style="color: #339933;">::</span><span style="color: #202020;">drivers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> a.<span style="color: #202020;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You&#8217;ll get these outputs showing that your Qt has supports for MySQL</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">(&quot;QSQLITE&quot;, &quot;QMYSQL3&quot;, &quot;QMYSQL&quot;)</pre></div></div>

</li>
</ol>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.pikopong.com/blog/2010/04/11/how-to-enable-mysql-support-in-qt-sdk-for-windows/" title="How to enable MySQL support in Qt SDK for Windows">How to enable MySQL support in Qt SDK for Windows</a></li><li><a href="http://www.pikopong.com/blog/2010/07/13/install-tweetdeck-on-slackware-13-1-with-xfce/" title="Install TweetDeck on Slackware 13.1 with Xfce">Install TweetDeck on Slackware 13.1 with Xfce</a></li><li><a href="http://www.pikopong.com/blog/2010/02/21/configuring-linux-to-work-with-nvidia-hybrid-sli-technology/" title="Configuring Linux to work with NVIDIA Hybrid SLI Technology">Configuring Linux to work with NVIDIA Hybrid SLI Technology</a></li><li><a href="http://www.pikopong.com/blog/2008/09/10/printing-to-pdf-using-cups/" title="Printing to PDF Using CUPS">Printing to PDF Using CUPS</a></li><li><a href="http://www.pikopong.com/blog/2010/05/18/streamyx-cool-uni-pack/" title="Streamyx Cool Uni Pack">Streamyx Cool Uni Pack</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.pikopong.com/blog/2009/07/22/how-to-enable-mysql-support-in-qt-sdk-for-linux/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reading Mifare 1K Card using Java in Linux</title>
		<link>http://www.pikopong.com/blog/2009/02/11/reading-mifare-1k-card-using-java-in-linux/</link>
		<comments>http://www.pikopong.com/blog/2009/02/11/reading-mifare-1k-card-using-java-in-linux/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 08:27:32 +0000</pubDate>
		<dc:creator>amree</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mifare]]></category>

		<guid isPermaLink="false">http://www.pikopong.com/blog/?p=179</guid>
		<description><![CDATA[At the end of last year, my friends and I were tasked to read our staff card. The main objective was pretty simple, to create a library where we don&#8217;t have to depend on the vendor everytime we want to read our own staff card and the library should also be working on Windows and [...]]]></description>
			<content:encoded><![CDATA[<p>At the end of last year, my friends and I were tasked to read our staff card. The main objective was pretty simple, to create a library where we don&#8217;t have to depend on the vendor everytime we want to read our own staff card and the library should also be working on Windows and Linux (that means Java to us).</p>
<p><strong>Working Environment</strong></p>
<ol>
<li><a href="http://slackware.com">Slackware 12.2</a></li>
<li><a href="http://java.sun.com/javase/downloads/index.jsp">JDK 6 Update 11</a></li>
<li><a href="http://www.netbeans.org/">Netbeans 6.5</a></li>
<li><a href="http://libusb.sourceforge.net/download.html">libusb 0.1.12</a></li>
<li><a href="http://www.musclecard.com/middle.html">Windows Binary of JPC/SC Java API 0.8.0</a></li>
<li>Your contactless smartcard reader&#8217;s driver</li>
</ol>
<p><strong>The Reader</strong></p>
<p>Let&#8217;s talk about the reader first. You need to get a reader that supports Mifare 1K card (obviously) which is to be exact a reader that supports ISO14443A/B or ISO15693 (contactless standard). Now, to make your life easier, you should get a reader that can read contactless standards compliant contactless using the same framework as ISO7816 compliant contact cards. In simple terms, you can use APDU to get the data that you want.</p>
<p>So, for this little project, we use <a href="http://www.omnikey.com/?id=products&#038;tx_okprod_pi1[product]=41">OMNIKEY CardMan 5321 RFID reader</a>. It has everything that we need, Linux/Windows supports, APDU calls to the contactless card, it even comes a contact card&#8217;s reader too and of course, it&#8217;s kinda cheap.</p>
<p>Make sure you choose the correct reader or you won&#8217;t be able to use this tutorial. Some reader requires you to read using their own specific code, which means you can only use the code you wrote on that particular reader, unlike readers that support APDU calls to contactless cards.</p>
<p><strong>The Card</strong></p>
<p>1024 byte memory is organised as sixteen sectors, each of which is made up of four blocks and each block is 16 bytes long. The first block in the memory (Block 0) is read-only and is set in the factory to contain the four-byte serial number (UID), check bytes and manufacturers data. The last block of each sector (Blocks 3, 7, 11, 15……59, 63) is the Sector Trailer Block which contains the two security Key codes (KeyA and KeyB) and the Access bits that define how the sector can be accessed.</p>
<p>Taking into account the Serial Number/Manufacturers Block and the Sector Trailer Blocks then there are 752 bytes of free memory for user storage. For all Read and Write operations the Mifare card memory is addressed by Block number (in hexadecimal format).</p>
<p><strong>The Installation</strong></p>
<p>Before you begin coding your codes, you need to make sure your environment is ready for you. To do that, just follow steps from my <a href="http://www.pikopong.com/blog/2008/01/06/reading-mykad-in-linux/">previous tutorial</a> which is pretty much the same from Step 1 tuntill Step 11. For Step 8, you should install the driver based on your reader. OMNIKEY 5321 users can get your driver <a href="http://www.omnikey.com/index.php?id=69">here</a>.</p>
<p><strong>General Steps to Read the Card</strong></p>
<ol>
<li>Load Mifare key</li>
<li>Authenticate</li>
<li>Read</li>
</ol>
<p>Looks simple right ?</p>
<p><strong>The Code</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">my.husm.mifare</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.nio.ByteBuffer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.smartcardio.Card</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.smartcardio.CardChannel</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.smartcardio.CardException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.smartcardio.CardTerminal</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.smartcardio.TerminalFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
*
* @author mree
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Read <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> Read<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
           CardTerminal terminal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
           <span style="color: #666666; font-style: italic;">// show the list of available terminals</span>
           TerminalFactory factory <span style="color: #339933;">=</span> TerminalFactory.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           List<span style="color: #339933;">&lt;</span>CardTerminal<span style="color: #339933;">&gt;</span> terminals <span style="color: #339933;">=</span> factory.<span style="color: #006633;">terminals</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
           <span style="color: #003399;">String</span> readerName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><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: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> terminals.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
               readerName <span style="color: #339933;">=</span> terminals.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>
                                   terminals.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
               <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>readerName.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;01&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                   terminal <span style="color: #339933;">=</span> terminals.<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: #009900;">&#125;</span>
           <span style="color: #009900;">&#125;</span>
&nbsp;
           <span style="color: #666666; font-style: italic;">// Establish a connection with the card</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;Waiting for a card..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           terminal.<span style="color: #006633;">waitForCardPresent</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
           Card card <span style="color: #339933;">=</span> terminal.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;T=0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           CardChannel channel <span style="color: #339933;">=</span> card.<span style="color: #006633;">getBasicChannel</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;">// Start with something simple, read UID, kinda like Hello World!</span>
           <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> baReadUID <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
           baReadUID <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xCA, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x00,
                                  <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x00, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x00<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
           <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;UID: &quot;</span> <span style="color: #339933;">+</span> send<span style="color: #009900;">&#40;</span>baReadUID, channel<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #666666; font-style: italic;">// If successfull, the output will end with 9000</span>
&nbsp;
           <span style="color: #666666; font-style: italic;">// OK, now, the real work</span>
           <span style="color: #666666; font-style: italic;">// Get Serial Number</span>
           <span style="color: #666666; font-style: italic;">// Load key</span>
           <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> baLoadKey <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
           baLoadKey <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x82, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x20,
                                     <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x1A, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x06, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF,
                                     <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF,
                                     <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
           <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;LOAD KEY: &quot;</span> <span style="color: #339933;">+</span> send<span style="color: #009900;">&#40;</span>baLoadKey, channel<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #666666; font-style: italic;">// If successfull, will output 9000</span>
&nbsp;
           <span style="color: #666666; font-style: italic;">// Authenticate</span>
           <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> baAuth <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
           baAuth <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x88, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x00,
                               <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x09, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x60, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x00<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
           <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;AUTHENTICATE: &quot;</span> <span style="color: #339933;">+</span> send<span style="color: #009900;">&#40;</span>baAuth, channel<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #666666; font-style: italic;">// If successfull, will output 9000</span>
&nbsp;
           <span style="color: #666666; font-style: italic;">// Read Serial</span>
           <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> baRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
           baRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xFF, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0xB0, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x00,
                               <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x09, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> 0x10<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
           <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;READ: &quot;</span> <span style="color: #339933;">+</span> send<span style="color: #009900;">&#40;</span>baRead, channel<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #666666; font-style: italic;">// If successfull, the output will end with 9000</span>
&nbsp;
       <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: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> send<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> cmd, CardChannel channel<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
       <span style="color: #003399;">String</span> res <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> baResp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
       ByteBuffer bufCmd <span style="color: #339933;">=</span> ByteBuffer.<span style="color: #006633;">wrap</span><span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       ByteBuffer bufResp <span style="color: #339933;">=</span> ByteBuffer.<span style="color: #006633;">wrap</span><span style="color: #009900;">&#40;</span>baResp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #666666; font-style: italic;">// output = The length of the received response APDU</span>
       <span style="color: #000066; font-weight: bold;">int</span> output <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
           output <span style="color: #339933;">=</span> channel.<span style="color: #006633;">transmit</span><span style="color: #009900;">&#40;</span>bufCmd, bufResp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>CardException 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>
&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> output<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           res <span style="color: #339933;">+=</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%02X&quot;</span>, baResp<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #666666; font-style: italic;">// The result is formatted as a hexadecimal integer</span>
       <span style="color: #009900;">&#125;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">return</span> res<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</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> Read<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></pre></td></tr></table></div>

<p>Make sure you set the right connection protocol at line 43, as pointed out by <a href="http://www.pikopong.com/blog/2009/02/11/reading-mifare-1k-card-using-java-in-linux/comment-page-1/#comment-673">Animesh</a>.</p>
<p>These codes use Java API to read the smartcard, but I suggest you use the <a href="http://www.musclecard.com/middle.html">jpcsc</a> library, it&#8217;s much more robust when it comes to error handling. These codes will generate error response, so, you need to change the commands based on your smartcard.</p>
<p><strong>The APDU Commands</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="text" style="font-family:monospace;"># Load Key
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|   FF   |   82   |   20   |   1A   |   06   |   FF   |   FF   |   FF   |   FF   |   FF   |   FF   |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|   1    |   2    |   3    |   4    |   5    |   6    |   7    |   8    |   9    |   10   |   11   |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
&nbsp;
1   = CLA : (Fixed)
2   = INS : (Fixed)
3   = P1  : (Fixed)
4   = P2  : Key type, could be 00, 1A, 1B
5   = LC  : Key length, usually 6 for 6 byte
6   = XX  : ----+
7   = XX  :     |
8   = XX  :     |__ The Key (eg. FF FF FF FF FF FF)
9   = XX  :     |
10  = XX  :     |
11  = XX  : ----+
&nbsp;
# Autenthicate
+--------+--------+--------+--------+--------+--------+
|   FF   |   88   |   00   |   01   |   60   |   00   |
+--------+--------+--------+--------+--------+--------+
|   1    |   2    |   3    |   4    |   5    |   6    |
+--------+--------+--------+--------+--------+--------+
&nbsp;
1   = CLA : (Fixed)
2   = INS : (Fixed)
3   = P1  : (Fixed)
4   = P2  : Block Number you want to authenticate in Hex
5   = P3  : Mifare Block Number LSB
6   = XX  : Key type, could be 00, 1A, 1B
&nbsp;
# Read
+--------+--------+--------+--------+--------+
|   FF   |   B0   |   00   |   01   |   10   |
+--------+--------+--------+--------+--------+
|   1    |   2    |   3    |   4    |   5    |
+--------+--------+--------+--------+--------+
&nbsp;
1   = CLA : (Fixed)
2   = INS : (Fixed)
3   = P1  : (Fixed)
4   = P2  : Block Number that you want to read in Hex
5   = Le  : (Fixed)</pre></td></tr></table></div>

<p>So, that&#8217;s it, good luck trying !</p>
<div class="box-icon-yellow">This project has been abandoned. Thus, I may or may not be able to help if you encountered any problems but thanks for viewing :)</div>
<p></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.pikopong.com/blog/2008/09/10/printing-to-pdf-using-cups/" title="Printing to PDF Using CUPS">Printing to PDF Using CUPS</a></li><li><a href="http://www.pikopong.com/blog/2008/09/09/java-printing-fix-for-linux-with-cups/" title="Java Printing Fix for Linux with CUPS">Java Printing Fix for Linux with CUPS</a></li><li><a href="http://www.pikopong.com/blog/2010/07/13/install-tweetdeck-on-slackware-13-1-with-xfce/" title="Install TweetDeck on Slackware 13.1 with Xfce">Install TweetDeck on Slackware 13.1 with Xfce</a></li><li><a href="http://www.pikopong.com/blog/2010/02/21/configuring-linux-to-work-with-nvidia-hybrid-sli-technology/" title="Configuring Linux to work with NVIDIA Hybrid SLI Technology">Configuring Linux to work with NVIDIA Hybrid SLI Technology</a></li><li><a href="http://www.pikopong.com/blog/2009/07/22/how-to-enable-mysql-support-in-qt-sdk-for-linux/" title="How to enable MySQL support in Qt SDK for Linux">How to enable MySQL support in Qt SDK for Linux</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.pikopong.com/blog/2009/02/11/reading-mifare-1k-card-using-java-in-linux/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Auto Resize JTable Column Width</title>
		<link>http://www.pikopong.com/blog/2008/08/13/auto-resize-jtable-column-width/</link>
		<comments>http://www.pikopong.com/blog/2008/08/13/auto-resize-jtable-column-width/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 06:49:59 +0000</pubDate>
		<dc:creator>amree</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[swing]]></category>

		<guid isPermaLink="false">http://www.pikopong.com/blog/?p=88</guid>
		<description><![CDATA[This code should resize your JTable column width based on the contents of the header and data. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 [...]]]></description>
			<content:encoded><![CDATA[<p>This code should resize your JTable column width based on the contents of the header and data.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">JTable</span> autoResizeColWidth<span style="color: #009900;">&#40;</span><span style="color: #003399;">JTable</span> table, <span style="color: #003399;">DefaultTableModel</span> model<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        table.<span style="color: #006633;">setAutoResizeMode</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">JTable</span>.<span style="color: #006633;">AUTO_RESIZE_OFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        table.<span style="color: #006633;">setModel</span><span style="color: #009900;">&#40;</span>model<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">int</span> margin <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><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: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> table.<span style="color: #006633;">getColumnCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #000066; font-weight: bold;">int</span>                     vColIndex <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
            <span style="color: #003399;">DefaultTableColumnModel</span> colModel  <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">DefaultTableColumnModel</span><span style="color: #009900;">&#41;</span> table.<span style="color: #006633;">getColumnModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">TableColumn</span>             col       <span style="color: #339933;">=</span> colModel.<span style="color: #006633;">getColumn</span><span style="color: #009900;">&#40;</span>vColIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span>                     width     <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Get width of column header</span>
            <span style="color: #003399;">TableCellRenderer</span> renderer <span style="color: #339933;">=</span> col.<span style="color: #006633;">getHeaderRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>renderer <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>
                renderer <span style="color: #339933;">=</span> table.<span style="color: #006633;">getTableHeader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDefaultRenderer</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: #003399;">Component</span> comp <span style="color: #339933;">=</span> renderer.<span style="color: #006633;">getTableCellRendererComponent</span><span style="color: #009900;">&#40;</span>table, col.<span style="color: #006633;">getHeaderValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span>, <span style="color: #000066; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            width <span style="color: #339933;">=</span> comp.<span style="color: #006633;">getPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">width</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Get maximum width of column data</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> r <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> r <span style="color: #339933;">&lt;</span> table.<span style="color: #006633;">getRowCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> r<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                renderer <span style="color: #339933;">=</span> table.<span style="color: #006633;">getCellRenderer</span><span style="color: #009900;">&#40;</span>r, vColIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                comp     <span style="color: #339933;">=</span> renderer.<span style="color: #006633;">getTableCellRendererComponent</span><span style="color: #009900;">&#40;</span>table, table.<span style="color: #006633;">getValueAt</span><span style="color: #009900;">&#40;</span>r, vColIndex<span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span>, <span style="color: #000066; font-weight: bold;">false</span>,
                        r, vColIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                width <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">max</span><span style="color: #009900;">&#40;</span>width, comp.<span style="color: #006633;">getPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Add margin</span>
            width <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> margin<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Set the width</span>
            col.<span style="color: #006633;">setPreferredWidth</span><span style="color: #009900;">&#40;</span>width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">DefaultTableCellRenderer</span><span style="color: #009900;">&#41;</span> table.<span style="color: #006633;">getTableHeader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDefaultRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setHorizontalAlignment</span><span style="color: #009900;">&#40;</span>
            <span style="color: #003399;">SwingConstants</span>.<span style="color: #006633;">LEFT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// table.setAutoCreateRowSorter(true);</span>
        table.<span style="color: #006633;">getTableHeader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setReorderingAllowed</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> table<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Must pass the model</span>
<span style="color: #003399;">DefaultTableModel</span> model <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">DefaultTableModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jTable <span style="color: #339933;">=</span> autoResizeColWidth<span style="color: #009900;">&#40;</span>jTable, model<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Tested on JRE v5 and JRE v6</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.pikopong.com/blog/2008/05/11/printing-to-zebra-s4m-using-java-and-zpl-ii/" title="Printing to Zebra S4M Using Java and ZPL II">Printing to Zebra S4M Using Java and ZPL II</a></li><li><a href="http://www.pikopong.com/blog/2008/04/14/automatically-sign-jars-using-ant-and-bash/" title="Automatically Sign JARs using Ant and Bash">Automatically Sign JARs using Ant and Bash</a></li><li><a href="http://www.pikopong.com/blog/2009/02/11/reading-mifare-1k-card-using-java-in-linux/" title="Reading Mifare 1K Card using Java in Linux">Reading Mifare 1K Card using Java in Linux</a></li><li><a href="http://www.pikopong.com/blog/2008/09/10/printing-to-pdf-using-cups/" title="Printing to PDF Using CUPS">Printing to PDF Using CUPS</a></li><li><a href="http://www.pikopong.com/blog/2008/09/09/java-printing-fix-for-linux-with-cups/" title="Java Printing Fix for Linux with CUPS">Java Printing Fix for Linux with CUPS</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.pikopong.com/blog/2008/08/13/auto-resize-jtable-column-width/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Reading MyKad using Visual Basic</title>
		<link>http://www.pikopong.com/blog/2008/05/28/reading-mykad-using-visual-basic/</link>
		<comments>http://www.pikopong.com/blog/2008/05/28/reading-mykad-using-visual-basic/#comments</comments>
		<pubDate>Tue, 27 May 2008 16:32:38 +0000</pubDate>
		<dc:creator>amree</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[mykad]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.pikopong.com/blog/?p=69</guid>
		<description><![CDATA[It seems that a lot of people asking how to read MyKad using Visual Basic, but since I don&#8217;t have the slightest idea on how to code in VB, I started to search around. To my surprise, Xenon (the one who reverse engineered MyKad to get the APDU) actually wrote a small application in VB [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that a lot of people asking how to read MyKad using Visual Basic, but since I don&#8217;t have the slightest idea on how to code in VB, I started to search around. To my surprise, <a title="Xenon's profile in Lowyat.NET" href="http://forum.lowyat.net/index.php?showuser=22825" target="_blank">Xenon</a> (the one who reverse engineered MyKad to get the APDU) actually wrote a small application in VB to read MyKad. So, for those who can&#8217;t afford to buy SDK from commercial company, you can download the code here for FREE !</p>
<p><a title="VB Application to read MyKad" rel="lightbox[pics69]" href="http://www.pikopong.com/blog/wp-content/uploads/2008/05/mykad-vb.jpg"><img class="attachment wp-att-70" src="http://www.pikopong.com/blog/wp-content/uploads/2008/05/mykad-vb.thumbnail.jpg" alt="VB Application to read MyKad" width="200" height="184" /></a><br />
<br clear="all"><br />
<a title="View the discussion" href="http://forum.lowyat.net/topic/441431/+0" target="_blank">View the discussion</a><br />
<a title="Code download" href="http://forum.lowyat.net/index.php?s=b6af87093aa7ba9a66042af53c1ebb49&amp;act=Attach&amp;type=post&amp;id=220777" target="_blank">Download the code</a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.pikopong.com/blog/2008/01/05/reading-mykad-in-windows/" title="Reading MyKad in Windows">Reading MyKad in Windows</a></li><li><a href="http://www.pikopong.com/blog/2008/01/06/reading-mykad-in-linux/" title="Reading MyKad in Linux">Reading MyKad in Linux</a></li><li><a href="http://www.pikopong.com/blog/2010/05/08/fix-dell-studio-xps-laptop-hanging-problem/" title="Fix Dell Studio XPS Laptop Hanging Problem">Fix Dell Studio XPS Laptop Hanging Problem</a></li><li><a href="http://www.pikopong.com/blog/2010/04/11/how-to-enable-mysql-support-in-qt-sdk-for-windows/" title="How to enable MySQL support in Qt SDK for Windows">How to enable MySQL support in Qt SDK for Windows</a></li><li><a href="http://www.pikopong.com/blog/2010/04/08/fix-windows-7-with-eset-nod32-antivirus-memory-leak/" title="Fix Windows 7 with Eset NOD32 Antivirus = Memory Leak">Fix Windows 7 with Eset NOD32 Antivirus = Memory Leak</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.pikopong.com/blog/2008/05/28/reading-mykad-using-visual-basic/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
