You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Arun Chauhan <ar...@gmail.com> on 2012/12/19 13:43:42 UTC
Wicket create image from file system outside web application
directory
I have a repository storing many images somewhere on the server.
I want to be able to create a dynamic Image object with one of the images
stored in my repository.
I am using wicket 1.5.7. I saw this example somewhere
1) Created the FileResource class:
public class FileResource extends WebResource {
private static final long serialVersionUID = 1L;
private File file;
public FileResource(File file) {
this.file = file;
}
@Override
public IResourceStream getResourceStream() {
return new FileResourceStream(file);
}
}
2) In MyPage.java:
File imageFile = new File("local_path_to_image");
Image myImage = new Image("myImage", new FileResource(imageFile));
add(myImage);
3) In MyPage.html:
<i-m-g wicket:id="myImage" />
But this is not working in my case because WebResource is not available in
my wicket 1.5.
I have also studied this
[link](http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/) in
wicket action. But I am a wicket bignner i could not understand much.
I am making a project in which user when click on a product a modal window
open with the product name. I also want to include the product image on my
modal window inside a modal window. Images are stored on my server in a
directory.
Any help and advices appreciated! Thanks in advance.
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket create image from file system outside web application
directory
Posted by Arun Chauhan <ar...@gmail.com>.
hi,
it should return image bytes or only link will work
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932p4654973.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket create image from file system outside web application
directory
Posted by Arun Chauhan <ar...@gmail.com>.
Hi Martin,
I have cloned the code. I am getting link (F:\wicket
tomcat\apache-tomcat-7.0.16\bin\images\455919d7-3ff6-4cef-a749-c198a94f31c3.jpeg)
on modal window panel. Why image is not coming. And control is also not
coming to inner *ImageResource.class*.
*WicketApplication.java*
I have added this code
mountResource("/orderPage/{name}",new
ImageResourceReference());
orderPage is my page on which modal window is opening.
I think here also i am doing something wrong at this line.
*ItemOrderPanel.java*
final ResourceReference imageResourceReference = new
ImageResourceReference();
String imageName = itm.getProductImage();
final PageParameters parameters = new PageParameters();
parameters.set("name", imageName);
// generates nice looking url (the mounted one) to the current image
CharSequence urlForWordAsImage =
getRequestCycle().urlFor(imageResourceReference, parameters);
ExternalLink link = new ExternalLink("link",
urlForWordAsImage.toString());
link.setBody(Model.of(imageName));
add(link);
*ItemOrderPanel.html*
<div>
<li> </li>
</div>
*ImageResouceReference.java*
package com.zipgrocery.service;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.DynamicImageResource;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.string.StringValue;
public class ImageResourceReference extends ResourceReference{
public ImageResourceReference(){
super(ImageResourceReference.class,"imagesDemo");
System.out.println("*********************** 1 **********************");
}
@Override
public IResource getResource() {
System.out.println("********************** 2 ***********************");
return new ImageResource();
}
private static class ImageResource extends DynamicImageResource{
private static final long serialVersionUID = 1L;
@Override
protected byte[] getImageData(Attributes attributes) {
System.out.println("********************** 3 ***********************");
PageParameters parameters = attributes.getParameters();
StringValue name = parameters.get("name");
System.out.println("************************name************************"+
name);
byte[] imageBytes = null;
if(name.isEmpty() == false)
imageBytes = getImageAsBytes(name.toString());
return imageBytes;
}
private byte[] getImageAsBytes(String label){
System.out.println("********************** 4 ***********************");
BufferedImage image = new BufferedImage(800, 600,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.BLACK);
g.setBackground(Color.WHITE);
g.clearRect(0, 0, image.getWidth(), image.getHeight());
//g.setFont(new Font("SansSerif", Font.PLAIN, 48));
//g.drawString(label, 50, 50);
g.dispose();
Iterator<ImageWriter> writers =
ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = writers.next();
if (writer == null) {
throw new RuntimeException("JPG not supported?!");
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] imageBytes = null;
try {
ImageOutputStream imageOut =
ImageIO.createImageOutputStream(out);
writer.setOutput(imageOut);
writer.write(image);
imageOut.close();
imageBytes = out.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return imageBytes;
}
@Override
public boolean equals(Object that){
return that instanceof ImageResource;
}
}
}
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932p4654972.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket create image from file system outside web application directory
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
On Thu, Dec 20, 2012 at 12:20 PM, Arun Chauhan <ar...@gmail.com>wrote:
> add(new Image("img",urlForImage.toString()));
>
Instead of this, do:
WebMarkupContainer img = new WebMarkupContainer("img);
img.add(AttributeModifier.replace("src", urlForImage.toString()))
Clone the demo application and check how it works.
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>
Re: Wicket create image from file system outside web application
directory
Posted by Arun Chauhan <ar...@gmail.com>.
Helo Martin. Thanks for your help
I tried example given on that link
*ItemOrderPanel.html*
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<wicket:panel>
<div class="item-order-panel">
<div style="position:absolute;top:-13px;left:509px;">
<img src="images/zip_close.png"/> <#>
</div>
<strong wicket:id="itemName">Item Name</strong>
<hr/>
<div>
</div>
<div style="margin-left:200px;">
<form wicket:id="frmItemOrder">
<strong>Total: </strong>$9.99
<select wicket:id="quantity"><option>1</option></select>
Add <#>
</form>
</div>
</div>
</wicket:panel>
</body>
</html>
*ItemOrderPanel.java*
final ResourceReference imageResourceReference = new
ImageResourceReference();
String imageName = itm.getProductImage();
final PageParameters parameters = new PageParameters();
parameters.set("name", imageName);
CharSequence urlForImage =
getRequestCycle().urlFor(imageResourceReference,parameters);
System.out.println("***********************************url For Image
***********************"+ urlForImage);
//add(new Image("productImage",urlForImage.toString()));
//ExternalLink link = new ExternalLink("link",
urlForImage.toString());
add(new Image("img",urlForImage.toString()));
*ImageResourceReference.java*
package com.zipgrocery.service;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.DynamicImageResource;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.string.StringValue;
public class ImageResourceReference extends ResourceReference{
public ImageResourceReference(){
super(ImageResourceReference.class,"imagesDemo");
System.out.println("*********************** 1 **********************");
}
@Override
public IResource getResource() {
System.out.println("********************** 2 ***********************");
return new ImageResource();
}
private static class ImageResource extends DynamicImageResource{
private static final long serialVersionUID = 1L;
@Override
protected byte[] getImageData(Attributes attributes) {
System.out.println("********************** 3 ***********************");
PageParameters parameters = attributes.getParameters();
StringValue name = parameters.get("name");
System.out.println("************************name************************"+
name);
byte[] imageBytes = null;
if(name.isEmpty() == false)
imageBytes = getImageAsBytes(name.toString());
return imageBytes;
}
private byte[] getImageAsBytes(String label){
System.out.println("********************** 4 ***********************");
BufferedImage image = new BufferedImage(800, 600,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.BLACK);
g.setBackground(Color.WHITE);
g.clearRect(0, 0, image.getWidth(), image.getHeight());
//g.setFont(new Font("SansSerif", Font.PLAIN, 48));
//g.drawString(label, 50, 50);
g.dispose();
Iterator<ImageWriter> writers =
ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = writers.next();
if (writer == null) {
throw new RuntimeException("JPG not supported?!");
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] imageBytes = null;
try {
ImageOutputStream imageOut =
ImageIO.createImageOutputStream(out);
writer.setOutput(imageOut);
writer.write(image);
imageOut.close();
imageBytes = out.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return imageBytes;
}
@Override
public boolean equals(Object that){
return that instanceof ImageResource;
}
}
}
But image is not rendering and control is also not coming to inner
/ImageResource.java/.
*At server console *
WARN- ResourceReferenceRegistry - Asked to autocreate a ResourceReference
but resourceReferenceRegistry.createDefaultResourceReference() return null
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932p4654970.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket create image from file system outside web application
directory
Posted by Arun Chauhan <ar...@gmail.com>.
hi Martin,
finally i settled on this code and I am getting perfect image. Thanks for
help
String imageName = itm.getProductImage();
final PageParameters parameters = new PageParameters();
parameters.set("name", imageName);
add(new NonCachingImage("imgPlc", new
AbstractReadOnlyModel<DynamicImageResource>(){
@Override public DynamicImageResource getObject() {
DynamicImageResource dir = new DynamicImageResource() {
@Override protected byte[] getImageData(Attributes
attributes) {
StringValue name = parameters.get("name");
byte[] imageBytes = null;
if(name.isEmpty() == false)
imageBytes = getImageAsBytes(name.toString());
return imageBytes;
}
};
dir.setFormat("image/png");
return dir;
}
}));
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932p4654997.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket create image from file system outside web application directory
Posted by Martin Grigorov <mg...@apache.org>.
Why do you use ExternalLink link = new ExternalLink("link",
urlForWordAsImage.toString());
?
You need to show an image.
I guess if you click on this link it will hit the resource reference.
On Thu, Dec 20, 2012 at 1:24 PM, Arun Chauhan <ar...@gmail.com> wrote:
> hi,
>
> I tried to debug the code and I found out that control is not coming to
> internal /ImageResource/ class which is returning bytes.
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932p4654974.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>
Re: Wicket create image from file system outside web application
directory
Posted by Arun Chauhan <ar...@gmail.com>.
hi,
I tried to debug the code and I found out that control is not coming to
internal /ImageResource/ class which is returning bytes.
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932p4654974.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket create image from file system outside web application directory
Posted by Martin Grigorov <mg...@apache.org>.
Hi
http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/ is exactly
what you need.
At the bottom of the article there is a link to a demo application. Check
its sources
On Wed, Dec 19, 2012 at 2:43 PM, Arun Chauhan <ar...@gmail.com> wrote:
> I have a repository storing many images somewhere on the server.
> I want to be able to create a dynamic Image object with one of the images
> stored in my repository.
>
> I am using wicket 1.5.7. I saw this example somewhere
>
> 1) Created the FileResource class:
>
> public class FileResource extends WebResource {
> private static final long serialVersionUID = 1L;
>
> private File file;
>
> public FileResource(File file) {
> this.file = file;
> }
>
> @Override
> public IResourceStream getResourceStream() {
> return new FileResourceStream(file);
> }
> }
>
>
> 2) In MyPage.java:
>
> File imageFile = new File("local_path_to_image");
> Image myImage = new Image("myImage", new FileResource(imageFile));
> add(myImage);
>
> 3) In MyPage.html:
>
> <i-m-g wicket:id="myImage" />
>
> But this is not working in my case because WebResource is not available in
> my wicket 1.5.
>
> I have also studied this
> [link](http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/)
> in
> wicket action. But I am a wicket bignner i could not understand much.
>
> I am making a project in which user when click on a product a modal window
> open with the product name. I also want to include the product image on my
> modal window inside a modal window. Images are stored on my server in a
> directory.
>
> Any help and advices appreciated! Thanks in advance.
>
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Wicket-create-image-from-file-system-outside-web-application-directory-tp4654932.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>