List of usage examples for org.opencv.imgcodecs Imgcodecs imencode
public static boolean imencode(String ext, Mat img, MatOfByte buf, MatOfInt params)
From source file:edu.ucue.tfc.Modelo.VideoProcessor.java
public Image convertCvMatToImage(Mat frameToConvert) throws IOException { if (!buffer.empty()) { buffer.release();//from ww w. j a v a 2 s.c om } try { Imgproc.resize(frameToConvert, frameToConvert, frameSize); Imgcodecs.imencode(".png", frameToConvert, buffer, params); } catch (Exception e) { System.out.println(e.getMessage()); } image = ImageIO.read(new ByteArrayInputStream(buffer.toArray())); if (!frameToConvert.empty()) { frameToConvert.release(); } return image; }
From source file:hu.unideb.fksz.VideoProcessor.java
License:Open Source License
/** * Returns an {@code Image}, converted from a {@code Mat}. * * @param frameToConvert The frame to be converted to a {@code Image} * @return The {@code Image}, converted from a {@code Mat} *//*from w ww . jav a 2s . c o m*/ public Image convertCvMatToImage(Mat frameToConvert) { if (!buffer.empty()) { buffer.release(); } try { Imgproc.resize(frameToConvert, frameToConvert, frameSize); Imgcodecs.imencode(".jpg", frameToConvert, buffer, params); } catch (Exception e) { logger.error(e.getMessage()); } fxImage = new Image(new ByteArrayInputStream(buffer.toArray())); if (!frameToConvert.empty()) { frameToConvert.release(); ///// } return fxImage; }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.UDPVideoServer.java
License:Open Source License
/** * Sends an image to all connected clients. This blocks until the image is * sent./*ww w .ja v a 2 s.c o m*/ * * @param image the image to send * @throws IOException if the image could not be sent */ public void sendImage(Mat image) throws IOException { // Only send if server is running. if (running) { // Encode the image as a JPEG. Imgcodecs.imencode(".jpg", image, compressionBuffer, qualityParams); int size = (int) compressionBuffer.total() * compressionBuffer.channels(); String ip = VisionParameters.getStreamIP(); // Resize the Java buffer to fit the data if necessary if (size > socketBuffer.length) { socketBuffer = new byte[size]; packet = new DatagramPacket(socketBuffer, size, InetAddress.getByName(ip), PORT); } // Copy the OpenCV data to a Java byte[]. compressionBuffer.get(0, 0, socketBuffer); if (!ip.equals(oldIP)) { oldIP = ip; packet.setAddress(InetAddress.getByName(ip)); } try { packet.setLength(size); socket.send(packet); } catch (IllegalArgumentException ex) { // No idea why this is thrown sometimes. Race condition? System.out.println("Illegal size: " + size); } } else { throw new IllegalStateException("Server must be running to send an image."); } }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.VideoServer.java
License:Open Source License
/** * Sends an image to all connected clients. This blocks until the image is * sent.// ww w. j ava 2 s . c o m * * @param image the image to send * @throws IOException if the image could not be sent */ public void sendImage(Mat image) throws IOException { // Only send if server is running. if (running) { // Encode the image as a JPEG. Imgcodecs.imencode(".jpg", image, compressionBuffer, qualityParams); int size = (int) compressionBuffer.total() * compressionBuffer.channels(); // Resize the Java buffer to fit the data if necessary if (size > socketBuffer.length) { socketBuffer = new byte[size]; } // Copy the OpenCV data to a Java byte[]. compressionBuffer.get(0, 0, socketBuffer); synchronized (responseBufferOutputStream) { ++imageIndex; responseBufferOutputStream.reset(); // Reset response buffer to the end of the header responseBufferOutputStream.markReset(); // Write content length responseBufferOutputStream.write(("Content-Length: " + size + "\r\n\r\n").getBytes()); // Write image to response buffer responseBufferOutputStream.write(socketBuffer, 0, size); responseBufferOutputStream.notifyAll(); } } else { throw new IllegalStateException("Server must be running to send an image."); } }
From source file:org.usfirst.frc.team4276.robot.VideoServer.java
License:Open Source License
/** * Sends an image to all connected clients. This blocks until the image is * sent./*from ww w . jav a 2 s .c o m*/ * * @param image * the image to send * @throws IOException * if the image could not be sent */ public void sendImage(Mat image) throws IOException { // Only send if server is running. if (running) { // Only send if at least one client is connected. if (!clientSockets.isEmpty()) { // Encode the image as a JPEG. Imgcodecs.imencode(".jpg", image, compressionBuffer, qualityParams); int size = (int) compressionBuffer.total() * compressionBuffer.channels(); // Resize the Java buffer to fit the data if necessary if (size > socketBuffer.length) { socketBuffer = new byte[size]; } // Copy the OpenCV data to a Java byte[]. compressionBuffer.get(0, 0, socketBuffer); responseBufferOutputStream.reset(); // Reset response buffer to the end of the header responseBufferOutputStream.markReset(); // Write content length responseBufferOutputStream.write(("Content-Length: " + size + "\r\n\r\n").getBytes()); // Write image to response buffer responseBufferOutputStream.write(socketBuffer, 0, size); synchronized (clientSockets) { // Send to all clients. for (int i = 0; i < clientSockets.size(); i++) { AsynchronousSocketChannel cs = clientSockets.get(i); Future<Integer> future = clientSocketFutures.get(i); if (future == null || future.isDone()) { try { if (future != null) { future.get(); } clientSocketFutures.set(i, cs.write(ByteBuffer.wrap(responseBufferOutputStream.toByteArray()))); } catch (InterruptedException | ExecutionException ex) { cs.close(); clientSocketFutures.remove(i); clientSockets.remove(i); } } } } } } else { throw new IllegalStateException("Server must be running to send an image."); } }