cancel
Showing results for 
Search instead for 
Did you mean: 

Edit and insert a image

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

Answers (1)

Answers (1)

Former Member
0 Kudos

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!