java 旋转 png base64_Java BufferedImage to PNG format Base64 String

问题

I'm trying to get a screenshot output as a base64 encoded string but not getting very far. The code I have so far uses a Base64 library ( http://iharder.sourceforge.net/current/java/base64/ ):

Robot robot = new Robot();

Rectangle r = new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() );

BufferedImage bi = robot.createScreenCapture(r);

ByteArrayOutputStream os = new ByteArrayOutputStream();

OutputStream b64 = new Base64.OutputStream(os);

ImageIO.write(bi, "png", os);

ByteArrayOutputStream out = new ByteArrayOutputStream();

out.writeTo(b64);

String result = out.toString("UTF-8");

Each time I run this, "result" is always an empty string but I don't understand why. Any ideas?

Note: I don't want to have to write the png to a file on disk.

回答1:

I followed xehpuk's answer but had issues with certain images having the last few rows of pixels missing when rendered in certain browsers via a data url (Chrome and Firefox, Safari seemed to render them fine). I suspect this is because the browser is doing it's best to interpret the data but the last few bytes of data was missing so it shows what it can.

The wrapping of the output stream seems to be the cause of this problem. The documentation for Base64.wrap(OutputStream os) explains:

It is recommended to promptly close the returned output stream after use, during which it will flush all possible leftover bytes to the underlying output stream.

So depending on the length of the data, it's possible the last few bytes are not flushed from the stream because close() isn't called on it. My solution to this was to not bother wrapping the stream and just encode the stream directly:

public static String imgToBase64String(final RenderedImage img, final String formatName)

{

final ByteArrayOutputStream os = new ByteArrayOutputStream();

try

{

ImageIO.write(img, formatName, os);

return Base64.getEncoder().encodeToString(os.toByteArray());

}

catch (final IOException ioe)

{

throw new UncheckedIOException(ioe);

}

}

This resolved the issues with the missing rows of pixels when rendered in a browser.

回答2:

The following statement works in the wrong direction:

out.writeTo(b64);

It overwrites the Base 64 data with the empty byte array of out.

What's the purpose of out anyway? I don't think you need it.

Update:

And you write the image directly to os instead of writing through the Base 64 encoder.

The following code should work:

...

ByteArrayOutputStream os = new ByteArrayOutputStream();

OutputStream b64 = new Base64.OutputStream(os);

ImageIO.write(bi, "png", b64);

String result = os.toString("UTF-8");

回答3:

Base64 encoding and decoding of images using Java 8:

public static String imgToBase64String(final RenderedImage img, final String formatName) {

final ByteArrayOutputStream os = new ByteArrayOutputStream();

try {

ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));

return os.toString(StandardCharsets.ISO_8859_1.name());

} catch (final IOException ioe) {

throw new UncheckedIOException(ioe);

}

}

public static BufferedImage base64StringToImg(final String base64String) {

try {

return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64String)));

} catch (final IOException ioe) {

throw new UncheckedIOException(ioe);

}

}

Use it like this for your screenshot scenario:

final Robot robot = new Robot();

final Rectangle r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

final BufferedImage bi = robot.createScreenCapture(r);

final String base64String = imgToBase64String(bi, "png");

回答4:

This works for me:

Encode Image to Base64 String

public static String encodeToString(BufferedImage image, String type) {

String imageString = null;

ByteArrayOutputStream bos = new ByteArrayOutputStream();

try {

ImageIO.write(image, type, bos);

byte[] imageBytes = bos.toByteArray();

Base64.Encoder encoder = Base64.getEncoder();

imageString = encoder.encodeToString(imageBytes);

bos.close();

} catch (IOException e) {

e.printStackTrace();

}

return imageString;

}

Decode Base64 String to Image

public static BufferedImage decodeToImage(String imageString) {

BufferedImage image = null;

byte[] imageByte;

try {

Base64.Decoder decoder = Base64.getDecoder();

imageByte = decoder.decode(imageString);

ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);

image = ImageIO.read(bis);

bis.close();

} catch (Exception e) {

e.printStackTrace();

}

return image;

}

来源:https://stackoverflow.com/questions/7178937/java-bufferedimage-to-png-format-base64-string

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值