Show Some Love
JAVA

Webcam image capture

This forum was posted on Friday, March 29, 2013 8:40:16 AM



I'm attempting to capture an image from a webcam using the below codes.
code snippet:

import javax.media.*;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import java.awt.*;
import javax.media.control.FrameGrabbingControl;
import java.io.*;
import java.language.*;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class FrameCapture {
static MediaLocator ml;
static Player player;
static Component videoScreen;
static Buffer buf = null;
static String str = null;
static BufferToImage btoi = null;
static BufferedImage bi = null;
static Image frameImage = null;
//static FileOutputStream out = null;
static String filename = null;
public static void main(String args) {
try {
ml = new MediaLocator("vfw://0");
player = Manager.createRealizedPlayer(ml);
player.start();
videoScreen = player.getVisualComponent();
Frame frame = new Frame();
frame.setBounds(10 10 300 300);
frame.add(videoScreen);
frame.setVisible(true);
FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
if (fgc == null) {
System.out.println("fgc == null");
//System.exit(0);
}
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat) buf.getFormat());
if (btoi == null) {
System.out.println("btoi == null");
}
/*
VideoFormat format = (VideoFormat) buf.getFormat();
Dimension size = format.getSize();
if (format == null) {
System.out.println("format == null");
}
frameImage = btoi.createImage(buf);
if (frameImage == null) {
System.out.println("frameImage == null");
}
bi = new BufferedImage(frameImage.getHeight(null) frameImage.getWidth(null) BufferedImage.TYPE_INT_RGB);
if (bi == null) {
System.out.println("bi == null");
}
*/
java.awt.image.BufferedImage img = (java.awt.image.BufferedImage)btoi.createImage(buf);
try
{
filename = "myImage.png";
File file = new File(filename);
FileOutputStream out = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
param.setQuality(1.0f false); // 100% high quality setting no compression
encoder.setJPEGEncodeParam(param);
//encoder.encode ( bi );
encoder.encode(img);
out.close();
}
catch(Exception e) {
System.out.println ("Error saving JPEG: " + e.getMessage() );
}
player.close();
player.deallocate();
}catch(Exception e) {
System.err.println("U r Stupid..: " + e.getMessage());
}
}
}

Here i get a java.language.NullPointerException in the saving JPEG try catch.
I hv altered a little the code snippet in the below approach but i get again an error.
Code:

import javax.media.*;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import java.awt.*;
import javax.media.control.FrameGrabbingControl;
import java.io.*;
import java.language.*;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.media.control.FormatControl;
public class FrameCapture {
static MediaLocator ml;
static Player player;
static Component videoScreen;
static Buffer buf = null;
static String str = null;
static BufferToImage btoi = null;
static BufferedImage bi = null;
static Image frameImage = null;
static String filename = null;
static FormatControl formatControl = null;
static VideoFormat currentFormat = null;

public static void main(String args) {
try {
ml = new MediaLocator("vfw://0");
player = Manager.createRealizedPlayer(ml);
player.start();
videoScreen = player.getVisualComponent();
Frame frame = new Frame();
frame.setBounds(10 10 300 300);
frame.add(videoScreen);
frame.setVisible(true);
formatControl = (FormatControl) player.getControl(
"javax.media.control.FormatControl");
Format currFormat = formatControl.getFormat();
currentFormat = (VideoFormat) currFormat;
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl(
"javax.media.control.FrameGrabbingControl");
Buffer buffer = fgc.grabFrame();
BufferToImage btoi = new BufferToImage((VideoFormat) buffer.
getFormat());
Image image = btoi.createImage(buffer);
String filename = "betaImage.jpeg";
BufferedImage bi = new BufferedImage(image.getWidth(null)
image.getHeight(null)
BufferedImage.TYPE_INT_RGB);
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f false); // 100% high quality setting no compression
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
out.close();
} catch (Exception e) {
System.out.println("Error saving JPEG: " + e.getMessage());
}
player.close();
player.deallocate();
} catch (Exception e) {
System.err.println("U r Stupid..: " + e.getMessage());
}
}
}

Any solution on what's wrong?


114 Viewed this Question



 Recent Replies on "Webcam image capture"

  • Nobody has replied on this post, be first to post answer.

Post Your Solution for this Question


Note: Please Put Code into <code></code> block.

Threads related to "Webcam image capture":

Need som advice for a part of a GUI

Hey all!You might have seen a thread a little days ago about me having som issues with GUI's. Well I solved a best segment of it (I found out there was a very stable WYSIWYG plugin for Eclipse at their own website) but I'm now having other sort of problem.I'm trying to generate a stats viewer and it's a bit fiddly trying to figure out which approach would be the best to select.I hv been experimenting for quite little time and I hv read alot of tips and approach but have not quite gotten my

Jakarta fileupload problems

Hello I'm searcHello ng for a working demo of the "new" Jakarta fileupload-1.1.1.jar that actually works. Does anyone have one?i wish to upload from a JSP and using a servlet store the uploaded file in an Oracle DB (we've been using a BLOG). Our code snippet snippet is old and works erratically. I was hoping to fold in the new code snippet snippet but I hv not been capable to get it working yet.Thnk u in advance for your help Ric

JAva help

can any one help I am trying to generate a JAVA feedback useing the similar statement three times to demonstrate each of the three methods of commenting in java this is what I hv done so far //Demonstrating comments// Written by // Written on public class Hello{/*Program feedback are nonexecuting statements your add to a program for the goal of documentation */System.out.print1n("Hello");//Program feedback are nonexecuting statements your // add to a program for the goal of docum

Java FTP

i require to write a program tocommunicate with an FTP server. generally i require to poll generate an automated FTP download tool. The only caveat is that i require to download only specific images. The filenames that I am searching for update often (manufacturer numbers) so what i require to do is pass a list of filenames to The code snippet and have it download only those files and store them on a folder on my desktop. I hv absolutely no logic How do I go about doing this. I hv never programmed ja

Read a file into an array

I'm searching for a approach of reading a text file formated like this:code snippet snippet: string1 string2 string3 string4 ect ... then put each string into an array. Any solution?Sorry I'm a newguy...

Help with slideshow script

Hi folks I found this slideshow script on a website. Is it possible to hack this to show a text link for each image under the image?code snippet snippet: /************************************************ MultiFrame Image Slideshow script- Dynamic Drive DHTML code snippet snippet library (www.dynamicdrive.com)* This notice MUST stay intact for legal use* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code***********************************************/var seqslides=new Array()//Set Path

Page 2 - Creating an exe...

Ok i took out the .class extension it worked but it can't find the main class. The Clock class is in the similar folder as the jar file and i packaged it with it but it can't find it...

Creating an exe...

Ok I have it clock program i wrote and i require it to be capable to run on my desktop. Nothing special just start it up in the window and have the clock there. how absolutely do i do that? I'm not sure How do I use Jars with one file. I can get to the point where all the -(letters) are shown but don't know where to go from there. Thnk u.

JMenuBar causes panels to repaint continually

I hv made an applet that has a little JPanels on it and a JMenuBar.When I open one of the menu items it works as normal. However if I then select a various menu (causing the first one to hide) all of the panels that were originally covered by the menu bar go into an endless loop of repainting using 100% CPU.As soon as I close all the menu bars it goes back to normal and stops repainting.Is this normal behaviour for a JMenuBar? Is it possible to stop this happening?

JDBC - Listing DBs and Tables

Hey all I hv been searching for quite a while now and since it's been a while since I hv done any MySQL I can not quite remind how this is done.I hv handled to get set up so I my Java applications can connect to a MySQL server and it's all working best. But I'm trying to figure out how I can list all the databases available to select.Also is it possible to list all tables within a database? If someone could just write little code snippet snippet or give me an illustration of How do I do thi