|
| 1 | +import java.awt.Dimension; |
| 2 | +import java.awt.Transparency; |
| 3 | +import java.awt.color.ColorSpace; |
| 4 | +import java.awt.image.BufferedImage; |
| 5 | +import java.awt.image.ComponentColorModel; |
| 6 | +import java.awt.image.ComponentSampleModel; |
| 7 | +import java.awt.image.DataBuffer; |
| 8 | +import java.awt.image.DataBufferByte; |
| 9 | +import java.awt.image.Raster; |
| 10 | +import java.awt.image.WritableRaster; |
| 11 | +import java.nio.ByteBuffer; |
| 12 | + |
| 13 | +import javax.swing.JFrame; |
| 14 | + |
| 15 | +import com.github.sarxos.webcam.Webcam; |
| 16 | +import com.github.sarxos.webcam.WebcamPanel; |
| 17 | +import com.github.sarxos.webcam.WebcamPanel.ImageSupplier; |
| 18 | +import com.github.sarxos.webcam.WebcamResolution; |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * This example demonstrates a possibility to provide custom {@link ImageSupplier} which will be |
| 23 | + * used to obtain image displayed on the {@link WebcamPanel}. In this specific example we will |
| 24 | + * implement it in such a way that it won't be using {@link Webcam#getImage()} but |
| 25 | + * {@link Webcam#getImageBytes(ByteBuffer)} instead. This may be required in some cases if you need |
| 26 | + * very fast access to underlying buffer. |
| 27 | + */ |
| 28 | +public class WebcamPanelWithImageSupllierExample { |
| 29 | + |
| 30 | + public static void main(String[] args) throws InterruptedException { |
| 31 | + |
| 32 | + final Dimension size = WebcamResolution.VGA.getSize(); |
| 33 | + |
| 34 | + final Webcam webcam = Webcam.getDefault(); |
| 35 | + webcam.setViewSize(size); |
| 36 | + webcam.open(); |
| 37 | + |
| 38 | + final ImageSupplier supplier = new ImageSupplier() { |
| 39 | + |
| 40 | + private final int[] imageOffset = new int[] { 0 }; |
| 41 | + private final int[] bandOffsets = new int[] { 0, 1, 2 }; |
| 42 | + private final int[] bits = { 8, 8, 8 }; |
| 43 | + private final int dataType = DataBuffer.TYPE_BYTE; |
| 44 | + private final int length = size.width * size.height * 3; |
| 45 | + private final ComponentSampleModel sampleModel = new ComponentSampleModel(dataType, size.width, size.height, 3, size.width * 3, bandOffsets); |
| 46 | + private final ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 47 | + private final ComponentColorModel colorModel = new ComponentColorModel(colorSpace, bits, false, false, Transparency.OPAQUE, dataType); |
| 48 | + private final ByteBuffer buffer = ByteBuffer.allocateDirect(length); |
| 49 | + |
| 50 | + @Override |
| 51 | + public BufferedImage get() { |
| 52 | + |
| 53 | + webcam.getImageBytes(buffer); |
| 54 | + |
| 55 | + final byte[] bytes = new byte[length]; |
| 56 | + final byte[][] data = new byte[][] { bytes }; |
| 57 | + |
| 58 | + buffer.get(bytes); |
| 59 | + buffer.clear(); |
| 60 | + |
| 61 | + final DataBufferByte dataBuffer = new DataBufferByte(data, bytes.length, imageOffset); |
| 62 | + final WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, null); |
| 63 | + final BufferedImage image = new BufferedImage(colorModel, raster, false, null); |
| 64 | + |
| 65 | + return image; |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + final WebcamPanel panel = new WebcamPanel(webcam, size, true, supplier); |
| 70 | + panel.setFPSDisplayed(true); |
| 71 | + panel.setDisplayDebugInfo(true); |
| 72 | + panel.setImageSizeDisplayed(true); |
| 73 | + panel.setMirrored(true); |
| 74 | + |
| 75 | + final JFrame window = new JFrame("Test webcam panel"); |
| 76 | + window.add(panel); |
| 77 | + window.setResizable(true); |
| 78 | + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 79 | + window.pack(); |
| 80 | + window.setVisible(true); |
| 81 | + } |
| 82 | +} |
0 commit comments