001    package com.github.sarxos.webcam.ds.cgt;
002    
003    import java.nio.ByteBuffer;
004    
005    import org.slf4j.Logger;
006    import org.slf4j.LoggerFactory;
007    
008    import com.github.sarxos.webcam.WebcamDevice;
009    import com.github.sarxos.webcam.WebcamDevice.BufferAccess;
010    import com.github.sarxos.webcam.WebcamDriver;
011    import com.github.sarxos.webcam.WebcamTask;
012    
013    
014    public class WebcamReadBufferTask extends WebcamTask {
015    
016            private static final Logger LOG = LoggerFactory.getLogger(WebcamReadBufferTask.class);
017    
018            private volatile ByteBuffer buffer = null;
019    
020            public WebcamReadBufferTask(WebcamDriver driver, WebcamDevice device) {
021                    super(driver, device);
022            }
023    
024            public ByteBuffer getBuffer() {
025                    try {
026                            process();
027                    } catch (InterruptedException e) {
028                            LOG.debug("Image buffer request interrupted", e);
029                            return null;
030                    }
031                    return buffer;
032            }
033    
034            @Override
035            protected void handle() {
036    
037                    WebcamDevice device = getDevice();
038                    if (!device.isOpen()) {
039                            return;
040                    }
041    
042                    if (!(device instanceof BufferAccess)) {
043                            return;
044                    }
045    
046                    buffer = ((BufferAccess) device).getImageBytes();
047            }
048    }