在 Java 中是否可以将 BufferedImage 转换为 IMG 数据 URI?

发布于 2024-11-15 23:10:18 字数 393 浏览 1 评论 0原文

我使用以下示例代码创建了一个图形图像。

BufferedImage bi = new BufferedImage(50,50,BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2d = bi.createGraphics();

// Draw graphics. 

g2d.dispose();
// BufferedImage now has my image I want.

此时我有 BufferedImage,我想将其转换为 IMG 数据 URI。这可能吗?例如..

<IMG SRC="data:image/png;base64,[BufferedImage data here]"/>

I have created a graphical image with the following sample code.

BufferedImage bi = new BufferedImage(50,50,BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2d = bi.createGraphics();

// Draw graphics. 

g2d.dispose();
// BufferedImage now has my image I want.

At this point I have BufferedImage which I want to convert into an IMG Data URI. Is this possible? For example..

<IMG SRC="data:image/png;base64,[BufferedImage data here]"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

却一份温柔 2024-11-22 23:10:18

尚未测试,但类似的东西应该可以做到:

ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "PNG", out);
byte[] bytes = out.toByteArray();

String base64bytes = Base64.encode(bytes);
String src = "data:image/png;base64," + base64bytes;

Java 有很多不同的 base64 编解码器实现。我使用 MigBase64 取得了很好的结果。

Not tested, but something like this ought to do it:

ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "PNG", out);
byte[] bytes = out.toByteArray();

String base64bytes = Base64.encode(bytes);
String src = "data:image/png;base64," + base64bytes;

There are lots of different base64 codec implementations for Java. I've had good results with MigBase64.

亚希 2024-11-22 23:10:18

您可以使用此解决方案,它不使用任何外部库。又短又干净!它使用 Java 6 库 (DatatypeConverter)。为我工作!

ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(image, "png", output);
DatatypeConverter.printBase64Binary(output.toByteArray());

You could use this solution which doesn't use any external libraries. Short and clean! It uses a Java 6 library (DatatypeConverter). Worked for me!

ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(image, "png", output);
DatatypeConverter.printBase64Binary(output.toByteArray());
郁金香雨 2024-11-22 23:10:18

我使用 Webdriver,获取验证码,如下所示:

// formatName -> png
// pathname -> C:/Users/n/Desktop/tmp/test.png
public static String getScreenshot(WebDriver driver, String formatName, String pathname) {
    try {
        WebElement element = driver.findElement(By.xpath("//*[@id=\"imageCodeDisplayId\"]"));
        File screenshot = element.getScreenshotAs(OutputType.FILE);
        // base64 data
        String base64Str = ImageUtil.getScreenshot(screenshot.toString()); 
        return base64Str;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static String getScreenshot(String imgFile) {
    InputStream in;
    byte[] data = null; 
    try {
        in = new FileInputStream(imgFile);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String base64Str = new String(Base64.getEncoder().encode(data));
    if (StringUtils.isAnyBlank(base64Str)) {
        return null;
    }
    if (!base64Str.startsWith("data:image/")) {
        base64Str = "data:image/jpeg;base64," + base64Str;
    }
    return base64Str;
}

I use Webdriver, get captcha, like this below:

// formatName -> png
// pathname -> C:/Users/n/Desktop/tmp/test.png
public static String getScreenshot(WebDriver driver, String formatName, String pathname) {
    try {
        WebElement element = driver.findElement(By.xpath("//*[@id=\"imageCodeDisplayId\"]"));
        File screenshot = element.getScreenshotAs(OutputType.FILE);
        // base64 data
        String base64Str = ImageUtil.getScreenshot(screenshot.toString()); 
        return base64Str;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static String getScreenshot(String imgFile) {
    InputStream in;
    byte[] data = null; 
    try {
        in = new FileInputStream(imgFile);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String base64Str = new String(Base64.getEncoder().encode(data));
    if (StringUtils.isAnyBlank(base64Str)) {
        return null;
    }
    if (!base64Str.startsWith("data:image/")) {
        base64Str = "data:image/jpeg;base64," + base64Str;
    }
    return base64Str;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文