Home about IT Motivation Course Sales Project About Me

Thursday, June 30, 2011

JAVA: capture camera using JMF

use below script to capture camera on notebook or
Logitech USB Video Camera use JMF

name: SwingCapture.java

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JButton;
import javax.swing.JComponent;
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGEncodeParam;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class SwingCapture extends Panel implements ActionListener
{public static Player player = null;

public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public JButton capture = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public SwingCapture()
{setLayout(new BorderLayout());

setSize(320,550);
imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
//Getting Camera Device Information
ml = di.getLocator();
//MediaLocator for Camera
try
{player = Manager.createRealizedPlayer(ml);//Creating Player
player.start();
//Start Camera Player
Component comp;
//for Getting Visual Player Component of Camera
if ((comp = player.getVisualComponent()) != null)
{
add(comp,BorderLayout.NORTH); //Add Camera to North

}
add(capture,BorderLayout.CENTER); //Add Capture Button in Between
add(imgpanel,BorderLayout.SOUTH); //Add Captured image
}
catch (Exception e)
{e.printStackTrace();
}
}
public static void main(String[] args)
{Frame f = new Frame("SwingCapture");
//Creating New Frame
SwingCapture cf = new SwingCapture();
//Main Object Create
f.addWindowListener(new WindowAdapter() //Window Closing
{public void windowClosing(WindowEvent e)
{playerclose();

//Closing Player
System.exit(0);
}
});
f.add("Center",cf);
//Adding Main Object to Frame
f.pack();

//Packing Frame
f.setSize(new Dimension(320,550));
f.setVisible(true);
}
public static void playerclose()//Function While Closing Player
{player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e)
{JComponent c = (JComponent) e.getSource();//Getting Source of Event
if (c == capture)//Capture Button Event
{FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Grabbing a frame
btoi = new BufferToImage((VideoFormat)buf.getFormat());// Convert it to an image Buffer
img = btoi.createImage(buf); //Creating Image from Buffer
imgpanel.setImage(img);

// show the image
saveJPG(img,"c:\\test.jpg"); // save image Function
}
}
class ImagePanel extends Panel
//Panel for Displaying Cptured Image
{public Image myimg = null;
public ImagePanel()
{setLayout(null);
setSize(320,240);
}
public void setImage(Image img)
{this.myimg = img;
repaint();
}
public void paint(Graphics g)

{if (myimg != null)
{g.drawImage(myimg, 0, 0, this);
}
}
}
public static void saveJPG(Image img, String s)
{BufferedImage bi = new BufferedImage(img.getWidth(null),

img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out = null;
try
{out = new FileOutputStream(s);
}
catch (java.io.FileNotFoundException io)
{System.out.println("File Not Found");

}
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
//param.setQuality(0.5f,false);
//encoder.setJPEGEncodeParam(param);
try
{//encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{System.out.println("IOException");
}
}
}

Wednesday, June 29, 2011

The Basic Steps to Connect Oracle and Java Program

http://www.dbapool.com/articles/011706.html

The Basic Steps to Connect Oracle and Java Program

Here, I will show you what the steps to make a connection between Oracle database and your Java program. To do this work, you need the JDBC (Java DataBase Connectivity) driver. The file name of Oracle's JDBC driver is classes12.zip or classes12.jar. By default, Oracle have included this driver at the software installation process. Its location is in the ORACLE_HOME\jdbc\lib directory. For example, if our ORACLE_HOME is C:\Oracle\Ora90 then the JDBC driver will be placed in C:\Oracle\Ora90\jdbc\lib directory.

Make sure to set the Java CLASSPATH correctly. Here is the command line to do it.
(Note: Assume the ORACLE_HOME is C:\Oracle\Ora90)

set CLASSPATH=.;C:\Oracle\Ora90\jdbc\lib\classes12.jar

or

set CLASSPATH=.;C:\Oracle\Ora90\jdbc\lib\classes12.zip

In addition, you can also set the CLASSPATH in your autoexec.bat file on your Windows operating system.

Now, just follow these steps:

STEP 1. Import the java.sql package into your program.
------------------------------------------------------

import java.sql.*;

The syntax above will import all classes in the java.sql package. If you want to import a few of them, you can write the syntax like this

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;

It only imports the Driver, DriverManager, and Connection class into your Java program.

STEP 2. Load the Oracle's JDBC driver.
--------------------------------------

There are two ways to load your JDBC driver. The first, use the forName() method of java.lang.Class class.

Class.forName("oracle.jdbc.driver.OracleDriver");

And the second way is use the registerDriver() method of DriverManager class.

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

STEP 3. Create a Connection object.
-----------------------------------

To create a Connection object, use the getConnection() method of DriverManager class. This method takes three parameters: URL, username, and password.

Connection conn =
DriverManager.getConnection(
"jdbc:oracle:thin:@mylaptop:1521:ORADB", // URL
"budi", // username
"Bandung" // password
);

STEP 4. Create a Statement object.
----------------------------------

The Statement object can be created by call the createStatement() method of Connection object that you made before. So, you can write the following code

Statement mystat = conn.createStatement();

STEP 5. Execute your SQL statement.
-----------------------------------

After you create a Statement object successfully, you can execute a query (SELECT statement) from your Java program using the executeQuery() method of Statement class. The result of execution process will be stored in ResultSet object, so you need to declare an object of ResultSet first. Here is the code.

ResultSet rs = mystat.executeQuery("select custno, custname from customer");

STEP 6. Display your data.
--------------------------

The next step is display your data using the looping control.

while (rs.next()) {
System.out.println(
rs.getInt(1) + // first column
"\t" + // the horizontal tab
rs.getString(2) // second column
);
}

STEP 7. Close your statement and connection.
--------------------------------------------

mystat.close();
conn.close();


Here is the complete code.

/*********************************************************************
* File name : SimpleOraJava.java

*********************************************************************/

import java.sql.*;

class SimpleOraJava {
public static void main(String args[]) throws SQLException {
DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver()
);
String serverName = "mylaptop";
int port = 1521;
String user = "budi";
String password = "password";
String SID = "ORADB";
String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
Connection conn = DriverManager.getConnection(URL, user, password);
String SQL = "SELECT CUSTNO, CUSTNAME FROM CUSTOMER";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(SQL);
while (rs.next()) {
System.out.println(
rs.getInt(1) +
"\t" +
rs.getString(2)
);
}
stat.close();
conn.close();
}
}