Skip to content

Commit

Permalink
PS3Eye library
Browse files Browse the repository at this point in the history
  • Loading branch information
diwi committed Mar 26, 2017
0 parents commit 6a26c6e
Show file tree
Hide file tree
Showing 8 changed files with 2,317 additions and 0 deletions.
58 changes: 58 additions & 0 deletions examples/java/FrameRate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
*
* PS3Eye | Copyright (C) 2017 Thomas Diewald (www.thomasdiewald.com)
*
* src - www.github.com/diwi/PS3Eye
*
* A Processing/Java library for PS3Eye capture using libusb.
* MIT License: https://opensource.org/licenses/MIT
*
*/


package java;

import java.util.Locale;

public class FrameRate {
private long count;
private float framerate = 0;
float smooth = 0.95f; // [0,1]

private int num_timers = 30;
private long[] timer_history = new long[num_timers];
private int timer_idx = 0;


public FrameRate(){
}

public float fps(){
return framerate;
}
public long counter(){
return count;
}

public FrameRate update() {
int idx_cur = timer_idx%num_timers;
int idx_old = (timer_idx+num_timers+1)%num_timers;

timer_history[idx_cur] = System.nanoTime();
timer_idx++;
count++;

long duration = timer_history[idx_cur] - timer_history[idx_old];

float framerate_cur = num_timers/(duration/1E09f);
framerate = framerate * smooth + framerate_cur*(1.0f-smooth);

return this;
}

@Override
public String toString(){
return String.format(Locale.ENGLISH, "%5.2f", framerate);
}

}
109 changes: 109 additions & 0 deletions examples/java/PS3Eye_Basic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
*
* PS3Eye | Copyright (C) 2017 Thomas Diewald (www.thomasdiewald.com)
*
* src - www.github.com/diwi/PS3Eye
*
* A Processing/Java library for PS3Eye capture using libusb.
* MIT License: https://opensource.org/licenses/MIT
*
*/

package java;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.thomasdiewald.PS3Eye.PS3Eye;
import com.thomasdiewald.PS3Eye.PS3Eye.Format;


public class PS3Eye_Basic extends JPanel implements Runnable {

JFrame jframe;

BufferedImage ps3eye_frame;
PS3Eye ps3eye;

public PS3Eye_Basic() {

PS3Eye[] devices = PS3Eye.getDevices();
if (devices.length == 0) {
System.out.println("No PS3Eye connected. Good Bye!");
System.exit(0);
}

ps3eye = devices[0];
ps3eye.init(60, Format.RGB);
ps3eye.start();

int frame_w = ps3eye.getFrameWidth();
int frame_h = ps3eye.getFrameHeight();

ps3eye_frame = new BufferedImage(frame_w, frame_h, BufferedImage.TYPE_INT_ARGB);

setPreferredSize(new Dimension(frame_w, frame_h));

jframe = new JFrame("PS3Eye Capture");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
jframe.add(this);
jframe.pack();
jframe.setVisible(true);
jframe.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
PS3Eye.disposeAll(); // cleanup, shut-off led
}
});

// start capturing
new Thread(this).start();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(ps3eye_frame, 0, 0, this);
}


@Override
public void run() {
FrameRate framerate = new FrameRate();
int[] pixels = ((DataBufferInt) ps3eye_frame.getRaster().getDataBuffer()).getData();
// ps3eye.waitAvailable(false);
while (true) {

// if(ps3eye.isAvailable()){
ps3eye.getFrame(pixels);
repaint();
jframe.setTitle(""+framerate.update());
// }

// try {
// Thread.sleep(0);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}

}


public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PS3Eye_Basic();
}
});
}

}
74 changes: 74 additions & 0 deletions examples/processing/PS3Eye_Basic/PS3Eye_Basic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
*
* PS3Eye | Copyright (C) 2017 Thomas Diewald (www.thomasdiewald.com)
*
* src - www.github.com/diwi/PS3Eye
*
* A Processing/Java library for PS3Eye capture using libusb.
* MIT License: https://opensource.org/licenses/MIT
*
*/


package processing.PS3Eye_Basic;

import com.thomasdiewald.PS3Eye.PS3Eye;

import processing.core.PApplet;
import processing.core.PImage;


public class PS3Eye_Basic extends PApplet{

PS3Eye ps3eye;
PImage ps3eye_frame;

public void settings(){
size(640, 480);
smooth(0);
}

public void setup(){
PS3Eye[] ps3eye_list = PS3Eye.getDevices();

if(ps3eye_list.length == 0){
System.out.println("No PS3Eye connected. Good Bye!");
exit();
return;
}

// pick the first device in the list
ps3eye = ps3eye_list[0];
// init, optionally with parameter ... ps3eye.init(60, Format.RGB);
ps3eye.init();
// register "dispose" to cleanup on program exit.
registerMethod("dispose", ps3eye);
// start
ps3eye.start();

// create frame for pixel transfer
ps3eye_frame = createImage(ps3eye.getFrameWidth(), ps3eye.getFrameHeight(), ARGB);

}

public void draw(){

// transfer pixel data
ps3eye_frame.loadPixels();
ps3eye.getFrame(ps3eye_frame.pixels);
ps3eye_frame.updatePixels();

// display frame
image(ps3eye_frame, 0, 0);

// info
String txt_fps = String.format(getClass().getName()+ " [fps %6.2f]", frameRate);
surface.setTitle(txt_fps);
}


public static void main(String[] args) {
PApplet.main(new String[] { PS3Eye_Basic.class.getName() });
}

}
Loading

0 comments on commit 6a26c6e

Please sign in to comment.