Skip to content

Instantly share code, notes, and snippets.

@paksv
Created April 14, 2015 11:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paksv/877c7e87779ed289778d to your computer and use it in GitHub Desktop.
Save paksv/877c7e87779ed289778d to your computer and use it in GitHub Desktop.
OpenCV test
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_highgui;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
/**
* @author sergey.pak
* Date: 12/15/2014
* Time: 6:29 PM
*/
public class OpenCV {
public static void main(String[] args) throws FrameGrabber.Exception {
System.out.println("java.library.path=" + System.getProperty("java.library.path"));
System.load("c:\\windows\\system32\\KERNEL32.DLL");
System.load("C:\\Windows\\System32\\msvcr100.dll");
System.load("C:\\Windows\\System32\\msvcrt.dll");
System.load("C:\\Program Files (x86)\\Screen Capturer Recorder\\screen-capture-recorder-x64.dll");
System.load("E:\\tmp\\javacv\\javacv-bin\\dll\\AVUTIL-52.DLL");
System.load("E:\\tmp\\javacv\\extrapath\\SWRESAMPLE-0.DLL");
System.load("E:\\tmp\\javacv\\javacv-bin\\dll\\avcodec-55.DLL");
System.load("E:\\tmp\\javacv\\javacv-bin\\dll\\jniavcodec.DLL");
System.load("E:\\tmp\\javacv\\javacv-bin\\dll\\jniavcodec.DLL");
final FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("video=screen-capture-recorder");
grabber.setBitsPerPixel(8);
grabber.setFormat("dshow");
grabber.setFrameRate(3);
grabber.start();
long startTime = System.currentTimeMillis();
final SimpleDateFormat sdf = new SimpleDateFormat("HHmmss_SSS");
final ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
Thread the = new Thread(r);
//the.setPriority(Thread.MIN_PRIORITY);
return the;
}
});
final AtomicLong frames = new AtomicLong(0);
new Thread(){
private long prevFrames = 0;
@Override
public void run() {
while (true) {
final long elapsedFrames = frames.get() - prevFrames;
if (elapsedFrames > 0) {
prevFrames += elapsedFrames;
System.out.println("Framerate: " + elapsedFrames / 10.0);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
final Thread thread = new Thread() {
@Override
public void run() {
while (true) {
try {
final Frame frame = grabber.grabFrame(true);
final Runnable task = new Runnable() {
@Override
public void run() {
final BufferedImage bufferedImage = frame.image.getBufferedImage();
final BufferedImage resized = new BufferedImage(400, 400, BufferedImage.TYPE_BYTE_INDEXED);
final Graphics2D g = resized.createGraphics();
g.drawImage(bufferedImage, 0, 0, 400, 400, null);
g.dispose();
File file = new File("img_" + sdf.format(new Date()) + ".jpg");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
ImageIO.write(resized, "jpg", new BufferedOutputStream(out));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
//executorService.submit(task);
task.run();
frames.incrementAndGet();
} catch (Exception ex){
ex.printStackTrace();
}
}
}
};
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment