/**
* 直接输出图片为base64码
*
* @param key
* @param imageurl
* @param width
* @param height
* @throws IOException
* @算法:
* @日期:2015年1月28日
*/
public void putImage(String key, Image image) throws IOException {
// Image->bufferreImage
BufferedImage bimg = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
// bufferImage->base64
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bimg, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
String base64Img = encoder.encode(outputStream.toByteArray());
// 输出
this.content = this.content.replace("@{" + key + "}",
"<img src= \"data:image/png;base64," + base64Img + "\"/>");
}
/**
* 直接输出图片为base64码
*
* @param key
* @param imageurl
* @param width
* @param height
* @算法:
* @日期:2015年1月28日
*/
public void putImage(String key, Image image, int width, int height)
throws IOException {
// Image->bufferreImage
BufferedImage bimg = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
// bufferImage->base64
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bimg, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
String base64Img = encoder.encode(outputStream.toByteArray());
this.content = this.content.replace("@{" + key + "}",
"<img src= \"data:image/png;base64," + base64Img
+ "\" width=\"" + width + "\" height=\"" + height
+ "\"/>");
}
本文欢迎转载,转载请注明:
http://itsshq.com/article-255.html
Image对象转Base64码(java)
最新推荐文章于 2024-07-12 13:57:41 发布