on 09-01-2008 10:10 AM
Hi,
i want to load and edit a image with the following code:
(I want to insert some text in the image)
public static BufferedImage getImage(String Text)
{
File fileImage = new File("E:/TestHeader1200x54.bmp");
BufferedImage image = null;
try
{
image = ImageIO.read(fileImage);
}
catch(IOException e)
{
e.printStackTrace();
}
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.black);
g2.drawString(Text, 100,30);
return image;
}
The problem is, that image is null. So the "Graphics2D g2 = image.createGraphics();" line throws a null pointer exception.
The line "image = ImageIO.read(fileImage);" doesnt throw a exception!
But i also got a second problem, if the method would work, how can i insert a BufferedImage into a view?
I already tryed to cast it to an IWDImage, but that doesnt work.
EDIT: I can already insert a picture with a picture element in the view node and using Calculated attribute getter.
Is It possible to edit that picture like in the upper code?
Regards,
Hubert
Edited by: Hubert Strauß on Sep 1, 2008 11:28 AM
Hi,
May be of use for you
File fileImage = new File("D:/globe.jpg");
ByteArrayOutputStream arrayOutputStream= null;
JPEGImageEncoder encoder = null;
BufferedImage image = null;
try {
image = ImageIO.read(fileImage);
arrayOutputStream=new ByteArrayOutputStream();
encoder = JPEGCodec.createJPEGEncoder(arrayOutputStream);
encoder.encode(image);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.black);
g2.drawString("Text", 100,30);
byte []b= arrayOutputStream.toByteArray();
IWDResource res=WDResourceFactory.createCachedResource(b,"MyImage",WDWebResourceType.JPG_IMAGE);
Regards
Ayyapparaj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for the code Ayyapparaj!
I think thats what i searched for, but i still got the problem with the nullpointer exception.
java.lang.NullPointerException
at com.siemens.hft.BrandLabel.getImage(BrandLabel.java:74)
Line 74: "Graphics2D g2 = image.createGraphics();"
But this time i get a Exception when he trys to read the image "javax.imageio.IIOException: Can't read input file!".
I am sure that the picture exists, is it possible that the application doesnt have the rights to access the picture on drive E ?
EDIT: It works now, i copyed the picture to the directory of the application and changed the type to .jpg.
Now i got another problem, how can i insert the IWDResource object in the view?
Tryed it this way:
IWDTransparentContainer con =
(IWDTransparentContainer) view.getElement("RootUIElementContainer");
con.addChild((IWDImage)BrandLabel.getImage("test"));
but it doesnt work. (the .getImage method returns the IWDResource)
EDIT: I solved the problem, inserting it this way:
IWDImage iwdimg = (IWDImage)view.createElement(IWDImage.class,null);
iwdimg.setSource(BrandLabel.getImage("test").getUrl(1));
con.addChild(iwdimg);
If someone got the same problem as me, here is another change:
IWDResource res=null;
File fileImage = new File("E:/......../File.jpg");
ByteArrayOutputStream arrayOutputStream= null;
JPEGImageEncoder encoder = null;
BufferedImage image = null;
try {
image = ImageIO.read(fileImage);
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.black);
g2.drawString("Textzjuzud", 100,30);
arrayOutputStream=new ByteArrayOutputStream();
encoder = JPEGCodec.createJPEGEncoder(arrayOutputStream);
encoder.encode(image);
} catch (IOException e) {
e.printStackTrace();
}
byte []b= arrayOutputStream.toByteArray();
res=WDResourceFactory.createCachedResource(b,"MyImage",WDWebResourceType.JPG_IMAGE);
The drawing of the color must be inside the try!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.