Share the ideas and thoughts, become united ...

Saturday, December 19, 2009

Simple Image Resizing Code ...

Some times you need to quickly resize your image to a different dimension. you may need to resize it to a smaller version (saving bandwidth, computation time etc) or need to scale it to a larger version (displaying).

you can use the following code to resize your image as per your requirement.

LANGUAGE: C#/JAVA. Can also be easily convert to C++ and other languages.


public static int[] rescaleRGBArray(int[] rgb, int oldWidth, int oldHeight, int newWidth, int newHeight) {
    int out[] = new int[newWidth*newHeight];
    for (int yy = 0; yy < newHeight; yy++) {
    int dy = yy * oldHeight / newHeight;
    for (int xx = 0; xx < newWidth; xx++) {
    int dx = xx * oldWidth / newWidth;
    out[(newWidth*yy)+xx]=rgb[(oldWidth*dy)+dx];
    }
    }
   return out; 

}

Hope this help you in your work.

Use your Bluetooth Adaptar without using 3rd patry bluetooth stack !!!

I have a bluetooth adapter which i wanted to use after a long time. but unfortunately i lost it's driver. So, i was wondering, is there any way to use the bluetooth adapter in my winxp with out the driver? well, after a little research i found out that there is a way to use window xp's built in bluetooth stack. You can then use your bluetooth adapter to transfer file or even you can use the bluetooth dialup service from your mobile phone.

here how it is done !!!

WARNING - if you are a serious rookie user do not try this :D

Step # 1 - Plug your bluetooth adapter. Windows will ask you about the driver. Cancel it (don't if you have driver :) ). So, Windows wont recognize your device.

Step # 2 - Go to Control Panel -> System -> Hardware -> Device Manager

Step # 3 - As your device is still unrecognized by the system, look for any unrecognized devices and once you have found your device double click it to bring in the device property page and select details.As you can see in the picture i have selected the hardware id from the drop down box. Now select the device hardware id and press CTRL+C to copy it into clipboard. Windows finds the appropriate driver by using this hardware id.

Step # 4 - Go to your windows directory. Easy way, select run from start menu, type %systemroot% and press ok/enter. Now go to inf folder. If you don't find it its probably hidden. Now find out the bth.ini file in that folder. Once you have found it open it and scroll down until you have found something like below,
;------------- Device section - Start -----------------------
Below this label there are many predefined driver for specific bluetooth adapter.

Step # 5 - Just add your device's hardware id in the end of any manufacturer to use the winxp's built in bluetooth stack. Here is an example -

I have highlighted the inf folder path and bth.ini file in green color. I have also highlighted the device section. You just have to add one line of text which is inside the red box.
The format is like
DEVICE_DESCRIPTION= BthUSB, Your_Device's_Hardware_id_which_you_copied
Now save the bth.ini file and exit.
Note: You can use any text to describe your bluetooth adapter. (You can also add your name!!!)
Note: You can use BthEnum in place of BthUSB if the device support.

Step # 6 - Well your device has now built in driver. Just replug your device or go to device manager and then select scan for hardware changes or make windows automatically check for driver. Windows automatically detects and installs the Microsoft bluetooth stack. Now you can use your device from the control panel.

LIMITATIONS
As far as i know, Microsoft bluetooth stack works pretty much fine. But it doesn't support all bluetooth profile. For example, You cannot use bluetooth headset with Microsoft's bluetooth stack. In order to do so, You must install a 3rd party bluetooth stack.

Hope this walk through will help you.

Raise the desired event in java - it's possible and easy

I, myself is a beginner in programming languages. So, i encounter problem now and then. Recently i have faced a very interesting problem. As you know it is pretty much easy in C# to raise the desire event (or you don't know). But i was wondering how to do it in JAVA. Well i found it pretty much amazingly easy !!!

you just have to call dispatchEvent(AWTEvent e).

Explanation : This method takes the event that you want to raise and dispatches it (handles it).

Sample Code:

public class MainWindow extends JFrame implements WindowListener {
/* properties + methods */
public MainWindow() {
// bla bla codes
this.addWindowListener(this); // i am listening this window and the event handler is also def. in this class
// more codes
}
public void windowClosing(WindowEvent e) { // an event handler
// codes ...
System.out.println("dude this window is closing... save it.."); // code i am going to execute
// and still coding
}
public void raiseWindowCloseEvnt() { // user def. fn to raise the event
// last words before you close your window :)
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); // will i am dispatching a new event which is associated with this window (via this pointer). and i am likely to raise the Window_Closing event. Now i can raise the event just by calling this function.
}
public static void main(String[] args) {
MainWindow mw = new MainWindow();
mw.setVisible(true);
mw.raiseWindowCloseEvnt(); // see the output console :D
}
}

Pretty much easy isn't it? :)