@@ -0,0 +1,2221 @@
+import org.opencv.core.Core;%0Aimport org.opencv.core.Mat;%0A//import org.opencv.videoio.VideoCapture;%0Aimport org.opencv.highgui.VideoCapture;%0Aimport org.opencv.highgui.Highgui;%0A%0Aimport java.awt.image.BufferedImage;%0Aimport java.awt.image.DataBufferByte;%0Aimport java.awt.Graphics2D;%0A%0Apublic class Webcam implements ImageHolder%7B%0A%09static %7B%0A%09%09System.loadLibrary(Core.NATIVE_LIBRARY_NAME);%0A%09%7D%0A%09     %0A%09private Mat currentImg = new Mat();%0A%09private BufferedImage img;%0A%09private VideoCapture capture;%0A%09%0A%09private BufferedImage stdImg;%0A%09private Graphics2D g2;%0A%0A%09public Webcam()%7B%0A%09%09this(0);%0A%09%7D%0A%09%0A%09public Webcam(int deviceNumber)%7B%0A%09%09capture = new VideoCapture(deviceNumber);%0A%09%09if (capture.isOpened())%7B%0A%09%09%09//Capture is open%0A%09%09%09%0A%09%09%09//Get width and height of capture for bufferedImage%0A%09%09%09int width = (int)capture.get(Highgui.CV_CAP_PROP_FRAME_WIDTH);%0A%09%09%09int height = (int)capture.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT);%0A%09%09%09%0A%09%09%09//Initialize BufferedImage, Note: Mats use BGR, not RGB%0A%09%09%09if (width + height %3E 1)%7B%0A%09%09%09%09img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);%0A%09%09%09%09stdImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);%0A%09%09%09%09g2 = stdImg.createGraphics();%0A%09%09%09%7Delse%7B%0A%09%09%09%09System.err.println(%22VideoCapture could not get Frame Size%22);%0A%09%09%09%7D%0A%09%09%7Delse%7B%0A%09%09%09System.err.println(%22VideoCapture is not opened.%22);%0A%09%09%7D%0A%09%7D%0A%09private void capture()%7B%0A%09%09if(capture.isOpened())%7B%0A%09%09%09capture.read(currentImg);%0A%09%09%09if (!currentImg.empty())%7B%0A%09%09%09%09convertMat();%0A%09%09%09%09g2.drawImage(img, 0, 0, null);//Draw the image onto the standard BufferedImage.%0A%09%09%09%09//g2.dispose();%0A%09%09%09%09/*try%7B%0A%09%09%09%09%09javax.imageio.ImageIO.write(img, %22JPG%22, new java.io.File(%22test%22+(int)(Math.random()*1000)+%22.jpg%22));%0A%09%09%09%09%7Dcatch(Exception ex)%7B%0A%09%09%09%09%09ex.printStackTrace();%0A%09%09%09%09%7D*/%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09private void convertMat()%7B%0A%09%09byte%5B%5D inPixels = new byte%5BcurrentImg.width() * currentImg.height() * currentImg.channels()%5D;%0A%09%09byte%5B%5D outPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();%0A%09%09currentImg.get(0, 0, inPixels);%09%09%0A%09%09System.arraycopy(inPixels, 0, outPixels, 0, inPixels.length);%0A%09%7D%0A%09%0A%09@Override%0A%09public BufferedImage getCurrentFrame() %7B//Return a reference to the buffered image%0A%09%09capture();%0A%09%09//return img;%0A%09%09return stdImg;%0A%09%7D%0A%7D%0A

Add Webcam