Avoid an exception when getting the default bitmap image size on some JVMs

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@474253 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2006-11-13 11:13:55 +00:00
parent 59ced4d73e
commit 23303f691f
2 changed files with 17 additions and 5 deletions

View File

@ -122,12 +122,19 @@ public class Picture extends SimpleShape {
public void setDefaultSize(){ public void setDefaultSize(){
PictureData pict = getPictureData(); PictureData pict = getPictureData();
if (pict instanceof Bitmap){ if (pict instanceof Bitmap){
BufferedImage img = null;
try { try {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData())); img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
if(img != null) setAnchor(new java.awt.Rectangle(0, 0, img.getWidth(), img.getHeight())); }
else setAnchor(new java.awt.Rectangle(0, 0, 200, 200)); catch (IOException e){}
} catch (IOException e){ catch (NegativeArraySizeException ne) {}
;
if(img != null) {
// Valid image, set anchor from it
setAnchor(new java.awt.Rectangle(0, 0, img.getWidth(), img.getHeight()));
} else {
// Invalid image, go with the default metafile size
setAnchor(new java.awt.Rectangle(0, 0, 200, 200));
} }
} else { } else {
//default size of a metafile picture is 200x200 //default size of a metafile picture is 200x200

View File

@ -257,6 +257,11 @@ public class TestPictures extends TestCase{
Slide slide = ppt.createSlide(); Slide slide = ppt.createSlide();
File img = new File(cwd, "sci_cec.dib"); File img = new File(cwd, "sci_cec.dib");
// Check we can read the test DIB image
assertTrue(img.exists());
// Add the image
int idx = ppt.addPicture(img, Picture.DIB); int idx = ppt.addPicture(img, Picture.DIB);
Picture pict = new Picture(idx); Picture pict = new Picture(idx);
assertEquals(idx, pict.getPictureIndex()); assertEquals(idx, pict.getPictureIndex());