Here is my problem - I have a desktop application written in JavaFX. I need to show a full-screen webpage to an user and save the rendered page as PNG. I need to save the whole page (e.g. resolution 1920×3500).
Now I'm using Selenium and Firefox to do this. It works fine but there is one big disadvantage - the user must have Firefox installed on his machine.
I've tried to render the webpage using WebView by JavaFX and take a .snapshot(). This would be great but this approach doesn't give me the whole page, only the visible part of the WebView. Is there any approach how to get the whole page snapshot using WebView? Or any other ideas how to do that? Thanks.
You may want to check out this post. I don't know if it is working or not, but it seems like a reasonable solution.
After a lot of searching and scraping several pieces together I found that the only problem I had with an example some SO-poster posted in an oracle forum was that the size of the webview was fixed and that my css used in the html (not in JavaFX) needed.
overflow-x: hidden;
overflow-y: hidden;
to hide the last scrollbar.
So I come up with the following snapshot method (application with animation just as example of your application) See if it works for your sizes, otherwise you might scale javafx.scene.web.WebView.setZoom(double value) the webview down to something that works, you might lose some resolution, but at least have the whole picture:
package application;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javafx.animation.Animation;
import javafx.animation.PauseTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.web.WebView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
public class WebViewSnapshot extends Application {
BorderPane rootPane;
TranslateTransition animation;
#Override
public void start(Stage primaryStage) {
Rectangle rect = new Rectangle(50, 50, 50, 50);
rect.setFill(Color.CORAL);
animation = createAnimation(rect);
Button snapshotButton = new Button("Take snapshot");
Pane pane = new Pane(rect);
pane.setMinSize(600, 150);
rootPane = new BorderPane(pane, null, null, snapshotButton, new Label("This is the main scene"));
snapshotButton.setOnAction(e -> {
// html file being shown in webview
File htmlFile = new File ("generated/template.html");
// the resulting snapshot png file
File aboutFile = new File ("generated/about.png");
generate(htmlFile, aboutFile, 1280, 720);
});
BorderPane.setAlignment(snapshotButton, Pos.CENTER);
BorderPane.setMargin(snapshotButton, new Insets(5));
Scene scene = new Scene(rootPane);
primaryStage.setScene(scene);
primaryStage.show();
}
private TranslateTransition createAnimation(Rectangle rect) {
TranslateTransition animation = new TranslateTransition(Duration.seconds(1), rect);
animation.setByX(400);
animation.setCycleCount(Animation.INDEFINITE);
animation.setAutoReverse(true);
animation.play();
return animation;
}
public void generate(File htmlFile, File outputFile, double width, double height) {
animation.pause();
// rootPane is the root of original scene in an FXML controller you get this when you assign it an id
rootPane.setEffect(new GaussianBlur());
Stage primaryStage = (Stage)rootPane.getScene().getWindow();
// creating separate webview holding same html content as in original scene
WebView webView = new WebView();
// with the size I want the snapshot
webView.setPrefSize(width, height);
AnchorPane snapshotRoot = new AnchorPane(webView);
webView.getEngine().load(htmlFile.toURI().toString());
Stage popupStage = new Stage(StageStyle.TRANSPARENT);
popupStage.initOwner(primaryStage);
popupStage.initModality(Modality.APPLICATION_MODAL);
// this popup doesn't really show anything size = 1x1, it just holds the snapshot-webview
popupStage.setScene(new Scene(snapshotRoot, 1, 1));
// pausing to make sure the webview/picture is completely rendered
PauseTransition pt = new PauseTransition(Duration.seconds(2));
pt.setOnFinished(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
WritableImage image = webView.snapshot(null, null);
// writing a png to outputFile
// writing a JPG like this will result in a pink JPG, see other posts
// if somebody can scrape me simple code to convert it ARGB to RGB or something
String format = "png";
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), format, outputFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
rootPane.setEffect(null);
popupStage.hide();
animation.play();
}
}
});
// pausing, after pause onFinished event will take + write snapshot
pt.play();
// GO!
popupStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related
I've a website, here I display an <iframe> with video player.
Thats videos are uploaded to Powvideo. Powvideo put some popups with ads in the player.
Now I'm developing a desktop aplication. I use JavaFX to load the url of my site.
The problem is, when try to see some video, this popups open in same page, so the user can't view anything. The page redirect to the ads windows.
My code:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.scene.image.Image;
public class Main extends Application {
VBox vb = new VBox();
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) {
vb.setId("root");
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
String url = "http://xxx.xxxxx.net/";
engine.load(url);
vb.setAlignment(Pos.CENTER);
vb.getChildren().addAll(browser);
Scene scene = new Scene(vb, 1024, 600);
stage.setTitle("Series EnLatino");
stage.setScene(scene);
stage.show();
stage.getIcons().add(new Image("iconoAplicacionWindows.png"));
}
}
This are a test of the player, with these uglies ads http://powvideo.net/embed-d45fwobyzyup.html
When you play the video or click anywhere of player, the popads open.
In Webaplication, this popads open in same windows.
How can prevent this? Or how can make that popups open under the webapp?.
It's this possible?
Thanks.
You can try providing your own WebEngine "popup" or "alert" handlers, and then silently ignoring those requests..
https://docs.oracle.com/javafx/2/api/javafx/scene/web/WebEngine.html#setOnAlert(javafx.event.EventHandler)
I'm showing date picker using following ane
https://github.com/freshplanet/ANE-DatePicker
In iphone it always show datepicker at the bottom. I take a look at AirDatePicker.as, in iPad I can do it, but not found any way for custom positioning in iphone.
This can be done using FPNativeUI.ane:
package control
{
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.system.Capabilities;
import ru.flashpress.nui.FPNativeUI;
import ru.flashpress.nui.view.control.FPDatePicker;
import ru.flashpress.nui.events.FPDatePickerEvent;
import ru.flashpress.nui.view.system.FPWindowView;
public class DatePickerProgrammatically extends Sprite
{
private var datePicker:FPDatePicker;
public function DatePickerProgrammatically()
{
FPNativeUI.init();
//
var w:int = Capabilities.screenResolutionX;
//
var bounds:Rectangle = new Rectangle(0, 100, w, 400);
FPWindowView.window.screen.boundsFlashToNative(bounds);
//
datePicker = new FPDatePicker();
datePicker.frame = bounds;
//
datePicker.stage.addChild(datePicker);
datePicker.addEventListener(FPDatePickerEvent.VALUE_CHANGED, valueChangeHandler);
}
private function valueChangeHandler(event:FPDatePickerEvent):void
{
trace(event.date);
}
}
}
I am developing an IOS app in Flash CS6. I'm trying to load an external swf. The preloader loads the external swf. The loading itself seems to work. After loading the external swf, I add its movieclips to the stage. But strangely, on iOS devices, the movieclips are not added to stage.
Here's the code for the preloader:
package {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.utils.getDefinitionByName;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
public class Test extends MovieClip {
private var lobbyBgAssetsLoader:Loader = new Loader() ;
private var ldrContext:LoaderContext;
public function Test() {
var urlStr:String = "cards.swf";
lobbyBgAssetsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LobbyBgAssetsswfLoaded);
lobbyBgAssetsLoader.load(new URLRequest(urlStr));
ldrContext = new LoaderContext(false,ApplicationDomain.currentDomain,null);
}
private function LobbyBgAssetsswfLoaded(e:Event):void
{
txt.text = "loaded complete test ";
var logoSmall:Bitmap = new Bitmap();
var classDefinition:Class = lobbyBgAssetsLoader.contentLoaderInfo.applicationDomain.getDefinition("cardBg") as Class;
txt.text = "after load ";
var img:MovieClip = new classDefinition() as MovieClip;
this.addChild(img);
}
}
}
All actionscript gets converted into native Obj-C code when you compile your app for iOS. So even if your app is able to load an SWF file from the internet, the actionscript inside will not be converted to Obj-C.
Apple does not allow the actual flash player inside an iOS app, so an app cannot play back SWF content.
I'm having an issue with libGDX's otherwise great scene2d ui library when running my app in landscape mode. My program works as expected in both the android simulator and the desktop environment, but when I run it on the iOS simulator or a real device, the main menu is drawn as if the phone were still in the portrait orientation, and the just cropped to fit in a landscape view. At first, I assumed I had just screwed up, as I'm still learning the api, but the app works just as expected on android. I have configured my game to run in landscape mode on both iOS and android.
On the android simulator, the menu works as expected. The simulator is in landscape orientation, and properly displays the menu.
On iOS, the menu is off center and unclickable.
I pretty sure that the problem has to do with landscape mode, because everything works fine in portrait orientation. Here's my code for the MainMenuScreen, which extends a ScreenAdapter.
package bakpakin.techgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
/**
* Created by Calvin on 9/25/14.
*/
public class MainMenuScreen extends ScreenAdapter {
private TechGame game;
private Stage stage;
private Skin skin;
private Table table;
public MainMenuScreen(TechGame game) {
this.game = game;
}
#Override
public void show() {
this.stage = new Stage();
this.skin = new Skin();
skin.add("default", Assets.superScript48);
skin.add("title", Assets.superScript128);
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.font = skin.getFont("default");
textButtonStyle.pressedOffsetY = 4;
skin.add("default", textButtonStyle);
Label.LabelStyle labelStyle = new Label.LabelStyle();
labelStyle.font = skin.getFont("title");
labelStyle.fontColor = Color.CYAN;
skin.add("default", labelStyle);
// Create a table that fills the screen. Everything else will go inside this table.
table = new Table();
table.setFillParent(true);
stage.addActor(table);
final TextButton play = new TextButton("Play", skin);
final TextButton about = new TextButton("About", skin);
final TextButton quit = new TextButton("Quit", skin);
play.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Assets.button1Sound.play(1, 1, 0);
}
});
about.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Assets.button1Sound.play(1, 1, 0);
}
});
quit.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Assets.button1Sound.play(1, 1, 0);
Gdx.app.exit();
}
});
final Label title = new Label("My Game", skin);
VerticalGroup vg = new VerticalGroup();
vg.pad(60);
table.add(title);
table.row();
table.add(vg);
vg.addActor(play);
vg.addActor(about);
vg.addActor(quit);
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
GL20 gl = Gdx.gl;
gl.glClearColor(0, 0, 0, 1);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
IOS 7 or 8? I heard there are some iOS 8 rendering issues
Apparently this is fixed in the latest snapshot (see github.com/libgdx/libgdx/issues/2386). With a new stable release promised real-soon-now, I'm sure the fix will be folded in if you're not brave enough to run snapshot builds
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flare.basic.Scene3D;
import flare.basic.Viewer3D;
import flare.core.Pivot3D;
import flare.basic.Scene3D;
import flare.basic.Viewer3D;
import flare.core.Pivot3D;
[SWF(frameRate = 60, width = 800, height = 450, backgroundColor = 0x000000)]
/**
* Starting the project.
*/
public class MyFirstApp extends Sprite
{
private var scene:Scene3D;
private var planet:Pivot3D;
private var astronaut:Pivot3D;
public function MyFirstApp()
{
trace("Hello Flare3D");
}
}
}
I am trying to get Flare3D to work in an ActionScript Mobile Project in Flash Builder.When I run the project I am recieving an error message that says ... " VerifyError: Error #1014: Class mx.core::ByteArrayAsset could not be found. " All I have in this code is three Vairables, I have removed all other code and still I recieve this message. I have added Flare3D into the ActionScript BulderPath. My Goal is to take the Yellow Planet Tutorial and run it on iOS Air Simulator. http://www.flare3d.com/demos/yellowplanet/.... If I dont include the 3 variables I get the trace statement to execute with no error message. How does adding three variable cause this error, or rather what am I overlooking here?
Fixed error mx.core::ByteArrayAsset by adding the Flex SDK into my Actionscript Mobile project. By using Scene3D this must be linked to the project.