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