public static BufferedImage bufferedImage(Mat m) { int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; } BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); m.get( 0, 0, ((DataBufferByte) image.getRaster().getDataBuffer()).getData()); // get all the pixels return image; }
/** Capture images and run color processing through here */ public void capture() { VideoCapture camera = new VideoCapture(); camera.set(12, -20); // change contrast, might not be necessary // CaptureImage image = new CaptureImage(); camera.open(0); // Useless if (!camera.isOpened()) { System.out.println("Camera Error"); // Determine whether to use System.exit(0) or return } else { System.out.println("Camera OK"); } boolean success = camera.read(capturedFrame); if (success) { try { processWithContours(capturedFrame, processedFrame); } catch (Exception e) { System.out.println(e); } // image.processFrame(capturedFrame, processedFrame); // processedFrame should be CV_8UC3 // image.findCaptured(processedFrame); // image.determineKings(capturedFrame); int bufferSize = processedFrame.channels() * processedFrame.cols() * processedFrame.rows(); byte[] b = new byte[bufferSize]; processedFrame.get(0, 0, b); // get all the pixels // This might need to be BufferedImage.TYPE_INT_ARGB img = new BufferedImage( processedFrame.cols(), processedFrame.rows(), BufferedImage.TYPE_INT_RGB); int width = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_WIDTH); int height = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT); // img.getRaster().setDataElements(0, 0, width, height, b); byte[] a = new byte[bufferSize]; System.arraycopy(b, 0, a, 0, bufferSize); Highgui.imwrite("camera.jpg", processedFrame); System.out.println("Success"); } else System.out.println("Unable to capture image"); camera.release(); }