Skip to content

Commit

Permalink
added wait-parameter
Browse files Browse the repository at this point in the history
added GET request
  • Loading branch information
nort3x committed Feb 28, 2022
1 parent 34cf8ee commit 18b0c36
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 55 deletions.
97 changes: 64 additions & 33 deletions src/main/java/com/github/nort3x/jwebrender/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,100 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@RestController
public class Controller {

@Autowired
JWebRenderContext jWebRenderContext;

@ApiResponse(
description = "format supports: PEG, PNG, GIF, BMP and WBMP"
)
@PostMapping("/html")
public void render(@RequestBody byte[] html,
@RequestParam int width,
@RequestParam int height,
@RequestParam int x_offset,
@RequestParam int y_offset,
@RequestParam String format,
@RequestParam double pixelScale, HttpServletResponse response) throws Exception {
private void renderAndSend(
int width,
int height,
int x_offset,
int y_offset,
String format,
double pixelScale,
long wait,
HttpServletResponse response,
InputStream inputStream
) throws Exception {

var formatLower = format.toLowerCase();
if ( formatLower.equals("png") ||
if (formatLower.equals("png") ||
formatLower.equals("jpg") ||
formatLower.equals("gif") ||
formatLower.equals("bmp")
)
response.setContentType("image/"+format);
response.setContentType("image/" + format);
else
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

jWebRenderContext.doRenderAndDump(width, height, x_offset, y_offset, format.toUpperCase(), pixelScale, new ByteArrayInputStream(html), response.getOutputStream());
jWebRenderContext.doRenderAndDump(width, height, x_offset, y_offset, format.toUpperCase(), pixelScale,wait, inputStream, response.getOutputStream());
}


@ApiResponse(
description = "format supports: PEG, PNG, GIF, BMP and WBMP"
)
@PostMapping("/url")
public void renderUrl(@RequestBody String url,
@PostMapping("/html")
public void render(@RequestBody byte[] html,
@RequestParam int width,
@RequestParam int height,
@RequestParam int x_offset,
@RequestParam int y_offset,
@RequestParam String format,
@RequestParam double pixelScale, HttpServletResponse response) throws Exception {
@RequestParam double pixelScale,
@RequestParam long wait,
HttpServletResponse response) throws Exception {

var formatLower = format.toLowerCase();
if ( formatLower.equals("png") ||
formatLower.equals("jpg") ||
formatLower.equals("gif") ||
formatLower.equals("bmp")
)
response.setContentType("image/"+format);
else
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
jWebRenderContext.doRenderAndDump(width, height, x_offset, y_offset, format.toUpperCase(), pixelScale, DownloadHtmlPage.download(url), response.getOutputStream());
renderAndSend(width, height, x_offset, y_offset, format, pixelScale, wait, response, new ByteArrayInputStream(html));
}


@ApiResponse(
description = "format supports: PEG, PNG, GIF, BMP and WBMP"
)
@PostMapping("/url")
public void renderUrl(@RequestBody String url,
@RequestParam int width,
@RequestParam int height,
@RequestParam int x_offset,
@RequestParam int y_offset,
@RequestParam String format,
@RequestParam double pixelScale,
@RequestParam long wait,
HttpServletResponse response) throws Exception {

renderAndSend(width, height, x_offset, y_offset, format, pixelScale,wait, response, DownloadHtmlPage.download(url));
}


@ApiResponse(
description = "format supports: PEG, PNG, GIF, BMP and WBMP"
)
@GetMapping("/url")
public void renderUrlGet(
@RequestParam String url,
@RequestParam int width,
@RequestParam int height,
@RequestParam int x_offset,
@RequestParam int y_offset,
@RequestParam String format,
@RequestParam double pixelScale,
@RequestParam long wait,
HttpServletResponse response) throws Exception {

renderUrl(URLDecoder.decode(url, StandardCharsets.UTF_8), width, height, x_offset, y_offset, format, pixelScale,wait, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void construct() throws InterruptedException {



public void doRenderAndDump(int width, int height, int xOffset, int yOffset, String format, double pixelScale, InputStream inps, OutputStream oups) throws Exception {
public void doRenderAndDump(int width, int height, int xOffset, int yOffset, String format, double pixelScale,long wait, InputStream inps, OutputStream oups) throws Exception {
CountDownLatch cdl = new CountDownLatch(1);
AtomicReference<Exception> exceptionAtomicReference = new AtomicReference<>(null);
Platform.runLater(() -> {
Expand All @@ -60,6 +60,7 @@ public void doRenderAndDump(int width, int height, int xOffset, int yOffset, Str
yOffset,
format,
pixelScale,
wait,
inps,
oups,
() -> {
Expand Down
45 changes: 24 additions & 21 deletions src/main/java/com/github/nort3x/jwebrender/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ public class Renderer {
int offsetY;

double pixelScale;
long wait;

InputStream inputStream;
OutputStream outputStream;

String format;
Runnable onFinish;
Consumer<Exception> onError;

public Renderer(int width, int height, int xOffset, int yOffset, String format, double pixelScale, InputStream inputStream, OutputStream outputStream, Runnable onFinish,Consumer<Exception> onError) {
public Renderer(int width, int height, int xOffset, int yOffset, String format, double pixelScale, long wait, InputStream inputStream, OutputStream outputStream, Runnable onFinish, Consumer<Exception> onError) {
this.width = width;
this.height = height;
this.pixelScale = pixelScale;
Expand All @@ -49,72 +51,73 @@ public Renderer(int width, int height, int xOffset, int yOffset, String format,
this.offsetX = xOffset;
this.offsetY = yOffset;
this.onError = onError;
this.wait = wait;
}

public void render(Stage primaryStage) {
WebView wv = new WebView();

wv.setMinWidth(width);
wv.setMaxWidth(width);
wv.setPrefWidth(width);
wv.setMinWidth(width+offsetX);
wv.setMaxWidth(width+offsetX);
wv.setPrefWidth(width+offsetX);

wv.setPrefHeight(height);
wv.setMinHeight(height);
wv.setMaxHeight(height);
wv.setPrefHeight(height+offsetY);
wv.setMinHeight(height+offsetY);
wv.setMaxHeight(height+offsetY);


CountDownLatch cdl = new CountDownLatch(1);
CountDownLatch cdl = new CountDownLatch(1);

wv.getEngine().setJavaScriptEnabled(true);
wv.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
if(newValue.equals(Worker.State.SUCCEEDED))
if (newValue.equals(Worker.State.SUCCEEDED))
cdl.countDown();
}
});
try {
wv.getEngine().loadContent(new String(inputStream.readAllBytes()));
}catch (Exception e){
} catch (Exception e) {
onError.accept(e);
}
HBox p = new HBox();
HBox p = new HBox();
p.getChildren().add(wv);
HBox.setHgrow(wv, Priority.ALWAYS);
Scene s = new Scene(p,1,1);
Scene s = new Scene(p, 1, 1);
primaryStage.setScene(s);
primaryStage.show();
primaryStage.toBack();

wv.getEngine().setUserStyleSheetLocation(getClass().getClassLoader().getResource("style.css").toExternalForm());

new Thread(()->{
new Thread(() -> {

try {
cdl.await();
Thread.sleep(1000);
Thread.sleep(1000 + wait);
} catch (InterruptedException e) {
e.printStackTrace();
}


Platform.runLater(()->{
Platform.runLater(() -> {

int im_width = (int)Math.rint(pixelScale*(wv.getWidth()- offsetX));
int im_height = (int)Math.rint(pixelScale*(wv.getHeight() - offsetY));
int im_width = (int) Math.rint(pixelScale * width);
int im_height = (int) Math.rint(pixelScale * height);
SnapshotParameters sp = new SnapshotParameters();
sp.setViewport(new Rectangle2D(offsetX*pixelScale, offsetY*pixelScale,im_width,im_height));
sp.setViewport(new Rectangle2D(offsetX * pixelScale, offsetY * pixelScale, im_width, im_height));
sp.setTransform(Transform.scale(pixelScale, pixelScale));
BufferedImage bufferedImage = new BufferedImage(im_width,im_height,BufferedImage.TYPE_INT_ARGB);
BufferedImage bufferedImage = new BufferedImage(im_width, im_height, BufferedImage.TYPE_INT_ARGB);


SwingFXUtils.fromFXImage(wv.snapshot(sp,new WritableImage(im_width, im_height)), bufferedImage);
SwingFXUtils.fromFXImage(wv.snapshot(sp, new WritableImage(im_width, im_height)), bufferedImage);
SwingFXUtils.toFXImage(bufferedImage, new WritableImage(im_width, im_height));
try {
ImageIO.write(bufferedImage, format, outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
} finally {
onFinish.run();
}

Expand Down

0 comments on commit 18b0c36

Please sign in to comment.