Categories
code linux programming Solutions tips

Reading a directory in C++ (Win32 & Posix)

Just a snippet of code if someone out there needs it. I keep coming back to it, so I thought why not share it…

#include <iostream>
#ifndef WIN32
#include <dirent.h>
#endif
using namespace std;
void open_imgs_dir(char* dir_name, std::vector<std::string>& file_names) {
	if (dir_name == NULL) {
		return;
	}
	string dir_name_ = string(dir_name);
	vector<string> files_;
#ifndef WIN32
//open a directory the POSIX way
	DIR *dp;
	struct dirent *ep;
	dp = opendir (dir_name);
	if (dp != NULL)
	{
		while (ep = readdir (dp)) {
			if (ep->d_name[0] != '.')
				files_.push_back(ep->d_name);
		}
		(void) closedir (dp);
	}
	else {
		cerr << ("Couldn't open the directory");
		return;
	}
#else
//open a directory the WIN32 way
	HANDLE hFind = INVALID_HANDLE_VALUE;
	WIN32_FIND_DATA fdata;
	if(dir_name_[dir_name_.size()-1] == '\\' || dir_name_[dir_name_.size()-1] == '/') {
		dir_name_ = dir_name_.substr(0,dir_name_.size()-1);
	}
	hFind = FindFirstFile(string(dir_name_).append("\\*").c_str(), &fdata);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (strcmp(fdata.cFileName, ".") != 0 &&
				strcmp(fdata.cFileName, "..") != 0)
			{
				if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					continue; // a diretory
				}
				else
				{
					files_.push_back(fdata.cFileName);
				}
			}
		}
		while (FindNextFile(hFind, &fdata) != 0);
	} else {
		cerr << "can't open directory\n";
		return;
	}
	if (GetLastError() != ERROR_NO_MORE_FILES)
	{
		FindClose(hFind);
		cerr << "some other error with opening directory: " << GetLastError() << endl;
		return;
	}
	FindClose(hFind);
	hFind = INVALID_HANDLE_VALUE;
#endif
	file_names.clear();
	file_names = files_;
}

Enjoy
Roy.

Categories
linux Raspberry Pi Recommended school Solutions tips

The Raspberry Pi is Here

A few months back I placed an order for a raspberry pi. For those who don’t know what it is, it is a really cool project which is basically a computer for 35$ (Shipping for me almost doubled it, but that’s to be expected). It is a board, which as 256MB Ram, SD-Card slot, 2 USB Slots, an RCA Slot for analog video, and a headphone jack for analog audio.
It is originally a project for schools, to help today’s kids get started with (python, but not only) programming.
To be exact with what the project guys are describing it:
The Raspberry Pi is a credit-card sized computer board that plugs into a TV and a keyboard. It’s a miniature ARM-based PC which can be used for many of the things that a desktop PC does, like spreadsheets, word-processing and games. It also plays High-Definition video.
Here are some FAQs

The OS of this board is stored on an SD Card. I have bought a class 10 16gb SD Card off of eBay for this purpose.
So few days ago, the board arrived! I finally found myself playing with it, and it’s so much fun
Here are some common suggestions for usages:

Categories
code linux python Recommended Software Solutions video Website

Download all your Last.fm loved tracks in two simple steps

I’m a fan of Last.fm online radio, and I have a habit of marking every good song that I hear as a “loved track”. Over the years I got quite a list, and so I decided to turn it into my jogging playlist. But for that, I need all the songs downloaded to my computer so I can put them on my mobile. While Last.fm does link to Amazon for downloading all the loved songs for pay, I’m going to walk the fine moral line here and suggest how you can download every song from existing free YouTube videos.
If it really bothers you, think of it as if I created a YouTube playlist and now I’m using my data plan to stream the songs off YT itself..
Moral issues resolved, we can move on to the scripting.
Update (4/27/12): youtube-dl.py has moved: https://github.com/rg3/youtube-dl/, and also added a very neat –extract-audio option so you can get the songs in audio right away (it basically does a conversion in a second step).

Categories
ffmpeg linux Recommended tips video

How to rotate a video using MEncoder and FFmpeg and live to tell the tale

Hi
I’d like to share a quick tip on rotating video files.
I’m always frustrated with taking videos with my phone. Single handedly it’s easiest to do it when the phone is upright and not in landscape mode. But the files are always saved in landscape mode, which makes them rotated when you watch.
Although there are plenty of GUI software to do it, using the command line is faster and can also be batched!

Categories
Android code graphics Java linux Mobile phones programming Solutions

Some things I learned about Android's Frame animation

Hi
Just a quick share of lessons learned about Android’s Frame-by-Frame animations. Some of the functionality is poorly documented, as many people point out, so the web is the only place for answers. Having looked for some answers to these questions and couldn’t find any – here’s what I found out myself.
Update [2/3/11]: A new post on this topic gives a more broad view of my experience.

Categories
code Java linux programming school video Website

SmartHome – Embedded computing course project

Hi
In the past few weeks I have been working hard at a few projects for end-of-term at Uni. One of the projects is what I called “SmartHome”, for Embedded computing [link] course, is a home monitoring [link] application. In the course the students were given an LPC2148 arm7-MCU (NXP) based education board, implemented by Embedded Artists [link]. My partner Gil and I decided to work with ZigBee extension modules [link] to enable remote communication.
Here are the steps we took to bring this project to life.

Categories
gui linux programming qt Uncategorized video

Qt & OpenCV combined for face detecting QWidgets

As my search for the best platform to roll-out my new face detection concept continues, I decided to give ol’ Qt framework a go.
I like Qt. It’s cross-platform, a clear a nice API, straightforward, and remindes me somewhat of Apple’s Cocoa.
My intention is to get some serious face detection going on mobile devices. So that means either the iPhone, which so far did a crummy job performance-wise, or some other mobile device, preferably linux-based.
This led me to the decision to go with Qt. I believe you can get it to work on any linux-ish platform (limo, moblin, android), and since Nokia baught Trolltech – it’s gonna work on Nokia phones soon, awesome!
Lets get to the details, shall we?

Categories
ffmpeg graphics gui linux programming qt video

Showing video with Qt toolbox and ffmpeg libraries

I recently had to build a demo client that shows short video messages for Ubuntu environment.
After checking out GTK+ I decided to go with the more natively OOP Qt toolbox (GTKmm didn’t look right to me), and I think i made the right choice.
So anyway, I have my video files encoded in some unknown format and I need my program to show them in a some widget. I went around looking for an exiting example, but i couldn’t find anything concrete, except for a good tip here that led me here for an example of using ffmpeg’s libavformat and libavcodec, but no end-to-end example including the Qt code.
The ffmpeg example was simple enough to just copy-paste into my project, but the whole painting over the widget’s canvas was not covered. Turns out painting video is not as simple as overriding paintEvent()…
Firstly, you need a separate thread for grabbing frames from the video file, because you won’t let the GUI event thread do that.
That makes sense, but when the frame-grabbing thread (I called VideoThread) actually grabbed a frame and inserted it somewhere in the memory, I needed to tell the GUI thread to take that buffered pixels and paint them over the widget’s canvas.
This is the moment where I praise Qt’s excellent Signals/Slots mechanism. So I’ll have my VideoThread emit a signal notifying some external entity that a new frame is in the buffer.
Here’s a little code: