Java OpenCV视频捕获中断

时间:2018-04-06 01:03:34

标签: java opencv video-capture opencv3.0

我是初学者,在java中尝试创建一个图像捕获程序,它既记录用户又允许将图像移动到GUI上捕获的图像区域,我唯一的问题是记录似乎被切断了即使我指定了应该显示的区域,任何帮助都会被高度评价。

package gesture.recognition;
import com.sun.xml.internal.ws.api.Component;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import static org.opencv.videoio.Videoio.CV_CAP_PROP_FRAME_HEIGHT;
import static org.opencv.videoio.Videoio.CV_CAP_PROP_FRAME_WIDTH;

public class GestureRecognition extends javax.swing.JFrame {
    private DaemonThread myThread = null;
    int count = 0;
    VideoCapture webSource = null;
    Mat vid = new Mat();
    MatOfByte mem = new MatOfByte();
    int CANVAS_INITIAL_WIDTH =400;
    int CANVAS_INITIAL_HEIGHT =500;
    int Video_display=400;
    int Button_Area=200;
    float h,s,v;
    int alpha,r,g,b;
    BufferedImage img = null;
    File f = null;
    Mat hsv = new Mat();

    class Canvas extends JPanel
    {
        // Called every time there is a change in the canvas contents.
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);        
            draw(g);


        }
    } 

    class DaemonThread implements Runnable
    {
    protected volatile boolean runnable = false;

    @Override
    public  void run()
    {
        synchronized(this)
        {
            while(runnable)
            {
                if(webSource.grab())
                {
                try
                        {
                            webSource.retrieve(vid);
                            Imgcodecs.imencode(".jpg", vid, mem);
                Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));

                BufferedImage buff = (BufferedImage) im;
                Graphics g=freehandSliderPanel.getGraphics();
                            if (g.drawImage(im, 0, 0, null));
//              if (g.drawImage(buff,0, 0, getWidth(), getHeight() -150 , 0 , 0, buff.getWidth(), buff.getHeight(),null))



                if(runnable == false)
                            {
                    System.out.println("Going to wait()");
                    this.wait();
                }
             }
             catch(IOException | InterruptedException ex)
                         {
                System.out.println("Error");
                         }
                }
            }
        }
     }
   }

        private Canvas canvas;
        private JPanel controlPanel;
        private JPanel messageArea;
        private JButton jButton1;
        private JPanel freehandSliderPanel;

        public GestureRecognition() 
    {
        setTitle("Gesture Recognition");
        setLayout(new BorderLayout());  // Layout manager for the frame.

        // Canvas
        canvas = new Canvas();
        canvas.setBorder(new TitledBorder(new EtchedBorder(), "Video"));
        canvas.setPreferredSize(new Dimension(CANVAS_INITIAL_WIDTH, CANVAS_INITIAL_HEIGHT));
        add(canvas, BorderLayout.WEST);

        controlPanel = new JPanel();
          controlPanel.setBorder(new TitledBorder(new EtchedBorder(), "Picture"));
          controlPanel.setPreferredSize(new Dimension(Video_display, CANVAS_INITIAL_HEIGHT));
          // the following two lines put the control panel in a scroll pane (nicer?).      
          JScrollPane controlPanelScrollPane = new JScrollPane(controlPanel);
          controlPanelScrollPane.setPreferredSize(new Dimension(Video_display + 30, CANVAS_INITIAL_HEIGHT));
        add(controlPanelScrollPane, BorderLayout.EAST);

        messageArea = new JPanel();
        messageArea.setBackground(canvas.getBackground());
        messageArea.setBorder(new TitledBorder(new EtchedBorder(), "Message Area"));
        messageArea.setPreferredSize(new Dimension(Video_display + CANVAS_INITIAL_WIDTH, Button_Area));
        add(messageArea, BorderLayout.SOUTH);

        jButton1 = new JButton("Start Video");
          jButton1.setPreferredSize(new Dimension(Button_Area - 10, 50));
        messageArea.add(jButton1);



        freehandSliderPanel = new JPanel();
          freehandSliderPanel.setPreferredSize(new Dimension(CANVAS_INITIAL_WIDTH - 20, 90));
          canvas.setLayout(new GridLayout(0, 1));
          freehandSliderPanel.setBorder(new TitledBorder(new EtchedBorder(), "Display"));
        canvas.add(freehandSliderPanel,BorderLayout.CENTER);




        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        jButton1.setText("Video Capture");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
    }


        void draw(Graphics g)
    {

    }// end draw method 



   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    webSource =new VideoCapture(0);
    myThread = new DaemonThread();
            Thread t = new Thread(myThread);
            t.setDaemon(true);
            myThread.runnable = true;
            t.start();
             jButton1.setEnabled(false);  //start button
//            jButton2.setEnabled(true);  // stop button
    }
   public static void main(String args[]) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        GestureRecognition GestureRecognitionInstance = new GestureRecognition();

        /*Create and display the form */
/*        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

            }
        });
*/    }

}

到目前为止,它显示了GUI,但正如您所看到的,它只是简单地切断了图像

Example image

0 个答案:

没有答案