পৃষ্ঠাসমূহ

বৃহস্পতিবার, ১৯ জানুয়ারী, ২০১২

Capturing Image from webcam in Java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.CannotRealizeException;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;

public class JMF {

public static void main(String[] args)
{
//CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
// CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
Player player = null;
System.out.println("starting");
//player = Manager.createRealizedPlayer(deviceInfo.getLocator());
MediaLocator mdl=new MediaLocator("vfw://0");
try {
player = Manager.createRealizedPlayer(mdl);
} catch (NoPlayerException e) {
System.out.println("ex1");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotRealizeException e) {
System.out.println("2");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
System.out.println("3");
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
System.out.println("4");
// TODO Auto-generated catch block
e.printStackTrace();
}
//FrameGrabbingControl frameGrabber = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
/*Buffer buf = frameGrabber.grabFrame();

//Image img = (new BufferToImage((VideoFormat)buf.getFormat()).createImage(buf));
//BufferedImage img= ((VideoFormat)buf.getFormat()).;
//System.out.println(img.getWidth(null)+"height");
// BufferedImage img=ImageIO.read(new File("E:\\windows workspace\\test1\\Desert.jpg"));
//BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
BufferedImage buffImg = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.drawImage(img, 0,0, null);*/
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
Buffer buf = fgc.grabFrame();

// Convert it to an image
BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat());
Image img = btoi.createImage(buf);


//BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
BufferedImage buffImg = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);
Graphics2D g=buffImg.createGraphics();
g.drawImage(img, 0,0, null);

g.setColor(Color.RED);
g.setFont(new Font("Verdana", Font.BOLD, 16));
g.drawString((new Date()).toString(), 10, 25);

/*try {
ImageIO.write(buffImg, "jpg", new File("E:\\webcam1.jpg"));
} catch (IOException e) {
System.out.println("IO hhhhhhh");
// TODO Auto-generated catch block
e.printStackTrace();
}*/

player.close();
player.deallocate();
System.exit(0);
}
}

মঙ্গলবার, ৬ ডিসেম্বর, ২০১১

Status update in twitter using twitter4j jar file(Not complete)

package org.example.tweet;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class TweetActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

// configurationBuilder.setOAuthConsumerKey(CONSUMER_KEY);
// configurationBuilder.setOAuthConsumerSecret(CONSUMER_SECRET);

CustomAsyncTask cm = new CustomAsyncTask();
cm.execute(new String[] { "df" });

}

private class CustomAsyncTask extends AsyncTask {
@Override
protected String doInBackground(String... urls) {
String CALLBACK_URI = "http://myapp.com";
String CONSUMER_KEY = "GyJxm2irndDCc5L2zaA";
String CONSUMER_SECRET = "9pNTI1jNr5pwzbvVpkCDlBSH6LebskiEAP7q4ZQ";
String ACCESS_TOKEN = "163882382-y4BmXvQd0cvgZ74ATRbniEvntxfZEJmO7gJ1bjTE";
String ACCESS_TOKEN_SECRET = "nSGwDgDH01bTM5uksTMBgcDLWaPv3Y5CgCO7cCVHP8";
AccessToken ac = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
Log.d("robin", "caught");
// Configuration configuration = configurationBuilder.build();

// Twitter twitter = new
// TwitterFactory(configuration).getInstance(ac);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(ac);
Log.d("sayfsdem", "caught");

//Twitter t=new TwitterFactory().getInstance("fds","fsd");

try {
twitter4j.Status status = twitter
.updateStatus("tihs sw th aa updatein");
Log.d("status", status.toString());
} catch (TwitterException e) {
// TODO Auto-generated catch block
Log.d("final", "caught");
e.printStackTrace();
}

try {
Log.d("userName", twitter.getScreenName());
// Log.d("password",twitter.getFavorites()());
} catch (IllegalStateException e) {
Log.d("illesayem", "caught");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TwitterException e) {
Log.d("fdfds", "caught");
// TODO Auto-generated catch block
e.printStackTrace();
}

twitter.getAuthorization();
return "dfs";
}

@Override
protected void onPostExecute(String result) {


}
}
}

শনিবার, ১১ জুন, ২০১১

Huffman Coding (using dfs)

#include
#include
#include
#include
#include

#define lenght 100
using namespace std;
void makeCode(int i);
int matrix[lenght][lenght];int num_node;
mapmp;
stackletter;
class vertex{
public:
int id;
int frequency;
int nodeValue;
char c;
string str;
int left;
int leaf;
};
vertex node[lenght];
struct vertex node2[lenght];
string x;
mapv;
maptaken;
mapstream;
mapvisit;
mapleft_child;
bool compare(class vertex a, class vertex b){
if(a.nodeValue

মঙ্গলবার, ৩১ মে, ২০১১

How to increase internet speed in windows

at run write
gpedit.msc
then
addminitrative templates ->Network->Qos packet scheduler
->limit reserved bandwidth



make bandwidth limit 0;
thats all;

বৃহস্পতিবার, ২১ এপ্রিল, ২০১১

JDBC Connection

For Adding drivers go to Services then drivers(right click), add drivers then give address
Mysql connector:C:\Program Files\NetBeans 6.8\ide12\modules\ext\mysql-connector-java-5.1.6-bin.jar
oracle connectior:E:\app\1251\product\11.2.0\dbhome_2\jdbc\lib\ojdbc6_g.jar

Under project at Library (right click) add jar forlder then give the above address

Connection class:


import java.sql.*;

public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}

মঙ্গলবার, ১২ এপ্রিল, ২০১১

Euler path

#include iostream
using namespace std;
int graph[12][12];
int visited[12][12],n;
void traverse(int node);
int main(){
freopen("sam.txt","r",stdin);
int i,j,flag=0;
cin>>n;
int count=0;
for(i=0;i>graph[i][j];
count+=graph[i][j];
}

if(count%2){flag=1;break;}

}
if(flag){cout<<"\n No euler path is there\n";}
else traverse(0);
return 0;

}
void traverse(int node){
int i;
for(i=0;i

Huffman Coding

#include
#include "stdio.h"
#include "string.h"

using namespace std;

int freq[256];
int numc, numn = 0;
char code[32];
bool found;
char letter;

// Object for each node in the Huffman tree
class node
{
public:
int id;
char ch;
int frequency;
node *left;
node *right;

node()
{
id = 0;
ch = '\0';
frequency = 0;
left = NULL;
right = NULL;
}
};

// Element in the priority queue
class list
{
public:
node *n;
list* next;
};

// Finds the element in the queue with a frequency less than
// the paramter passed to the function
list *find(list *p, int a)
{
if(p->next == NULL) return p;
else
{
if(a <= p->next->n->frequency) return p;
else return find(p->next, a);
}
}

// Inserts a new node into the priority queue in order of increasing frequency
list *insert(list *h, node *in)
{
list *n1;

list *p = new list();
p->n = in;

if(p->n->frequency <= h->n->frequency)
{
p->next = h;
h = p;
}
else
{
n1 = find(h, p->n->frequency);

p->next = n1->next;
n1->next = p;
}
return h;
}

// Deletes a node from the front of the queue
list *ldel(list *h)
{
list *n = new list();
n = h;
h = n->next;
return h;
}

// Traverses the queue and prints out the data of each node
// Not required in the code. Used for testing purposes
void traverseList(list *h)
{
cout << h->n->ch << "(" << h->n->id << "): " << h->n->frequency << ", ";
if(h->next != NULL)
{
traverseList(h->next);
}
}

// Takes characters from an input and places them in an array
void input()
{
int num;

for(int i = 0; i < 256; i++)
{
freq[i] = 0;
}

freopen("sample3.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

while(scanf("%c", &letter) == 1)
{
if(letter == '\n') continue;
else
{
num = letter;
freq[num]++;
}
}

for(int i = 0; i < 256; i++)
{
if(freq[i] > 0) numc++;
}
}

// Displays the frquency of each character from the input
void showFreq()
{
char letter;

for(int i = 0; i < 256; i++)
{
if(freq[i] > 0)
{
letter = i;
cout << letter << ": " << freq[i] << ", ";
}
}

cout << endl;
}

// Makes the priority queue using the characters taken as input
// List is ordered by increasing frequency of the character
list *makeList(list *h)
{
int j;

for(int i = 0; i < 256; i++)
{
if(freq[i] > 0)
{
node *m = new node();
numn++;
m->id = numn;
m->ch = i;
m->frequency = freq[i];
m->left = NULL;
m->right = NULL;
h->n = m;
h->next = NULL;
j = i;
break;
}
}

for(int i = j + 1; i < 256; i++)
{
if(freq[i] > 0)
{
node *m = new node();
numn++;
m->id = numn;
m->ch = i;
m->frequency = freq[i];
m->left = NULL;
m->right = NULL;
h = insert(h, m);
}
}
return h;
}

// Creates a Huffman tree using the Huffman algorithm
list *huffman(list *h)
{
for(int i = 0; i < numc - 2; i++)
{
node *n = new node();
n->ch = NULL;
n->frequency = 0;
numn++;
n->id = numn;
n->left = h->n;
n->frequency += h->n->frequency;
h = ldel(h);
n->right = h->n;
n->frequency += h->n->frequency;
h = ldel(h);
h = insert(h, n);
}
node *n = new node();

n->ch = NULL;
n->frequency = 0;
numn++;
n->id = numn;
n->left = h->n;
n->frequency += h->n->frequency;
h = ldel(h);
n->right = h->n;
n->frequency += h->n->frequency;
h->n = n;
return h;
}

// The following functions traverse the tree to find the code for each letter
// Backtracking is used
void goRight(node *r, char a);

void goLeft(node *r, char a)
{
if(r->ch == a)
{
found = true;
}
else
{
if(r->left != NULL) goLeft(r->left, a);
if(!found && r->right != NULL) goRight(r->right, a);
}
if(found) strcat(code, "1");
}

void goRight(node *r, char a)
{
if(r->ch == a)
{
found = true;
}
else
{
if(r->left != NULL) goLeft(r->left, a);
if(!found && r->right != NULL) goRight(r->right, a);
}
if(found) strcat(code, "0");
}

void printCode()
{
for(int i = strlen(code) -1; i >= 0; i--) cout << code[i];
}

void getCode(node *r, char a)
{
found = false;
strcpy(code, "");

if(r->left != NULL)
{
goLeft(r->left, a);
}
if(!found && r->right != NULL)
{
goRight(r->right, a);
}
printCode();
}

void showAllCodes(node *r)
{
for(int i = 0; i < 256; i++)
{
if(freq[i] > 0)
{
letter = i;
if(letter == 32) cout << "SPACE: ";
else cout << letter << ": ";
getCode(r, letter);
cout << endl;
}
}
}

// Main function
int main()
{
list *head = new list(); // Creates the head of the priority queue
node *root = new node(); // Creates the root node of the Huffman tree

input();
head = makeList(head);
head = huffman(head);
root = head->n; // Root points to the last node left in queue
showAllCodes(root);
}