boofcv.io.webcamcapture.WebcamCaptureWebcamInterface Maven / Gradle / Ivy
/*
* Copyright (c) 2021, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package boofcv.io.webcamcapture;
import boofcv.io.image.ConvertBufferedImage;
import boofcv.io.image.SimpleImageSequence;
import boofcv.io.wrapper.WebcamInterface;
import boofcv.struct.image.ImageBase;
import boofcv.struct.image.ImageType;
import com.github.sarxos.webcam.Webcam;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* Wrapper around webcam capture which allows its images to be used inside the {@link SimpleImageSequence}.
*
* @author Peter Abeles
*/
public class WebcamCaptureWebcamInterface implements WebcamInterface {
@Override
public > SimpleImageSequence
open( String device, int width, int height, ImageType imageType ) {
if (device != null) {
Webcam webcam;
try {
int which = Integer.parseInt(device);
webcam = Webcam.getWebcams().get(which);
} catch (NumberFormatException ignore) {
webcam = UtilWebcamCapture.findDevice(device);
}
if (webcam == null) {
throw new RuntimeException("Can't find webcam with ID or name at " + device);
}
try {
if (width >= 0 && height >= 0) {
UtilWebcamCapture.adjustResolution(webcam, width, height);
}
webcam.open();
return new SimpleSequence<>(webcam, imageType);
} catch (RuntimeException ignore) {
}
}
return new SimpleSequence<>(device, width, height, imageType);
}
@SuppressWarnings({"NullAway.Init"})
public static class SimpleSequence> implements SimpleImageSequence {
Webcam webcam;
int width, height;
T output;
BufferedImage bufferedImage;
int frames = 0;
public SimpleSequence( String device, int width, int height, ImageType imageType ) {
this(UtilWebcamCapture.openDefault(width, height), imageType);
}
public SimpleSequence( Webcam webcam, ImageType imageType ) {
this.webcam = webcam;
Dimension d = webcam.getDevice().getResolution();
width = d.width;
height = d.height;
output = imageType.createImage(width, height);
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public boolean hasNext() {
bufferedImage = webcam.getImage();
return bufferedImage != null;
}
@Override
public T next() {
if (bufferedImage == null)
bufferedImage = webcam.getImage();
ConvertBufferedImage.convertFrom(bufferedImage, output, true);
return output;
}
@Override
public T getImage() {
return output;
}
@Override
public void close() {
webcam.close();
}
@Override
public int getFrameNumber() {
return frames;
}
@Override
public void setLoop( boolean loop ) {}
@Override
public ImageType getImageType() {
return output.getImageType();
}
@Override
public void reset() {}
@Override
public BufferedImage getGuiImage() {
return bufferedImage;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy