Last active
April 20, 2016 06:11
-
-
Save tuti107/e4804adbc2362f34238a70e5af805e76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayInputStream; | |
import java.io.InputStream; | |
import javax.imageio.ImageIO; | |
import javax.swing.ImageIcon; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import org.opencv.core.Core; | |
import org.opencv.core.Mat; | |
import org.opencv.core.MatOfByte; | |
import org.opencv.core.Rect; | |
import org.opencv.core.Size; | |
import org.opencv.imgcodecs.Imgcodecs; | |
import org.opencv.imgproc.Imgproc; | |
import org.opencv.videoio.VideoCapture; | |
public class BlendApp { | |
public static void main(String[] args) { | |
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |
VideoCapture capL = new VideoCapture(1); | |
VideoCapture capR = new VideoCapture(3); | |
if (!capL.isOpened() || !capR.isOpened()) { | |
System.out.println("ERROR: can not open video device"); | |
System.exit(-1); | |
} | |
while(true) { | |
Mat cameraFrameL = new Mat(); | |
Mat cameraFrameR = new Mat(); | |
capL.read(cameraFrameL); | |
capR.read(cameraFrameR); | |
Mat combined = new Mat(cameraFrameL.rows() + cameraFrameR.rows(), cameraFrameL.cols(), cameraFrameL.type()); | |
cameraFrameL.copyTo(combined.submat(new Rect(0, 0, cameraFrameL.cols(), cameraFrameL.rows()))); | |
cameraFrameR.copyTo(combined.submat(new Rect(0, cameraFrameL.rows(), cameraFrameR.cols(), cameraFrameR.rows()))); | |
showResult(combined); | |
} | |
} | |
static JFrame frame = null; | |
static JLabel label = null; | |
public static void showResult(Mat img) { | |
Imgproc.resize(img, img, new Size(1280, 1440)); | |
MatOfByte matOfByte = new MatOfByte(); | |
Imgcodecs.imencode(".jpg", img, matOfByte); | |
byte[] byteArray = matOfByte.toArray(); | |
BufferedImage bufImage = null; | |
try { | |
InputStream in = new ByteArrayInputStream(byteArray); | |
bufImage = ImageIO.read(in); | |
if (frame == null) { | |
frame = new JFrame(); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there | |
//frame.setExtendedState(JFrame.MAXIMIZED_BOTH); | |
frame.setUndecorated(true); | |
label = new JLabel(new ImageIcon(bufImage)); | |
frame.getContentPane().add(label); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
else { | |
label.setIcon(new ImageIcon(bufImage)); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment