I got a problem when I'm trying to record my screen.
Here's a code to begin with :
When I'm pressing a button which will record this is the code :
if (record) { System.out.println("RECORDING!");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
while (record) {
count++;
try {
BufferedImage image = new Robot()
.createScreenCapture(new Rectangle((int) width, (int) height));
File f = new File(
"LOCATION" + count + ".jpg");
ImageIO.write(image, "JPEG", f);
} catch (AWTException e1) {
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
When I'm stopping the record then I'm starting to make the video (.avi) which is this code :
record = false;
System.out.println("RECORDING HAS STOPPED!");
try {
Thread.sleep(3500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JpegImagesToMovie imageToMovie = new JpegImagesToMovie();
Vector<String> imgList = new Vector<String>();
File f2 = new File("LOCATION");
File[] fileList = f2.listFiles();
for (int i = 0; i < fileList.length; i++) {
imgList.add(fileList[i].getAbsolutePath());
MediaLocator ml;
if ((ml = imageToMovie.createMediaLocator("a.avi")) == null) {
System.exit(0);
}
imageToMovie.doIt((int) width, (int) height, 1, imgList, ml);
}
}
When It makes the video this is what I get and you can see that the images are completely out of position
Setting the track format to: JPEG
- create DataSink for: file:LOCATION\a.avi
start processing...
- reading image file: LOCATION\image10.jpg
read 118168 bytes.
- reading image file: LOCATION\image100.jpg
read 115899 bytes.
- reading image file: LOCATION\image101.jpg
read 115899 bytes.
- reading image file: LOCATION\image102.jpg
read 115899 bytes.
- reading image file: LOCATION\image11.jpg
read 118168 bytes.
- reading image file: LOCATION\image12.jpg
read 118168 bytes.
- reading image file: LOCATION\image13.jpg
read 118168 bytes.
- reading image file: LOCATION\image14.jpg
read 118168 bytes.
- reading image file: LOCATION\image15.jpg
read 118168 bytes.
- reading image file: LOCATION\image16.jpg
read 118065 bytes.
- reading image file: LOCATION\image17.jpg
read 118065 bytes.
- reading image file: LOCATION\image18.jpg
read 118065 bytes.
- reading image file: LOCATION\image19.jpg
read 118065 bytes.
- reading image file: LOCATION\image2.jpg
read 106079 bytes.
- reading image file: LOCATION\image20.jpg
read 118065 bytes.
- reading image file: LOCATION\image21.jpg
read 118065 bytes.
- reading image file: LOCATION\image22.jpg
read 118065 bytes.
- reading image file: LOCATION\image23.jpg
read 118040 bytes.
- reading image file: LOCATION\image24.jpg
read 118169 bytes.
- reading image file: LOCATION\image25.jpg
read 118150 bytes.
- reading image file: LOCATION\image26.jpg
read 118120 bytes.
- reading image file: LOCATION\image27.jpg
read 118168 bytes.
- reading image file: LOCATION\image28.jpg
read 118168 bytes.
- reading image file: LOCATION\image29.jpg
read 118168 bytes.
- reading image file: LOCATION\image3.jpg
read 106612 bytes.
- reading image file: LOCATION\image30.jpg
read 118168 bytes.
- reading image file: LOCATION\image31.jpg
read 118168 bytes.
- reading image file: LOCATION\image32.jpg
read 118265 bytes.
- reading image file: LOCATION\image33.jpg
read 118263 bytes.
- reading image file: LOCATION\image34.jpg
read 118283 bytes.
- reading image file: LOCATION\image35.jpg
read 118283 bytes.
- reading image file: LOCATION\image36.jpg
read 118283 bytes.
- reading image file: LOCATION\image37.jpg
read 118283 bytes.
- reading image file: LOCATION\image38.jpg
read 118274 bytes.
- reading image file: LOCATION\image39.jpg
read 118270 bytes.
- reading image file: LOCATION\image4.jpg
read 106612 bytes.
- reading image file: LOCATION\image40.jpg
read 118209 bytes.
- reading image file: LOCATION\image41.jpg
read 118280 bytes.
- reading image file: LOCATION\image42.jpg
read 118203 bytes.
- reading image file: LOCATION\image43.jpg
read 118202 bytes.
- reading image file: LOCATION\image44.jpg
read 118165 bytes.
- reading image file: LOCATION\image45.jpg
read 118197 bytes.
- reading image file: LOCATION\image46.jpg
read 118251 bytes.
- reading image file: LOCATION\image47.jpg
read 118270 bytes.
- reading image file: LOCATION\image48.jpg
read 118172 bytes.
- reading image file: LOCATION\image49.jpg
read 118162 bytes.
- reading image file: LOCATION\image5.jpg
read 106612 bytes.
- reading image file: LOCATION\image50.jpg
read 118125 bytes.
- reading image file: LOCATION\image51.jpg
read 118101 bytes.
- reading image file: LOCATION\image52.jpg
read 118092 bytes.
- reading image file: LOCATION\image53.jpg
read 118096 bytes.
- reading image file: LOCATION\image54.jpg
read 118075 bytes.
- reading image file: LOCATION\image55.jpg
read 118060 bytes.
- reading image file: LOCATION\image56.jpg
read 118165 bytes.
- reading image file: LOCATION\image57.jpg
read 116149 bytes.
- reading image file: LOCATION\image58.jpg
read 117707 bytes.
- reading image file: LOCATION\image59.jpg
read 123115 bytes.
- reading image file: LOCATION\image6.jpg
read 106612 bytes.
- reading image file: LOCATION\image60.jpg
read 123115 bytes.
- reading image file: LOCATION\image61.jpg
read 123115 bytes.
- reading image file: LOCATION\image62.jpg
read 123575 bytes.
- reading image file: LOCATION\image63.jpg
read 123637 bytes.
- reading image file: LOCATION\image64.jpg
read 123488 bytes.
- reading image file: LOCATION\image65.jpg
read 123488 bytes.
- reading image file: LOCATION\image66.jpg
read 123637 bytes.
- reading image file: LOCATION\image67.jpg
read 123637 bytes.
- reading image file: LOCATION\image68.jpg
read 123481 bytes.
- reading image file: LOCATION\image69.jpg
read 123524 bytes.
- reading image file: LOCATION\image7.jpg
read 106612 bytes.
- reading image file: LOCATION\image70.jpg
read 123533 bytes.
- reading image file: LOCATION\image71.jpg
read 123115 bytes.
- reading image file: LOCATION\image72.jpg
read 124844 bytes.
- reading image file: LOCATION\image73.jpg
read 121337 bytes.
- reading image file: LOCATION\image74.jpg
read 119950 bytes.
- reading image file: LOCATION\image75.jpg
read 119528 bytes.
- reading image file: LOCATION\image76.jpg
read 119528 bytes.
- reading image file: LOCATION\image77.jpg
read 119528 bytes.
- reading image file: LOCATION\image78.jpg
read 119528 bytes.
- reading image file: LOCATION\image79.jpg
read 126779 bytes.
- reading image file: LOCATION\image8.jpg
read 118168 bytes.
- reading image file: LOCATION\image80.jpg
read 126479 bytes.
- reading image file: LOCATION\image81.jpg
read 124403 bytes.
- reading image file: LOCATION\image82.jpg
read 124797 bytes.
- reading image file: LOCATION\image83.jpg
read 125287 bytes.
- reading image file: LOCATION\image84.jpg
read 125276 bytes.
- reading image file: LOCATION\image85.jpg
read 125450 bytes.
- reading image file: LOCATION\image86.jpg
read 125178 bytes.
- reading image file: LOCATION\image87.jpg
read 131495 bytes.
- reading image file: LOCATION\image88.jpg
read 127806 bytes.
- reading image file: LOCATION\image89.jpg
read 114765 bytes.
- reading image file: LOCATION\image9.jpg
read 118168 bytes.
- reading image file: LOCATION\image90.jpg
read 114801 bytes.
- reading image file: LOCATION\image91.jpg
read 115281 bytes.
- reading image file: LOCATION\image92.jpg
read 115281 bytes.
- reading image file: LOCATION\image93.jpg
read 115281 bytes.
- reading image file: LOCATION\image94.jpg
read 117940 bytes.
- reading image file: LOCATION\image95.jpg
read 116221 bytes.
- reading image file: LOCATION\image96.jpg
read 115899 bytes.
- reading image file: LOCATION\image97.jpg
read 115899 bytes.
- reading image file: LOCATION\image98.jpg
read 115899 bytes.
- reading image file: LOCATION\image99.jpg
read 115899 bytes.
Done reading all images.
...done processing.
I can't find the problem and even where to begin with. What can I do to fix this?
Thanks In advance.
Change this:
File f = new File(
"LOCATION" + count + ".jpg");
ImageIO.write(image, "JPEG", f);
To :
File f = new File(
"LOCATION" + String.format("%04d",count) + ".jpg");
ImageIO.write(image, "JPEG", f);
That will add extra zeros so LOCATION0009.jpg is before LOCATION0010.jpg unlike LOCATION9.jpg and LOCATION10.jpg. Since they are ordered alphabetically instead of numerically. It looks like 4 digits is enough but for longer movies you might want more digits. Alternatively you could keep the same file names sort the file list with a custom comparitor but it requires slightly more code.