图片
Contents
[
Hide
]
本文演示了如何使用 Aspose.Slides for Android via Java 从内存图像插入和访问图片。下面的示例将在内存中创建图像,将其放置在幻灯片上,然后检索它。
添加图片
此代码生成一个小位图,将其转换为流,并将其作为图片框插入到第一张幻灯片上。
public static void addPicture() throws IOException {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
// 创建一个简单的内存图像。
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas graphics = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.valueOf(144, 238, 144).toArgb());
paint.setStyle(Paint.Style.FILL);
graphics.drawRect(0, 0, 100, 100, paint);
// 将位图转换为字节数组。
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG,100, bitmapStream);
byte[] pngBytes = bitmapStream.toByteArray();
// 将图像添加到演示文稿中。
IPPImage image = presentation.getImages().addImage(new ByteArrayInputStream(pngBytes));
// 在第一张幻灯片上插入显示该图像的图片框。
slide.getShapes().addPictureFrame(ShapeType.Rectangle, 50, 50, bitmap.getWidth(), bitmap.getHeight(), image);
presentation.save("picture.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
访问图片
此示例确保幻灯片包含图片框,然后访问找到的第一个图片框。
public static void accessPicture() throws IOException {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG,100, bitmapStream);
byte[] pngBytes = bitmapStream.toByteArray();
IPPImage image = presentation.getImages().addImage(new ByteArrayInputStream(pngBytes));
slide.getShapes().addPictureFrame(ShapeType.Rectangle, 0, 0, 40, 40, image);
IPictureFrame pictureFrame = null;
for (IShape shape : slide.getShapes()) {
if (shape instanceof IPictureFrame) {
pictureFrame = (IPictureFrame) shape;
break;
}
}
} finally {
presentation.dispose();
}
}