Hi everyone,
I believe everyone knows the animated image called GIF (or Graphics Interchange Format). As an IT developer, have you ever thought about how you can develop an app that captures videos and converts the images into a GIF file? This is very easy in JAVA. I'll show you how to create an app that captures videos (e.g. YouTube or any animated GIF) on the screen and lets you create your own GIF files that can be edited, etc.
First of all, an animation is a sequence of images that are slightly different from each other and occur in a rapid timeframe. Normally the animation is vivid when the sequence of timeframes is around 25 frames per second. There are two ways to capture images on the screen: an external camera or a screenshot. The first option is out of the question. The second option basically simulates an external camera that can take the screenshots fast enough to display a vivid animation (i.e. 25 time frames per second).
Then some words about coding: Coding is a question of belief. There is someone who relies only on framework and IDE which produce a heap of small APIs grouped into "package". The others, for example, love simple but convenient editors like Notepad++ or Jedit. Whatever the people like, coding is similar to building of a house: first the foundation, thenthe house frame, then the roof, then the door and windows, etc. None of the works can be done in parallel. For this reason, I will show you the codes and basic development of an application without going into frameworks or IDEs.
The basic development environment:
1) Working directory: for example: C:\myapp\gif (similar in Linux), where the sources are created.
2) Compile: Open a CMD window (terminal under Linux).
CMD window
3) Set CLASSPATH. For example, on WINDOWS: Create a batch file setCP.bat as follows:
The 3rd step includes the current (a dot) working directory and the subdirectory “classes” (.\classes) to the Java variable CLASSPATH (Similar to a shell file for Linux).
The source that simulates a camera is called CamCorder.java (Cam for Camera and Coder by reCorder). As mentioned above, we lay the foundation for CamCorder with the control panel, then the camera frame is the “lens”. From them we begin to develop the functionalities of a virtual camera. The source is written in JAVA JFX. At the end of this tutorial, I'll post the complete package of CamCorder here in ZIP format.
The CamCorder Control Panel (CamCorder.java)
The Tools codes (Tools.java):
CamCorder ControlPanel
Explanation:
- All JFX buttons are activated by lambda expression (e -> { ... });
- Line scene.getStylesheets().add("gif.css") uses the style from the external file gif.css
- Line panel.setAlwaysOnTop(true) always places the ControlPanel above other applications.
(to be continued)
I believe everyone knows the animated image called GIF (or Graphics Interchange Format). As an IT developer, have you ever thought about how you can develop an app that captures videos and converts the images into a GIF file? This is very easy in JAVA. I'll show you how to create an app that captures videos (e.g. YouTube or any animated GIF) on the screen and lets you create your own GIF files that can be edited, etc.
First of all, an animation is a sequence of images that are slightly different from each other and occur in a rapid timeframe. Normally the animation is vivid when the sequence of timeframes is around 25 frames per second. There are two ways to capture images on the screen: an external camera or a screenshot. The first option is out of the question. The second option basically simulates an external camera that can take the screenshots fast enough to display a vivid animation (i.e. 25 time frames per second).
Then some words about coding: Coding is a question of belief. There is someone who relies only on framework and IDE which produce a heap of small APIs grouped into "package". The others, for example, love simple but convenient editors like Notepad++ or Jedit. Whatever the people like, coding is similar to building of a house: first the foundation, thenthe house frame, then the roof, then the door and windows, etc. None of the works can be done in parallel. For this reason, I will show you the codes and basic development of an application without going into frameworks or IDEs.
The basic development environment:
1) Working directory: for example: C:\myapp\gif (similar in Linux), where the sources are created.
2) Compile: Open a CMD window (terminal under Linux).
3) Set CLASSPATH. For example, on WINDOWS: Create a batch file setCP.bat as follows:
Mã:
set CLASSPATH=%CLASSPATH%;.;.\classes
The source that simulates a camera is called CamCorder.java (Cam for Camera and Coder by reCorder). As mentioned above, we lay the foundation for CamCorder with the control panel, then the camera frame is the “lens”. From them we begin to develop the functionalities of a virtual camera. The source is written in JAVA JFX. At the end of this tutorial, I'll post the complete package of CamCorder here in ZIP format.
The CamCorder Control Panel (CamCorder.java)
Java:
public class CamCorder extends Application {
//
public void start(Stage panel) {
this.panel = panel;
panel.setOnCloseRequest(ev -> {
ev.consume(); // Ignore the close by pressing X in the rightmost upper corner
});
...
focus = Tools.getButton("FOCUS", "Focus Screen Image");
focus.setOnAction(ev -> {
});
start = Tools.getButton("START", "Start Recording Screen");
start.setOnAction(ev -> {
if (!focused) {
System.out.println("Nowhere on SCREEN is focused.");
return;
}
if (idle) {
start.setText("STOP");
} else {
start.setText("START");
}
});
//
load = Tools.getButton("LOAD", "Load from GIF/GZIP image file");
load.setOnAction(ev -> {
});
edit = Tools.getButton("EDIT", "Edit BufferedImages");
edit.setOnAction(ev -> {
editing();
});
undo = Tools.getButton("UNDO", "Undo Editing");
undo.setOnAction(ev -> {
});
show = Tools.getButton("SHOW", "Show animated BufferedImages");
show.setOnAction(ev -> {
});
save = Tools.getButton(new Image(CamCorder.class.getResourceAsStream("save.png")),
"Save to GIF/GZIP file");
save.setOnAction(ev -> {
});
reset = Tools.getButton("RESET", "RESET");
reset.setOnAction(ev -> {
});
Button quit = Tools.getButton("QUIT", "Quit & Exit CamCorder");
quit.setOnAction(ev -> {
panel.close();
Platform.exit();
System.exit(0);
});
VBox bBox = new VBox(5);
bBox.setAlignment(Pos.CENTER);
// Insets(top, right, bottom, left)
bBox.setPadding(new Insets(10, 0, 15, 0));
bBox.getChildren().addAll(focus, start, load, edit, undo, show, save, reset, quit);
Scene scene = new Scene(bBox, 145, 350);
scene.getStylesheets().add("gif.css");
panel.getIcons().add(new Image(CamCorder.class.getResourceAsStream("sanduhr.gif")));
panel.setAlwaysOnTop(true);
panel.setResizable(false);
panel.setScene(scene);
panel.setY(0);
panel.setX(0);
panel.show();
Java:
public class Tools {
...
/**
@param icon JFX Image or String
@param txt String as tooltip or button name (if icon = null)
@return Button
*/
public static Button getButton(Object icon, String txt) {
Button button = null;
if (icon == null || icon instanceof String) {
button = new Button(icon == null? txt:(String)icon);
// direct CSS style
button.setStyle("-fx-font-size:11; -fx-font-weight: bold;");
} else try {
button = new Button( );
button.setGraphic(new ImageView((Image) icon));
} catch (Exception ex) {
button = new Button(txt);
button.setStyle("-fx-font-weight: bold;");
}
Tooltip tt = new Tooltip();
tt.setText(txt);
tt.setStyle("-fx-base: #AE3522; -fx-text-fill: orange; -fx-font-cb: bold;");
button.setTooltip(tt);
return button;
}
CamCorder ControlPanel
Explanation:
- All JFX buttons are activated by lambda expression (e -> { ... });
- Line scene.getStylesheets().add("gif.css") uses the style from the external file gif.css
- Line panel.setAlwaysOnTop(true) always places the ControlPanel above other applications.
(to be continued)
Sửa lần cuối: