Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 733219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:13:55+00:00 2026-05-14T07:13:55+00:00

I have written code in Java to access web cam,and to save image… I

  • 0

I have written code in Java to access web cam,and to save image…
I am getting following exceptions :
Exception in thread “main” java.lang.NullPointerException
at SwingCapture.(SwingCapture.java:40)
at SwingCapture.main(SwingCapture.java:66)

how to remove this exceptions.

here is the code:

import javax.swing.*;

import javax.swing.event.;
import java.io.
;
import javax.media.;
import javax.media.format.
;
import javax.media.util.;
import javax.media.control.
;
import javax.media.protocol.;
import java.util.
;
import java.awt.;
import java.awt.image.
;
import java.awt.event.;
import com.sun.image.codec.jpeg.
;

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:iNTEX IT-308 WC:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();

try 
{
  player = Manager.createRealizedPlayer(ml);
  player.start();
  Component comp;

  if ((comp = player.getVisualComponent()) != null)
  {
    add(comp,BorderLayout.NORTH);
  }
  add(capture,BorderLayout.CENTER);
  add(imgpanel,BorderLayout.SOUTH);
} 
catch (Exception e) 
{
  e.printStackTrace();
}

}

public static void main(String[] args)
{
Frame f = new Frame(“SwingCapture”);
SwingCapture cf = new SwingCapture();

f.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
  playerclose();
  System.exit(0);}});

f.add("Center",cf);
f.pack();
f.setSize(new Dimension(320,550));
f.setVisible(true);

}

public static void playerclose()
{
player.close();
player.deallocate();
}

public void actionPerformed(ActionEvent e)
{
JComponent c = (JComponent) e.getSource();

if (c == capture) 
{
  // Grab a frame
  FrameGrabbingControl 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);

  // save image
  saveJPG(img,"\test.jpg");
}

}

class ImagePanel extends Panel
{
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"); 
}

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:13:56+00:00Added an answer on May 14, 2026 at 7:13 am

    If you are using built in web cam most of the time it shows errors. You can try with external web cam.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written java code for send mail which is giving exception : when
I have written java code for generating json of my searched data from file.But
I have written a multi threaded java code, which when runs creates 8 threads
I have written a MapReduce program, code is below: import java.io.IOException; import java.util.Iterator; import
In our application, I have seen code written like this: User.java (User entity) public
Let's assume I have a framework written in Java and some C++ code which
So I have taken over a Java Web project . The application was written
I have written the following code with permissions included in the manifest file. package
I have written a java server and here is the code: try { ss

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.