Created
January 9, 2013 01:55
-
-
Save He-Pin/4489904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.sosia.media.webcam; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.ScheduledFuture; | |
import java.util.concurrent.ScheduledThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
import javax.imageio.ImageIO; | |
import us.sosia.media.video.ImageUtils; | |
import com.github.sarxos.webcam.Webcam; | |
public class ImageGrabber { | |
protected ScheduledThreadPoolExecutor timeWorker = new ScheduledThreadPoolExecutor(1); | |
protected ConcurrentHashMap<Webcam, ByteArrayOutputStream> bytesOutputStreams = new ConcurrentHashMap<Webcam, ByteArrayOutputStream>(); | |
protected ConcurrentHashMap<Webcam, GrabberListener> graberListeners = new ConcurrentHashMap<Webcam, ImageGrabber.GrabberListener>(); | |
protected final boolean orginData; | |
public boolean isOrginData() { | |
return orginData; | |
} | |
public ImageGrabber(boolean orginData) { | |
super(); | |
this.orginData = orginData; | |
} | |
public ScheduledFuture<?> schedule(final Webcam webcam,final GrabberListener graberListener,int FPS){ | |
if (!webcam.isOpen()) { | |
webcam.open(); | |
} | |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
bytesOutputStreams.put(webcam, byteArrayOutputStream); | |
graberListeners.put(webcam, graberListener); | |
GrabTask grabTask = new GrabTask(webcam, byteArrayOutputStream, graberListener); | |
return timeWorker.scheduleWithFixedDelay(grabTask, 0, 1000/FPS, TimeUnit.MILLISECONDS); | |
} | |
public interface GrabberListener { | |
public void onGrabed(byte[] bytes); | |
public void onGrabed(BufferedImage bufferedImage); | |
} | |
public static class SimpleGraberListener implements GrabberListener{ | |
@Override | |
public void onGrabed(byte[] bytes) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onGrabed(BufferedImage bufferedImage) { | |
// TODO Auto-generated method stub | |
} | |
} | |
public class GrabTask implements Runnable{ | |
protected final Webcam webcam; | |
protected final ByteArrayOutputStream byteArrayOutputStream; | |
protected final GrabberListener graberListener; | |
public GrabTask(Webcam webcam, | |
ByteArrayOutputStream byteArrayOutputStream, | |
GrabberListener graberListener) { | |
super(); | |
this.webcam = webcam; | |
this.byteArrayOutputStream = byteArrayOutputStream; | |
this.graberListener = graberListener; | |
} | |
@Override | |
public void run() { | |
byteArrayOutputStream.reset(); | |
BufferedImage bufferedImage = webcam.getImage(); | |
BufferedImage compressedImage = ImageUtils.convertToType( | |
bufferedImage, BufferedImage.TYPE_3BYTE_BGR); | |
// System.out.println("image type :"+bufferedImage.getType()); | |
if (orginData) { | |
graberListener.onGrabed(compressedImage); | |
return; | |
} | |
try { | |
ImageIO.write(compressedImage, "JPG", byteArrayOutputStream); | |
// | |
graberListener.onGrabed(byteArrayOutputStream.toByteArray()); | |
//graberListener.onGrabed(byteArrayOutputStream.toByteArray()); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment