Package org.openpnp.machine.reference.camera

Source Code of org.openpnp.machine.reference.camera.OpenCvCamera

/*
   Copyright (C) 2011 Jason von Nieda <jason@vonnieda.org>
  
   This file is part of OpenPnP.
  
  OpenPnP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenPnP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OpenPnP.  If not, see <http://www.gnu.org/licenses/>.
  
   For more information about OpenPnP visit http://openpnp.org
*/

package org.openpnp.machine.reference.camera;

import java.awt.image.BufferedImage;

import javax.swing.Action;

import org.opencv.core.Mat;
import org.opencv.highgui.VideoCapture;
import org.openpnp.gui.support.PropertySheetWizardAdapter;
import org.openpnp.gui.support.Wizard;
import org.openpnp.gui.wizards.CameraConfigurationWizard;
import org.openpnp.machine.reference.ReferenceCamera;
import org.openpnp.machine.reference.camera.wizards.OpenCvCameraConfigurationWizard;
import org.openpnp.spi.PropertySheetHolder;
import org.openpnp.util.OpenCvUtils;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.core.Commit;

/**
* A Camera implementation based on the OpenCV FrameGrabbers.
*/
public class OpenCvCamera extends ReferenceCamera implements Runnable {
    static {
        nu.pattern.OpenCV.loadShared();
        System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
    }   
   
  @Attribute(required=true)
  private int deviceIndex = Integer.MIN_VALUE;
 
  private VideoCapture fg = new VideoCapture();
  private Thread thread;
 
  public OpenCvCamera() {
      setDeviceIndex(0);
  }
 
  @Commit
  private void commit() {
    setDeviceIndex(deviceIndex);
  }
 
  @Override
  public synchronized BufferedImage capture() {
    try {
        Mat mat = new Mat();
        if (!fg.read(mat)) {
            return null;
        }           
      return OpenCvUtils.toBufferedImage(mat);
    }
    catch (Exception e) {
      return null;
    }
  }
 
  public void run() {
    while (!Thread.interrupted()) {
      try {
        BufferedImage image = capture();
        if (image != null) {
            image = applyRotation(image);
          broadcastCapture(image);
        }
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      try {
        Thread.sleep(1000 / 24);
      }
      catch (InterruptedException e) {
        break;
      }
    }
  }
 
  public int getDeviceIndex() {
    return deviceIndex;
  }

  public synchronized void setDeviceIndex(int deviceIndex) {
      if (this.deviceIndex == deviceIndex) {
          return;
      }
    this.deviceIndex = deviceIndex;
    if (thread != null) {
      thread.interrupt();
      try {
        thread.join();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      thread = null;
    }
    try {
        fg.open(deviceIndex);
    }
    catch (Exception e) {
      e.printStackTrace();
      return;
    }
    thread = new Thread(this);
    thread.start();
  }

  @Override
  public Wizard getConfigurationWizard() {
    return new OpenCvCameraConfigurationWizard(this);
  }
 
   
    @Override
    public String getPropertySheetHolderTitle() {
        return getClass().getSimpleName() + " " + getId();
    }

    @Override
    public PropertySheetHolder[] getChildPropertySheetHolders() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public PropertySheet[] getPropertySheets() {
        return new PropertySheet[] {
                new PropertySheetWizardAdapter(new CameraConfigurationWizard(this)),
                new PropertySheetWizardAdapter(getConfigurationWizard())
        };
    }
   
    @Override
    public Action[] getPropertySheetHolderActions() {
        // TODO Auto-generated method stub
        return null;
    }
}
TOP

Related Classes of org.openpnp.machine.reference.camera.OpenCvCamera

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.