-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#392 refactor code for html2image conversion
the test is taken from PR #393 comments.
- Loading branch information
Showing
7 changed files
with
116 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
flying-saucer-core/src/test/java/org/xhtmlrenderer/swing/Java2DRendererTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.xhtmlrenderer.swing; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Base64; | ||
|
||
import static java.awt.Color.RED; | ||
import static java.awt.Color.WHITE; | ||
import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR; | ||
import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR_PRE; | ||
import static java.awt.image.BufferedImage.TYPE_INT_ARGB; | ||
import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class Java2DRendererTest { | ||
private static final Logger log = LoggerFactory.getLogger(Java2DRendererTest.class); | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {TYPE_INT_ARGB, TYPE_INT_ARGB_PRE, TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR_PRE}) | ||
void convertHtmlToImage_withResizedEmbeddedABGRImage(int imageType) throws Exception { | ||
BufferedImage smallImg = create2PixelImage(imageType); | ||
String html = """ | ||
<html><body> | ||
<img width="100" src="data:image/png;base64,%s"/> | ||
<h1>Hello</h1></body></html>""".formatted(imageBase64(smallImg)); | ||
BufferedImage htmlAsImage = Java2DRenderer.htmlAsImage(html, 100); | ||
|
||
File result = new File("target/%s.%s.png".formatted(getClass().getSimpleName(), imageType)); | ||
ImageIO.write(htmlAsImage, "png", result); | ||
log.info("Generated image from html: {}", result.getAbsolutePath()); | ||
|
||
assertEquals(RED.getRGB(), htmlAsImage.getRGB(75, 25)); | ||
assertEquals(WHITE.getRGB(), htmlAsImage.getRGB(25, 25)); | ||
} | ||
|
||
private static BufferedImage create2PixelImage(int imageType) { | ||
BufferedImage img = new BufferedImage(2, 1, imageType); | ||
img.setRGB(0, 0, 0); | ||
img.setRGB(1, 0, 0xFFFF0000); | ||
return img; | ||
} | ||
|
||
private static String imageBase64(BufferedImage img) throws IOException { | ||
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream(96)) { | ||
ImageIO.write(img, "png", bytes); | ||
return Base64.getEncoder().encodeToString(bytes.toByteArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters