You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Jim Pinkham <pi...@gmail.com> on 2011/07/02 14:19:02 UTC

Re: ResourceReference.getResource() returns NULL ?

Search for "Dynamically loading image" for good examples of how to do this.

Basically, you do not put the image data directly into HTML.  Instead, you
make your page.html  with some table perhaps, and a series of links to your
images.  So you would need one method to list the ids of your images to put
as a parameters into your links.   Then, you will 'mount' the resource in
your application class at app init time, which will allow your app to serve
up the image data for each <img src="mountpoint?id=123"> tag.

Good luck
Jim

On Tue, Feb 22, 2011 at 8:00 AM, smallufo <sm...@gmail.com> wrote:

> I want to build a DynamicImageResource which can scale internal (packaged)
> images (with name : {index}.gif ) .
> In getImageData() , I try to load a truly existing image , but cannot
> getResource() , it returns null !
>
> Here is my code :
>
> public class ScaledImageResource extends DynamicImageResource
> {
>  private ThreadLocal<Integer> index = new ThreadLocal<Integer>();
>  private ThreadLocal<Integer> width = new ThreadLocal<Integer>();
>  private ThreadLocal<Integer> height = new ThreadLocal<Integer>();
>
>  @Override
>  public IResourceStream getResourceStream()
>  {
>    ValueMap map = getParameters();
>    index .set(map.getAsInteger("index" ,  1));
>    width .set(map.getAsInteger("width" , 50));
>    height.set(map.getAsInteger("height", 50));
>    return super.getResourceStream();
>  }
>
>  @Override
>  protected byte[] getImageData()
>  {
>    ResourceReference imageResource = new ResourceReference(MyPage.class ,
> "icons/byIndex/"+index.get()+".gif");
>    http://foobar.com/app/resources/foo.bar.MyPage/icons/byIndex/1.gif does
> exist !
>
>    try
>    {
>      System.out.println("imageResource.getResource() = " +
> imageResource.getResource()); // returns NULL
>      InputStream is =
> imageResource.getResource().getResourceStream().getInputStream();
>      BufferedImage bufferedImage = ImageIO.read(is);
>      BufferedImage scaledImage = BufferedImageTools.getImage(bufferedImage,
> width.get() , height.get());
>
>      byte[] bytes = null;
>      //scale image , build bytes  , skipped here.
>      return bytes;
>    }
>    catch (Exception e)
>    {
>    }
>    return null;
>  }
> }
>
> And in Application's init():
> getSharedResources().add(IMG_KEY, new ScaledImageResource());
> mountSharedResource("/scaledImage", new
> ResourceReference(IMG_KEY).getSharedResourceKey());
>
> Note , the image file does exist there ,
> and http://foobar.com/app/resources/foo.bar.MyPage/icons/byIndex/1.gifdoes
> browsable !
>
> But why cannot I getResource() of the imageResource ?
>