人脸检测空指针异常

时间:2015-03-07 13:04:55

标签: nullpointerexception camera

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
import org.opencv.objdetect.CascadeClassifier;

public class WebCam extends JPanel implements ActionListener{
   private BufferedImage image;
   public JButton button = new JButton("capture");
   int count = 1;
   public WebCam() {
       super();
       button.addActionListener((ActionListener) this);
       this.add(button);
   }
   public BufferedImage getImage() {
       return image;
   }
   public void setimage(BufferedImage newimage) {
        image = newimage;
    }
   @Override
   public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (this.image == null) {
            return;
        }
        g.drawImage(this.image, 0, 0, this.image.getWidth(), this.image.getHeight(), null);
    }
   public static void main(String[] args) throws Exception {
       JFrame frame = new JFrame("face capture");
       frame.setSize(300,400);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
       CascadeClassifier faceDetector = new CascadeClassifier("C:\\opencv\\sources\\data\\lbpcascades\\lbpcascade_frontalface.xml");
       WebCam anan = new WebCam();
       frame.add(anan);
       frame.setVisible(true);
       Mat webcam_image = new Mat();
       MatToBufImg mat2Buf = new MatToBufImg();
       VideoCapture capture = null;
       try {
           capture = new VideoCapture(0);
       }
       catch (Exception e){
       }
       if (capture.open(0)) {
           while (true) {
                capture.read(webcam_image);
                if (!webcam_image.empty()) {
                    frame.setSize(webcam_image.width(), webcam_image.height());
                    MatOfRect faceDetections = new MatOfRect();
                    faceDetector.detectMultiScale(webcam_image, faceDetections);
                    for (Rect rect : faceDetections.toArray()) {
                        Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));// mat2Buf, mat2Buf);
                    }
                    System.out.println("...............face detected: " + faceDetections.toArray().length);
                    if (faceDetections.toArray().length == 0) {
                        System.out.println("Face not detected!");
                    }
                    mat2Buf.setMatrix(webcam_image, ".jpg");
                    anan.setimage(mat2Buf.getBufferedImage());
                    anan.repaint();
                }
                else {
                    System.out.println("problem");
                }
           }
       }
       capture.release();
   }
   @Override
   public void actionPerformed(ActionEvent e){
        System.out.println("hellohellohellohellohello");
        int width = 943;
        int height = 640;
        BufferedImage image = null;
        File f = null;
        String ans = JOptionPane.showInputDialog(null, "Color/Grey");
        System.out.println(ans);
        BufferedImage bi = image;
        ImageIcon ii = null;
        Image newimg = bi.getScaledInstance(320, 220, java.awt.Image.SCALE_SMOOTH);
        ii = new ImageIcon(newimg);
        Image i2 = ii.getImage();
        image = new BufferedImage(i2.getWidth(null), i2.getHeight(null), BufferedImage.SCALE_SMOOTH);
        image.getGraphics().drawImage(i2, 0, 0, null);
        RenderedImage resim = null;
        if (i2 instanceof RenderedImage) {
            resim = (RenderedImage) i2;
        } else {
            BufferedImage anlikResim = null;
            if (!ans.equalsIgnoreCase("color")) {
                anlikResim = new BufferedImage(
                        ii.getIconWidth(),
                        ii.getIconHeight(),
                        BufferedImage.TYPE_BYTE_GRAY);
            } else {
                anlikResim = new BufferedImage(
                        ii.getIconWidth(),
                        ii.getIconHeight(),
                        BufferedImage.SCALE_SMOOTH);
            }
            Graphics2D g = anlikResim.createGraphics();
            g.drawImage(i2, 0, 0, null);
            g.dispose();
            resim = anlikResim;
        }
        try {
            f = new File("C:\\opencv\\build\\java\\x64\\");
            ImageIO.write(resim, "jpg", f);   
            System.out.println("yes");
        } catch (Exception ex) {
        }
    }
}

我正在开展一个人脸检测项目。它过去工作得非常好,但突然停止正常工作。当我运行程序时,它会在第120行给出NullPointerException。我该如何解决这个问题?

0 个答案:

没有答案