我们将图像作为字节数组获取,并将其编码为base64字符串,然后将此字符串写入word doc,但无法更改其大小。
PFB代码:
worddocfile //字符串生成器
String encodedImage = new String(Base64.encodeBase64(fileType.getFileContent()), "UTF-8");
encodedImage = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
worddocfile.append("<img style='display:block; width:600px;height:600px;' id='base64image'")
.append(" src='data:image/jpeg;base64,")
.append(encodedImage)
.append("' /> ");
任何人都可以提出解决方案,因为它可以在html页面上工作,但不能在word文档上工作。
我们已经使用了背景图片标签,但无法正常工作。
<p style="background-image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='); border: 1px solid black;background-repeat: no-repeat;background-size: contain;width:600px;height:597px;\>
答案 0 :(得分:0)
PFB代码:
ByteArrayInputStream bis1 = new ByteArrayInputStream(fileType.getFileContent() );
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
//ImageIO is a class containing static methods for locating ImageReaders
//and ImageWriters, and performing simple encoding and decoding.
ImageReader reader = (ImageReader) readers.next();
Object source1 = bis1;
ImageInputStream iis = ImageIO.createImageInputStream(source1);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
try{
Image image1 = reader.read(0, param);
//got an image file image1 -- image1.getWidth(null)
BufferedImage bufferedImage = new BufferedImage(650,950, BufferedImage.TYPE_INT_RGB);
//bufferedImage is the RenderedImage to be written
Graphics2D g2 = bufferedImage.createGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, 650,950);
g2.drawImage(image1, 0,0,650,950, null);//
g2.dispose();
//mgpath can be WEB-INF folder
File imageFile = new File(imgpath+"UpdatedImg.jpg");
ImageIO.write(bufferedImage, "jpg", imageFile);
FileInputStream fisn = new FileInputStream(imageFile);
//create FileInputStream which obtains input bytes from a file in a file system
//FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
ByteArrayOutputStream bosn = new ByteArrayOutputStream();
byte[] bufn = new byte[1024];
try {
for (int readNum1; (readNum1 = fisn.read(bufn)) != -1;) {
//Writes to this byte array output stream
bosn.write(bufn, 0, readNum1);
System.out.println("read " + readNum1 + " bytes,");
}
} catch (IOException ex) {
}
byte[] outArray1 = bosn.toByteArray();
String encodedImage = new String(Base64.encodeBase64(outArray1), "UTF-8");
logger.debug("encodedImage" + encodedImage);
/// workbookDetails.append("<img src='data:image/png;base64," + encodedImage + "'/>");
workbookDetails.append("<img style='display:block; width:595px;height:609px;' id='base64image'")
.append(" src='data:image/jpeg;base64,")
.append(encodedImage)
.append("' /> ")
.append(" </p>")
.append(" </td>")
.append(" </tr>")
.append(" </tbody>")
.append("</table>");
.append("</table>");