MBoughaba
Messages postés2Date d'inscriptionmardi 6 avril 2010StatutMembreDernière intervention14 mars 2011
-
14 mars 2011 à 12:03
tarikiest
Messages postés1Date d'inscriptionmardi 21 février 2012StatutMembreDernière intervention15 avril 2012
-
15 avril 2012 à 14:10
Bonjour,
mon API consiste à prendre des image de ma webcam.
mon api marche très bien quand cela est fait en passant par un jButton et un actionListener.
mais une fois j'essaie de prendre les image d'une manière direct au lancement de l'api sans demander une interaction utilisateur j'ai l'erreur:
de Exception in thread "main" java.lang.NullPointerException
at vision.eCamVison.capture(eCamVison.java:151)
at vision.eCamVison.main(eCamVison.java:87)
j'ai initialiser tout ce qu'il faut je vois pas ce qui provoque cette exception pourriez vous m'aider car là je suis un peu bloquer!
voilà ma classe:
package vision;
import java.awt.BorderLayout;
import java.awt.Component;
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.awt.image.RenderedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
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;
/**
* @author
*/
public class eCamVison extends Panel implements ActionListener {
public Player player;
public CaptureDeviceInfo di;
public MediaLocator ml;
public Buffer buf;
public Image img;
public VideoFormat vf;
public BufferToImage btoi;
public BufferedImage bi;
public FrameGrabbingControl fgc;
public Vector videoCapDevList = null;
public ImagePanel imgpanel;
class ImagePanel extends Panel {
public Image myimg;
public ImagePanel() {
setLayout(null);
setSize(320, 240);
}
@Override
public void paint(Graphics g) {
if (myimg != null) {
g.drawImage(myimg, 0, 0, this);
}
}
public void setImage(Image img) {
this.myimg = img;
repaint();
}
}
public static void main(String[] args) throws InterruptedException, IOException {
Frame f = new Frame("iVision");
final eCamVison ecv = new eCamVison();
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
ecv.playerclose();
System.exit(0);
}
});
f.dispose();
f.add("Center", ecv);
f.pack();
f.setVisible(true);
ecv.capture();
ecv.playerclose();
ecv.exit();
}
public eCamVison() {
setLayout(new BorderLayout());
imgpanel = new ImagePanel();
try {
vf = new VideoFormat(VideoFormat.YUV);
videoCapDevList = CaptureDeviceManager.getDeviceList(vf);
if (videoCapDevList.size() > 0) {
CaptureDeviceInfo deviceInfo = (CaptureDeviceInfo) videoCapDevList.elementAt(0);
ml = deviceInfo.getLocator();
deviceInfo = CaptureDeviceManager.getDevice(""
+ "vfw:Microsoft WDM Image Capture (Win32):0");
//CaptureDeviceManager.commit();
System.out.println("video / Device name is: " +
deviceInfo.getName());
System.out.println("video / Device supported formats are: " +
deviceInfo.getFormats());
player = Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();
Component comp = player.getVisualComponent();
if (comp != null) {
add(comp, BorderLayout.NORTH);
}
add(imgpanel, BorderLayout.SOUTH);
}
}
catch (Exception e) {
System.out.println("capture device not found");
System.err.println(e.getMessage());
}
}
public void actionPerformed(ActionEvent e) {
}
public void savePNG(Image img, String s) throws IOException {
bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out;
try {
out = new FileOutputStream(s);
boolean write = ImageIO.write((RenderedImage) img, "png", out);
try {
out.close();
}
catch (java.io.IOException io) {
System.out.println("IOException");
System.err.println(io.getMessage());
}
}
catch (java.io.FileNotFoundException fnf) {
System.out.println("File Not Found");
System.err.println(fnf.getMessage());
}
}
public void capture() throws IOException, InterruptedException {
// Grab a frame
fgc = (FrameGrabbingControl) player.getControl(""
+ "javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
// show the image
imgpanel.setImage(img);
try {
// save image
savePNG(img, "test.png");
} catch (IOException ex) {
Logger.getLogger(eCamVison.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void playerclose() {
player.close();
player.deallocate();
}
public void exit() {
System.exit(0);
}
}
MBoughaba
Messages postés2Date d'inscriptionmardi 6 avril 2010StatutMembreDernière intervention14 mars 2011 14 mars 2011 à 12:19
désolé je ne trouve pas comment éditer ce poste
voilà je met en forme ma balise code :s
Bonjour,
mon API consiste à prendre des image de ma webcam.
mon api marche très bien quand cela est fait en passant par un jButton et un actionListener.
mais une fois j'essaie de prendre les image d'une manière direct au lancement de l'api sans demander une interaction utilisateur j'ai l'erreur:
de Exception in thread "main" java.lang.NullPointerException
at vision.eCamVison.capture(eCamVison.java:151)
at vision.eCamVison.main(eCamVison.java:87)
j'ai initialiser tout ce qu'il faut je vois pas ce qui provoque cette exception pourriez vous m'aider car là je suis un peu bloquer!
voilà ma classe:
package vision;
import java.awt.BorderLayout;
import java.awt.Component;
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.awt.image.RenderedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
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;
public class eCamVison extends Panel implements ActionListener {
public Player player;
public CaptureDeviceInfo di;
public MediaLocator ml;
public Buffer buf;
public Image img;
public VideoFormat vf;
public BufferToImage btoi;
public BufferedImage bi;
public FrameGrabbingControl fgc;
public Vector videoCapDevList = null;
public ImagePanel imgpanel;
class ImagePanel extends Panel {
public Image myimg;
public ImagePanel() {
setLayout(null);
setSize(320, 240);
}
@Override
public void paint(Graphics g) {
if (myimg != null) {
g.drawImage(myimg, 0, 0, this);
}
}
public void setImage(Image img) {
this.myimg = img;
repaint();
}
}
public static void main(String[] args) throws InterruptedException, IOException {
Frame f = new Frame("iVision");
final eCamVison ecv = new eCamVison();
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
ecv.playerclose();
System.exit(0);
}
});
f.dispose();
f.add("Center", ecv);
f.pack();
f.setVisible(true);
ecv.capture();
ecv.playerclose();
ecv.exit();
}
public eCamVison() {
setLayout(new BorderLayout());
imgpanel = new ImagePanel();
try {
vf = new VideoFormat(VideoFormat.YUV);
videoCapDevList = CaptureDeviceManager.getDeviceList(vf);
if (videoCapDevList.size() > 0) {
CaptureDeviceInfo deviceInfo = (CaptureDeviceInfo) videoCapDevList.elementAt(0);
ml = deviceInfo.getLocator();
deviceInfo = CaptureDeviceManager.getDevice(""
+ "vfw:Microsoft WDM Image Capture (Win32):0");
//CaptureDeviceManager.commit();
System.out.println("video / Device name is: " +
deviceInfo.getName());
System.out.println("video / Device supported formats are: " +
deviceInfo.getFormats());
player = Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();
Component comp = player.getVisualComponent();
if (comp != null) {
add(comp, BorderLayout.NORTH);
}
add(imgpanel, BorderLayout.SOUTH);
}
}
catch (Exception e) {
System.out.println("capture device not found");
System.err.println(e.getMessage());
}
}
public void actionPerformed(ActionEvent e) {
}
public void savePNG(Image img, String s) throws IOException {
bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out;
try {
out = new FileOutputStream(s);
boolean write = ImageIO.write((RenderedImage) img, "png", out);
try {
out.close();
}
catch (java.io.IOException io) {
System.out.println("IOException");
System.err.println(io.getMessage());
}
}
catch (java.io.FileNotFoundException fnf) {
System.out.println("File Not Found");
System.err.println(fnf.getMessage());
}
}
public void capture() throws IOException, InterruptedException {
// Grab a frame
fgc = (FrameGrabbingControl) player.getControl(""
+ "javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
// show the image
imgpanel.setImage(img);
try {
// save image
savePNG(img, "test.png");
} catch (IOException ex) {
Logger.getLogger(eCamVison.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void playerclose() {
player.close();
player.deallocate();
}
public void exit() {
System.exit(0);
}
}
tarikiest
Messages postés1Date d'inscriptionmardi 21 février 2012StatutMembreDernière intervention15 avril 2012 15 avril 2012 à 14:10
Bonjours ;
avant que tu lance la capture tu doit attendre qq Millisconde pour que le device
s'initialise correctement ,tu peut utilisé :
Thread.sleep(2000);// par exemple