|
begin process at 2008 05 16 04:45:53
Derniers logiciels
|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
WEBCAM HTTP SERVEUR LIVE SANS JMF
Information sur la source
Description
Serveur HTTP pour window capturant à interval régulier les frames provenant d'une webcam via la librairie webcamlib. Pour accéder à la page web votre webcam il vous suffit de double cliquer sur le jar et de taper dans votre navigateur "http://localhost/". La page index.html permet de recharger à interval régulier la source de l'image(/webcam) en javascript ( testé sous IE/Firefox/Opéra). Si l'url de la ressource demandée est "/webcam" le serveur retourne une image au format jpeg, sinon recherche des fichiers dans le répertoire "public_html". NB: Pour accéder à la webcam depuis internet il vous suffit de remplacer localhost par l'adresse IP du serveur à condition que vous ayez bien redirigé le port 80 vers votre serveur sur votre box adsl. Have fun, Pierrick
Source
- /**
- * HTTP Webcam Server Version 1.0
- * (c) 2008 Pierrick HYMBERT <pierrick.hymbert@gmail.com>
- * This library is "PROVIDED AS IS" without guaranty of any kinds.
- * Freely distribuable under the terms of an MIT-style license.
- * Visit www.hypik.fr
- */
- package org.hypik.webcamlib.test.server;
-
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
- import java.awt.image.DirectColorModel;
- import java.awt.image.WritableRaster;
- import java.io.ByteArrayOutputStream;
-
- import org.apache.log4j.Logger;
- import org.hypik.webcamlib.codecs.Codec;
- import org.hypik.webcamlib.codecs.Codecs;
- import org.hypik.webcamlib.compressor.CompressionException;
- import org.hypik.webcamlib.compressor.DecompressVideo;
- import org.hypik.webcamlib.compressor.jna.ICM;
- import org.hypik.webcamlib.device.Device;
- import org.hypik.webcamlib.device.listener.DeviceErrorListener;
- import org.hypik.webcamlib.device.listener.DeviceFrameListener;
- import org.hypik.webcamlib.device.listener.DeviceStatusListener;
- import org.hypik.webcamlib.jna.User32.BitmapInfoHeader;
- import org.hypik.webcamlib.jna.User32.VideoHDR;
-
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGEncodeParam;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- import com.sun.jna.Memory;
- /**
- * This class manage the webcam capture in a thread.
- * After configure decompression video driver and webcam device.
- * Instance can grab frame every rate seconds and decompress it.
- * Frame data can be get back as RGB or Jpeg formats.
- * @author Pierrick Hymbert <a href="mailto:pierrick.hymbert@gmail.com">Contact</a>
- *
- */
- public class WebcamCapture extends Thread {
- /**
- * The class logger.
- */
- private Logger logger = Logger.getLogger(WebcamCapture.class);
- /**
- * The webcam device.
- */
- private Device device;
- /**
- * Time between each frame.
- */
- private int rate;
- /**
- * Indicates if we must continue capturing frame.
- */
- private boolean capture = false;
- /**
- * Indicate that all is configured.
- */
- private boolean available = false;
- /**
- * The video driver decompression.
- */
- private DecompressVideo decompressVideo;
- /**
- * The decompressed data.
- */
- private Memory decompressedData;
- /**
- * The output frame format.
- */
- private BitmapInfoHeader bitmapInfoOutput;
- /**
- * Build a webcam capture.
- * @param device The webcam
- * @param rate The refresh rate.
- */
- public WebcamCapture(Device device, int rate){
- super("WCCapture #" + device.getIndex());
- this.device = device;
- this.rate = rate;
- }
- /**
- * Ask the thread to stop the capture.
- */
- public void stopCapture() {
- this.capture = false;
- }
- /**
- * Start the thread.
- */
- public void startCapture() {
- this.capture = true;
- this.start();
- }
- /**
- * Run webcam capture until stopCapture is called.
- */
- public void run() {
- try{
- // Configure device and decompressor
- logger.debug("Configuring webcam device...");
- configureDevice();
- logger.debug("Configuring frame decompressor...");
- configureDecompressor();
- logger.info("Webcam capture started.");
- }catch( Exception e ){
- logger.error(e);
- capture = false;
- }
- available = true;
- // Caputre loop
- while( capture ){
- // Sometimes call back functions are disabled.
- device.forceCallBacks();
- device.grabFrame();
- try{
- Thread.sleep(rate);
- }catch (Exception e) {
- logger.error(e);
- capture = false;
- }
- }
- logger.debug("Disconnecting webcam device...");
- // Destroy window and disconnect
- device.stop();
- if( decompressVideo != null ){
- try{
- if( decompressVideo.isOpened() ){
- logger.debug("Free decompression resources...");
- decompressVideo.freeResources();
- }
- }catch (CompressionException e) {
- logger.error(e);
- }finally{
- try{
- if( decompressVideo.isOpened() ){
- logger.debug("Closing decompression driver...");
- decompressVideo.close();
- }
- }catch (CompressionException e) {
- logger.error(e);
- }
- }
- }
- logger.info("Webcam capture stopped.");
- }
- /**
- * Configure the device.
- * @throws Exception
- */
- private void configureDevice() throws Exception {
- //Create the capture window
- device.createCaptureWindow("Webcam device", 640, 480, 0, 0);
- if( !device.isCaptureWindowCreated() )
- throw new Exception("Unable to create webcam capture window!");
-
- // Add listeners
- device.addDeviceErrorListener( errorListener );
- device.addDeviceStatusListener( statusListener );
- device.addDeviceFrameListener( frameListener );
-
- // Connect the window to device driver
- device.connect();
- if( !device.isConnected() )
- throw new Exception("Unable to connect webcam capture window to the webcam driver!");
- logger.debug("Webcam device is now connected to the webcam window. Do not close this program before calling stopCapture. Otherwise you can cause damage to your video device.");
-
- device.setScale(false);
- device.setPreview(false);
- device.setPreviewRate(rate);
- }
- /**
- * Configure the decompressor
- * @throws CompressionException If no compressor found.
- */
- private void configureDecompressor() throws CompressionException {
- // Get back device bit map info input
- BitmapInfoHeader bitmapInfoInput = device.getBitMapInfo().bmiHeader;
-
- // Build the output format
- bitmapInfoOutput = new BitmapInfoHeader();
- bitmapInfoOutput.biBitCount = 24;
- bitmapInfoOutput.biCompression = BitmapInfoHeader.BI_RGB;
- bitmapInfoOutput.biHeight = bitmapInfoInput.biHeight;
- bitmapInfoOutput.biWidth = bitmapInfoInput.biWidth;
- bitmapInfoOutput.biSizeImage = bitmapInfoInput.biSizeImage = 0;
- bitmapInfoOutput.biSize = bitmapInfoInput.biSize = bitmapInfoInput.size();
- bitmapInfoOutput.biPlanes = bitmapInfoInput.biPlanes = 1;
- bitmapInfoOutput.biXPelsPerMeter = bitmapInfoOutput.biYPelsPerMeter = bitmapInfoInput.biXPelsPerMeter = bitmapInfoInput.biYPelsPerMeter = 0;
- bitmapInfoOutput.biClrUsed = bitmapInfoOutput.biClrImportant = bitmapInfoInput.biClrUsed = bitmapInfoInput.biClrImportant = 256;
-
- Codec codecInput = Codecs.getCodec(bitmapInfoInput.biCompression);
- if (codecInput != null){
- logger.info("Device input codec is " + codecInput.getFourCCString() + ": " + codecInput.getLongName());
- }
- Codec codecOutput = Codecs.getCodec(bitmapInfoOutput.biCompression);
- if (codecOutput != null){
- logger.info("Device output codec is " + codecOutput.getFourCCString() + ": " + codecOutput.getLongName());
- }
- // Find a decompressor from the input format to the output
- decompressVideo = DecompressVideo.findDecompressorVideo(bitmapInfoInput, bitmapInfoOutput);
-
- //Check if a decompressor was found
- if( decompressVideo == null ){
- throw new CompressionException("No decompressor found from this input codec to this output.", ICM.ICERR_BADFORMAT);
- }
-
- // Allocate decompressed data buffer
- decompressedData = new Memory(3
- * bitmapInfoOutput.biHeight * bitmapInfoOutput.biWidth);
-
- // Opening decompression driver
- decompressVideo.open();
-
- // Begin decompression
- decompressVideo.begin(bitmapInfoInput, bitmapInfoOutput);
- }
- /**
- * Read decompressed datas in memory.
- * Warning NO Thread safe.
- * @return The decompressed data.
- */
- private byte[] getDatas(){
- return decompressedData.getByteArray(0, (int) decompressedData.getSize());
- }
- /**
- * A buffered image of the decompressed data.
- * @return The decompressed image.
- */
- public BufferedImage getBufferedImage(){
- BufferedImage image = null;
- synchronized (decompressedData) {
- byte[] data = getDatas();
- image = convertToAWT(data, bitmapInfoOutput.biWidth, bitmapInfoOutput.biHeight );
- }
- return image;
- }
-
- /**
- * Get a JPG image of the decompressed datas as byte array.
- * @param imageQuality The image quality.
- * @return Byte array of the JPG image data.
- */
- public byte[] getJpgBuffer( float imageQuality ){
- BufferedImage bufferImage = getBufferedImage();
- ByteArrayOutputStream outputArray = new ByteArrayOutputStream();
-
- JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(outputArray);
- JPEGEncodeParam jpegParam = jpegEncoder.getDefaultJPEGEncodeParam(bufferImage);
- jpegParam.setQuality(imageQuality, false);
- jpegEncoder.setJPEGEncodeParam(jpegParam);
- try
- {
- jpegEncoder.encode(bufferImage);
- outputArray.close();
- } catch (java.io.IOException IOEx) {IOEx.printStackTrace();}
-
- return outputArray.toByteArray();
- }
-
- /**
- * Converts a byte data image to a AWT
- * <code>BufferedImage</code>.
- * @param data The byte data array to be converted.
- * @return The AWT image version.
- */
- private BufferedImage convertToAWT(byte[] data, int width, int height) {
- ColorModel colorModel = null;
- colorModel = new DirectColorModel(24, 0xFF, 0xFF00, 0xFF0000);
- BufferedImage bufferedImage = new BufferedImage(colorModel,
- colorModel.createCompatibleWritableRaster(width,height), false, null);
- WritableRaster raster = bufferedImage.getRaster();
- int[] pixelArray = new int[3];
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width * 3; x+=3) {
- pixelArray[0] = data[ ( height - y - 1) * width * 3 + x + 2 ];
- pixelArray[1] = data[ ( height - y - 1) * width * 3 + x + 1 ];
- pixelArray[2] = data[ ( height - y - 1) * width * 3 + x];
- raster.setPixel(x / 3, y, pixelArray);
- }
- }
- return bufferedImage;
- }
- private DeviceErrorListener errorListener = new DeviceErrorListener(){
- /**
- * Fire the listener that the device throws an error message.
- *
- * @param device
- * The device in error state.
- * @param errorId
- * The error id is one of the constants below.
- * @param errorMessage
- * The error message.
- */
- public void deviceError(Device device, int errorId, String errorMessage){
- logger.error(device + "=>" + errorId + ":" + errorMessage);
- }
- };
- private DeviceStatusListener statusListener = new DeviceStatusListener(){
- /**
- * Fire the listener that the device fire a status message.
- *
- * @param device
- * The device.
- * @param statusId
- * The status id is one of the constants below.
- * @param statusMessage
- * The status message.
- */
- public void deviceStatus(Device device, int statusId, String statusMessage){
- logger.info(device + "=>" + statusId + ":" + statusMessage);
- }
-
- };
- private DeviceFrameListener frameListener = new DeviceFrameListener(){
- /**
- *
- * @param device The device
- * @param videoHDR The video header
- * @param data The frame data.
- */
- public void frame(Device device, VideoHDR videoHDR, Memory data){
- try{
- synchronized (decompressedData) {
- //Decompress data
- decompressVideo.decompress(ICM.ICDECOMPRESS_HURRYUP,
- data, decompressedData);
- }
- }catch (CompressionException e) {
- logger.error(e);
- // Stop the capture
- capture = false;
- }
- }
- };
- /**
- * Check if the webcam capture continue.
- * @return True if capture frame processing, false otherwise.
- */
- public boolean isCapture() {
- return capture;
- }
- /**
- * Check if all is ok.
- * @return True if both device and decompressor are set.
- */
- public boolean isAvailable() {
- return available;
- }
- }
/**
* HTTP Webcam Server Version 1.0
* (c) 2008 Pierrick HYMBERT <pierrick.hymbert@gmail.com>
* This library is "PROVIDED AS IS" without guaranty of any kinds.
* Freely distribuable under the terms of an MIT-style license.
* Visit www.hypik.fr
*/
package org.hypik.webcamlib.test.server;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import org.apache.log4j.Logger;
import org.hypik.webcamlib.codecs.Codec;
import org.hypik.webcamlib.codecs.Codecs;
import org.hypik.webcamlib.compressor.CompressionException;
import org.hypik.webcamlib.compressor.DecompressVideo;
import org.hypik.webcamlib.compressor.jna.ICM;
import org.hypik.webcamlib.device.Device;
import org.hypik.webcamlib.device.listener.DeviceErrorListener;
import org.hypik.webcamlib.device.listener.DeviceFrameListener;
import org.hypik.webcamlib.device.listener.DeviceStatusListener;
import org.hypik.webcamlib.jna.User32.BitmapInfoHeader;
import org.hypik.webcamlib.jna.User32.VideoHDR;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.jna.Memory;
/**
* This class manage the webcam capture in a thread.
* After configure decompression video driver and webcam device.
* Instance can grab frame every rate seconds and decompress it.
* Frame data can be get back as RGB or Jpeg formats.
* @author Pierrick Hymbert <a href="mailto:pierrick.hymbert@gmail.com">Contact</a>
*
*/
public class WebcamCapture extends Thread {
/**
* The class logger.
*/
private Logger logger = Logger.getLogger(WebcamCapture.class);
/**
* The webcam device.
*/
private Device device;
/**
* Time between each frame.
*/
private int rate;
/**
* Indicates if we must continue capturing frame.
*/
private boolean capture = false;
/**
* Indicate that all is configured.
*/
private boolean available = false;
/**
* The video driver decompression.
*/
private DecompressVideo decompressVideo;
/**
* The decompressed data.
*/
private Memory decompressedData;
/**
* The output frame format.
*/
private BitmapInfoHeader bitmapInfoOutput;
/**
* Build a webcam capture.
* @param device The webcam
* @param rate The refresh rate.
*/
public WebcamCapture(Device device, int rate){
super("WCCapture #" + device.getIndex());
this.device = device;
this.rate = rate;
}
/**
* Ask the thread to stop the capture.
*/
public void stopCapture() {
this.capture = false;
}
/**
* Start the thread.
*/
public void startCapture() {
this.capture = true;
this.start();
}
/**
* Run webcam capture until stopCapture is called.
*/
public void run() {
try{
// Configure device and decompressor
logger.debug("Configuring webcam device...");
configureDevice();
logger.debug("Configuring frame decompressor...");
configureDecompressor();
logger.info("Webcam capture started.");
}catch( Exception e ){
logger.error(e);
capture = false;
}
available = true;
// Caputre loop
while( capture ){
// Sometimes call back functions are disabled.
device.forceCallBacks();
device.grabFrame();
try{
Thread.sleep(rate);
}catch (Exception e) {
logger.error(e);
capture = false;
}
}
logger.debug("Disconnecting webcam device...");
// Destroy window and disconnect
device.stop();
if( decompressVideo != null ){
try{
if( decompressVideo.isOpened() ){
logger.debug("Free decompression resources...");
decompressVideo.freeResources();
}
}catch (CompressionException e) {
logger.error(e);
}finally{
try{
if( decompressVideo.isOpened() ){
logger.debug("Closing decompression driver...");
decompressVideo.close();
}
}catch (CompressionException e) {
logger.error(e);
}
}
}
logger.info("Webcam capture stopped.");
}
/**
* Configure the device.
* @throws Exception
*/
private void configureDevice() throws Exception {
//Create the capture window
device.createCaptureWindow("Webcam device", 640, 480, 0, 0);
if( !device.isCaptureWindowCreated() )
throw new Exception("Unable to create webcam capture window!");
// Add listeners
device.addDeviceErrorListener( errorListener );
device.addDeviceStatusListener( statusListener );
device.addDeviceFrameListener( frameListener );
// Connect the window to device driver
device.connect();
if( !device.isConnected() )
throw new Exception("Unable to connect webcam capture window to the webcam driver!");
logger.debug("Webcam device is now connected to the webcam window. Do not close this program before calling stopCapture. Otherwise you can cause damage to your video device.");
device.setScale(false);
device.setPreview(false);
device.setPreviewRate(rate);
}
/**
* Configure the decompressor
* @throws CompressionException If no compressor found.
*/
private void configureDecompressor() throws CompressionException {
// Get back device bit map info input
BitmapInfoHeader bitmapInfoInput = device.getBitMapInfo().bmiHeader;
// Build the output format
bitmapInfoOutput = new BitmapInfoHeader();
bitmapInfoOutput.biBitCount = 24;
bitmapInfoOutput.biCompression = BitmapInfoHeader.BI_RGB;
bitmapInfoOutput.biHeight = bitmapInfoInput.biHeight;
bitmapInfoOutput.biWidth = bitmapInfoInput.biWidth;
bitmapInfoOutput.biSizeImage = bitmapInfoInput.biSizeImage = 0;
bitmapInfoOutput.biSize = bitmapInfoInput.biSize = bitmapInfoInput.size();
bitmapInfoOutput.biPlanes = bitmapInfoInput.biPlanes = 1;
bitmapInfoOutput.biXPelsPerMeter = bitmapInfoOutput.biYPelsPerMeter = bitmapInfoInput.biXPelsPerMeter = bitmapInfoInput.biYPelsPerMeter = 0;
bitmapInfoOutput.biClrUsed = bitmapInfoOutput.biClrImportant = bitmapInfoInput.biClrUsed = bitmapInfoInput.biClrImportant = 256;
Codec codecInput = Codecs.getCodec(bitmapInfoInput.biCompression);
if (codecInput != null){
logger.info("Device input codec is " + codecInput.getFourCCString() + ": " + codecInput.getLongName());
}
Codec codecOutput = Codecs.getCodec(bitmapInfoOutput.biCompression);
if (codecOutput != null){
logger.info("Device output codec is " + codecOutput.getFourCCString() + ": " + codecOutput.getLongName());
}
// Find a decompressor from the input format to the output
decompressVideo = DecompressVideo.findDecompressorVideo(bitmapInfoInput, bitmapInfoOutput);
//Check if a decompressor was found
if( decompressVideo == null ){
throw new CompressionException("No decompressor found from this input codec to this output.", ICM.ICERR_BADFORMAT);
}
// Allocate decompressed data buffer
decompressedData = new Memory(3
* bitmapInfoOutput.biHeight * bitmapInfoOutput.biWidth);
// Opening decompression driver
decompressVideo.open();
// Begin decompression
decompressVideo.begin(bitmapInfoInput, bitmapInfoOutput);
}
/**
* Read decompressed datas in memory.
* Warning NO Thread safe.
* @return The decompressed data.
*/
private byte[] getDatas(){
return decompressedData.getByteArray(0, (int) decompressedData.getSize());
}
/**
* A buffered image of the decompressed data.
* @return The decompressed image.
*/
public BufferedImage getBufferedImage(){
BufferedImage image = null;
synchronized (decompressedData) {
byte[] data = getDatas();
image = convertToAWT(data, bitmapInfoOutput.biWidth, bitmapInfoOutput.biHeight );
}
return image;
}
/**
* Get a JPG image of the decompressed datas as byte array.
* @param imageQuality The image quality.
* @return Byte array of the JPG image data.
*/
public byte[] getJpgBuffer( float imageQuality ){
BufferedImage bufferImage = getBufferedImage();
ByteArrayOutputStream outputArray = new ByteArrayOutputStream();
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(outputArray);
JPEGEncodeParam jpegParam = jpegEncoder.getDefaultJPEGEncodeParam(bufferImage);
jpegParam.setQuality(imageQuality, false);
jpegEncoder.setJPEGEncodeParam(jpegParam);
try
{
jpegEncoder.encode(bufferImage);
outputArray.close();
} catch (java.io.IOException IOEx) {IOEx.printStackTrace();}
return outputArray.toByteArray();
}
/**
* Converts a byte data image to a AWT
* <code>BufferedImage</code>.
* @param data The byte data array to be converted.
* @return The AWT image version.
*/
private BufferedImage convertToAWT(byte[] data, int width, int height) {
ColorModel colorModel = null;
colorModel = new DirectColorModel(24, 0xFF, 0xFF00, 0xFF0000);
BufferedImage bufferedImage = new BufferedImage(colorModel,
colorModel.createCompatibleWritableRaster(width,height), false, null);
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[3];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width * 3; x+=3) {
pixelArray[0] = data[ ( height - y - 1) * width * 3 + x + 2 ];
pixelArray[1] = data[ ( height - y - 1) * width * 3 + x + 1 ];
pixelArray[2] = data[ ( height - y - 1) * width * 3 + x];
raster.setPixel(x / 3, y, pixelArray);
}
}
return bufferedImage;
}
private DeviceErrorListener errorListener = new DeviceErrorListener(){
/**
* Fire the listener that the device throws an error message.
*
* @param device
* The device in error state.
* @param errorId
* The error id is one of the constants below.
* @param errorMessage
* The error message.
*/
public void deviceError(Device device, int errorId, String errorMessage){
logger.error(device + "=>" + errorId + ":" + errorMessage);
}
};
private DeviceStatusListener statusListener = new DeviceStatusListener(){
/**
* Fire the listener that the device fire a status message.
*
* @param device
* The device.
* @param statusId
* The status id is one of the constants below.
* @param statusMessage
* The status message.
*/
public void deviceStatus(Device device, int statusId, String statusMessage){
logger.info(device + "=>" + statusId + ":" + statusMessage);
}
};
private DeviceFrameListener frameListener = new DeviceFrameListener(){
/**
*
* @param device The device
* @param videoHDR The video header
* @param data The frame data.
*/
public void frame(Device device, VideoHDR videoHDR, Memory data){
try{
synchronized (decompressedData) {
//Decompress data
decompressVideo.decompress(ICM.ICDECOMPRESS_HURRYUP,
data, decompressedData);
}
}catch (CompressionException e) {
logger.error(e);
// Stop the capture
capture = false;
}
}
};
/**
* Check if the webcam capture continue.
* @return True if capture frame processing, false otherwise.
*/
public boolean isCapture() {
return capture;
}
/**
* Check if all is ok.
* @return True if both device and decompressor are set.
*/
public boolean isAvailable() {
return available;
}
}
Conclusion
Si cette source s'avère utilisée je me pencherai sur une manière de sécuriser l'accès à la webcam par mot de passe.
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | | | 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|
|