2017年9月15日 星期五
Java Screen Capture
private static void screencapture() throws IOException, AWTException {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
try{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) { //get each monitor
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc = gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
Rectangle rec = gc[i].getBounds(); //get each screen resolution
BufferedImage image = new Robot().createScreenCapture(rec);
String timeStamp = new SimpleDateFormat("MMdd_HHmm_ss").format(Calendar.getInstance().getTime()); //yyyyMMdd_HHmmss
ImageIO.write(image, "png", new File("Screen_"+j+"_"+ rec.width +"x"+rec.height +"_"+timeStamp +".png"));
System.out.println("Screen:"+j+"_"+ rec.width +"x"+rec.height +"_"+timeStamp +".png");
//Save file at %userprofile%\Documents\NetBeansProjects\JavaSikuli
}
}
} catch (AWTException ex) {
Logger.getLogger(JavaSnapshot.class.getName()).log(Level.SEVERE, null, ex);
}
}
2016年1月17日 星期日
OpenCV 2.4.11 with JAVA and Netbeans
1. Add opencv-2411.jar and opencv_java2411.dll to your libraries in netbeans.
ex: C:\Users\Desktop\opencv\build\java\2.4.11\opencv-2411.jar
C:\Users\Desktop\opencv\build\java\2.4.11\x64
2. Modify VM Options in Run of netbeans
ex: -Djava.library.path="C:\Users\Desktop\opencv\build\java\2.4.11\x64"
3. add below code in Main
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
4. Modify source code and copy files "lbpcascade_frontalface.xml" and "lena.png" to your src folder in netbeans.
From:
CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath());
Mat image = Highgui.imread(getClass().getResource("/lena.png").getPath());
To: (For short term solution if you can't find the file that will show null point result)
CascadeClassifier faceDetector = new CascadeClassifier(".\\src\\lbpcascade_frontalface.xml");
Mat image = Highgui.imread(".\\src\\lena.png");
4. Run it and that will generate a faceDetection.png file if your settings is well.
BTW, if using openCV 3.1.0 below source code should be changed, but I still got fail.
From
Mat image = Highgui.imread(".\\src\\lena.png");
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
To
Mat image = Imgcodecs.imread("C:\\Users\\kyc1109\\Documents\\NetBeansProjects\\HelloOpenCV\\src\\lena.png");
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
Reference page :
http://stackoverflow.com/questions/26845885/java-opencv-org-opencv-core-core-rectangle-method-missing
http://stackoverflow.com/questions/17134126/open-cv-detecting-faces-in-java
Netbeans settings:
http://www.codeproject.com/Tips/717283/How-to-Use-OpenCV-with-Java-under-NetBeans-IDE
ex: C:\Users\Desktop\opencv\build\java\2.4.11\opencv-2411.jar
C:\Users\Desktop\opencv\build\java\2.4.11\x64
2. Modify VM Options in Run of netbeans
ex: -Djava.library.path="C:\Users\Desktop\opencv\build\java\2.4.11\x64"
3. add below code in Main
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
4. Modify source code and copy files "lbpcascade_frontalface.xml" and "lena.png" to your src folder in netbeans.
From:
CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath());
Mat image = Highgui.imread(getClass().getResource("/lena.png").getPath());
To: (For short term solution if you can't find the file that will show null point result)
CascadeClassifier faceDetector = new CascadeClassifier(".\\src\\lbpcascade_frontalface.xml");
Mat image = Highgui.imread(".\\src\\lena.png");
4. Run it and that will generate a faceDetection.png file if your settings is well.
BTW, if using openCV 3.1.0 below source code should be changed, but I still got fail.
From
Mat image = Highgui.imread(".\\src\\lena.png");
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
To
Mat image = Imgcodecs.imread("C:\\Users\\kyc1109\\Documents\\NetBeansProjects\\HelloOpenCV\\src\\lena.png");
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
Reference page :
http://stackoverflow.com/questions/26845885/java-opencv-org-opencv-core-core-rectangle-method-missing
http://stackoverflow.com/questions/17134126/open-cv-detecting-faces-in-java
Netbeans settings:
http://www.codeproject.com/Tips/717283/How-to-Use-OpenCV-with-Java-under-NetBeans-IDE
訂閱:
文章 (Atom)