Thursday, 12 December 2013

Generating Barcodes In Python

There are almost 2.7 million searches related to the term “barcode” each and every month on Google.
 It’s no wonder that barcoding is such a large topic for small businesses. Can you think of anything in this day and age that doesn’t have a barcode?
Barcodes have revolutionized the way we do business today. They allow you to run your small business more accurately and efficiently, getting you ready for growth. You’ll look more professional too!
Programmers these days are required to know just enough about barcodes in order to implement any serious shop management system or product identification system. However the barcode specification spans many pages. This means, it's impractical for any programmer to fully understand how barcodes work behind the scenes before being able to generate them from their codes.

There are a lot of software libraries out there to help in the generation and scanning of barcodes. For example, the ZXing library is a suite of java libraries for working with barcodes from the Android platform to JSE and JEE (Servlet Containers).
There is also Barcode4J, a freely available software library for generating and scanning barcodes in java.

Scanning of barcodes in a shop management system is actually a quick replacement for the keyboard. That is, instead of searching products by typing lengthy product codes into the software, a barcode scanner simply scans the barcode image and feeds the product code into the software for searching.

Of all the libraries around, I choose ReportLab, an amazingly free python package for generating PDFs. Aside it's extensive support for PDFs, it generates all kinds of barcodes including QRCodes.

The procedure for installing ReportLab is clearly laid out on their site.

Now with ReportLab installed, let's get our fingers dirty with some python codes
In our first example we will generate a simple PDF document showing "Hello World"
Open up your text editor and type in the following codes

from reportlab.pdfgen import canvas

c = canvas.Canvas("hello.pdf")
c.drawString(100,500,"Hello World")

After executing this python code, you should see a file, "hello.pdf" in your current working directory. Open it with any PDF reader and say a big thanks to the ReportLab team for such a fantastic offer.

The details of how this piece of code works is spelt out in the ReportLab documentation.

Now let's generate some simple barcode. The details of the different types of barcodes are out of this post's scope but in this example we will be generating a EAN13 barcode, an european standard barcode format for most products in shop. The ReportLab library is capable of generating all other kinds of barcodes. Just read the fu*king manual.

from import eanbc
from import Drawing
from reportlab.pdfgen import canvas
from import renderPDF

#Setting up canvas for pdf
c = canvas.Canvas("barcode.pdf")
code_number = "1234567899087" #This is the number for which the barcode is to be generated

#Generating a EAN13 code
barcode = eanbc.Ean13BarcodeWidget(code_number)

#Drawing generated barcode onto pdf
drawing = Drawing(50,10)

This is all there's to generating and saving a barcode to a PDF. Applying your own logic, you should be able to generate countless unique barcodes, print them out and stick them on your products for identification.

Lemme know of your questions by commenting.

Happy Coding!!!

Tuesday, 5 November 2013

Switching Android Activities : Fade Out - Fade In Animation

In this post, I will show you how to implement a basic slide-out-slide-in transition animation when switching between Activities in an Android application.

Animation in Android can be implemented either in Java code or XML markup. In this post we will use the latter approach. A similar post will be published on implementing animations in Java code.

We are going to make some assumptions. We will assume that we have 2 activity classes, ActivityA and ActivityB. Now when a button is clicked on ActivityA, we want it to slide out and ActivityB will slide in.

To define the animations in xml, create a file call fade_in.xml in the anim directory found in the res folder and paste the following code.

NOTE : The filename could be any valid android resource name

CODE FOR slide_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="">
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500" />
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="100" android:startOffset="200" />


Create another file called fade_out.xml in the anim directory found in the res folder of your project and paste in the following code. Again, the name of this file could be any valid android resource name.

CODE FOR slide_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="">
    <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="100" android:startOffset="200" />


<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500" />

The above code means the X position of the sliding Activity should change from -100 to 0 in 500 milliseconds.

<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="100" android:startOffset="200" />
The above code changes the transparency of the sliding Activity from 1 to 0.

To show ActivityB following an event in ActivityA and apply the defined animations use the following code

startActivity(new Intent(ActivityA.this,ActivityB.class));
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

If you don't understand anything just write it in a comment and get it answered.

Saturday, 2 November 2013

Africa Reborn

Many Africans want to be the Bill Gate or Zuckerberg or Steve Jobs etc of Africa. How about someone in America wanting to be the next Kofi Mensah or Araba Atta or the next you of America. Why habour such small dreams in our empty big heads. We proudly walk in town making noise about degrees. Degrees we earned cheaply by sitting in classes with equally empty headed brothers. Degrees we earned by cramming books into our heads without any practical experiences. Degrees in universities where a 4th year physics student can't even identify a common resistor or diode.
Even without working hard, we have accepted defeat. We never want the truth to be spoken and when someone does, we call him arrogant and braggy. We can't even distinguish between confidence and arrogance. Worst of it all, we proudly call them "too known". What on earth is the word "too known" supposed to mean. From childhood we are taught not to fight for what's right. We shun teamwork and welcome the I want it all attitude. This is what is killing us. Corruption all over. Someone is willing to gimme some money I haven't worked for and he sees nothing wrong with it. I will put his sorry ass in jail soon. I stand for seriousness and reality. I dont live in a utopian world where all is fantastic. I believe in the ups and downs in life. This is what many are trying to skip.
You think because you have a crappy degree you should drive that limousine and live in a mansion? The world has changed and so should you. The wealth of this world is no more for academicians. Its for problem solvers.
Renew your mind and have a better life. Dont let people's thought about you affect your thought process. Be bold and live by faith and action. Life is never a war. Everything in life will be a struggle to you if you see it as a war. Let's get real oo dear Africans. Its a blessing to be black. I see a serious Africa rising and the noise making Africa sinking. Where will you be during this revolution. If you do well, we will praise you. Godwin Amefia, congrats! Keep going and never be a noise maker. Rasheeda Yehuza keep championing your course. The journey is the reward.
Yes, am a HATER, hater of CRAP.