You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by gonzalomp87 <go...@gmail.com> on 2012/07/15 06:00:13 UTC

BufferedImage in Tapestry

First of all sorry for the translation. My level of English is very bad.

I'm trying to display an image on tapestry. In the .java  I have a variable
of type BufferedImage. I can show directly or I need to store it in a
physical file and then display it.

In the first way I would be much more comfortable but does not work.

I do not know to put in the code.   not work

Thank you very much!

--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by Michael Prescott <mi...@gmail.com>.
I wish I had more time to explain; I don't right now.  Here's a chunk of
code from our project that might help you along.

It's a tapestry page that loads a document (a "background material") from a
DAO and then displays it to the user.

public class Background {

@Inject
private BackgroundMaterialDao dao;

@Inject
private RequestGlobals requestGlobals;

 private String backgroundId;

public void beginRender(MarkupWriter writer) {
 requestGlobals.getHTTPServletResponse().setStatus(
HttpServletResponse.SC_NOT_FOUND);
 writer.element("p");
writer.write("No pre-read material found with id " + backgroundId);
 writer.end();
}

public Object onActivate(String backgroundId) throws SQLException,
MalformedURLException {
 BackgroundMaterial background = dao.get(backgroundId);
if (background != null) {
 if (background instanceof AssetBackgroundMaterial) {
return streamAsset((AssetBackgroundMaterial) background);
 } else if (background instanceof UrlBackgroundMaterial) {
return redirectTo((UrlBackgroundMaterial) background);
 }
}
return notFound(backgroundId);
 }

private URL redirectTo(UrlBackgroundMaterial background) throws
MalformedURLException {
 return new URL(background.getUrl());
}

private StreamResponse streamAsset(AssetBackgroundMaterial background)
throws SQLException {
 return new SimpleStreamResponse(background.getContentType(), background
.getMaterial().getBinaryStream());
 }

private Object notFound(String backgroundId) {
this.backgroundId = backgroundId;
 return null;
}
}

On 17 July 2012 16:33, gonzalomp87 <go...@gmail.com> wrote:

> Sorry, I dont understand.
>
> I have a BufferedImage type variable in my .tml can not show, but I can
> convert into a variable of type InputStream.
> Suppose I call that variable myImage
>
> I was looking at that page but do not quite understand. I'm supposed to use
> the last paragraph "Creating a page for streaming files" but I have some
> questions.
>
> 1. function getuploadfile should  return  Link instead of a string?
> 2. I do not understand that I would have to put in the variable "final
> String uuid" or in this call ".... getUploadedFile (" SOMEUUID ")"
> 3. At these points would have to enter some code? return new StreamResponse
> () {... }
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5714522.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: BufferedImage in Tapestry

Posted by gonzalomp87 <go...@gmail.com>.
Solved!!!!! 

I solved it adding a few lines to the server.xml file in tomcat / conf
Add appContextProtection="false" inside the tag
 <Listener
className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>



<Listener
className="org.apache.catalina.core.JreMemoryLeakPreventionListener" 
		appContextProtection="false"/>





--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5716734.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by gonzalomp87 <go...@gmail.com>.
I look the headers that tomcat return. 

[18:05:12.747] GET
http://localhost:8080/dicom-1.0.0/mostrarimagenpage/1.3.6.1.4.1.9328.50.3.10887
[HTTP/1.1 500 Error Interno del Servidor 424ms]

and inside show 2 fields where I think is the problem

x-Tapestry-ErrorMessage:
org.apache.tapestry5.ioc.internal.OperationException
Content-Type: text/html;charset=UTF-8

In my code I tried change the content-type with this lines:

@Meta("tapestry.response-content-type=image/JPEG")
@AuthenticationPolicy(AuthenticationPolicyType.AUTHENTICATED_USERS)
public class MostrarImagenPage 



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5716694.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by Lance Java <la...@googlemail.com>.
As I said before, use firebug or a similar tool to inspect the HTTP response
for the image. Are there any differences between the Jetty response and the
Tomcat response? Take note of the response headers.

Also, have you tried setting the content length header?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5716536.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by gonzalomp87 <go...@gmail.com>.
I post the problem on another forum and an user told me that I need put the
contentType for the browser know that is an image.
 

I add this line but doesn`t work yet

public void prepareResponse(Response response) {

response.setHeader("Content-type","image/jpeg");
response.setHeader("Content-Disposition", "inline;filename=" + filename+
((extension == null) ? "" : ("." +extension)));
}



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5716533.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by Lance Java <la...@googlemail.com>.
Some disadvantages worth considering when using data URLs for images:
http://en.wikipedia.org/wiki/Data_URI_scheme#Disadvantages



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5716529.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by Giulio Micali <gi...@gmail.com>.
for such kind of jobs, i use the following method:

<img src="data:image/gif;base64,<here the base64 encoded stream>"
width="80" height="15" />


Returning the entire tag, so I can include it even in ajax responses.

I hope it can be helpful,
Giulio

Re: BufferedImage in Tapestry

Posted by Lance Java <la...@googlemail.com>.
Using firebug or similar, can you inspect the response from the image
request? What headers are included?

Perhaps setting the "Content-Length" header on the image response will help.
This should be the byte count (inputStream.available() or file.length()
etc).



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5716515.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by gonzalomp87 <go...@gmail.com>.
Hi! Up the thread because I have a problem.

I was working with Jetty and the above code worked perfectly but  in tomcat
not showing the image and nor give any error .Anyone know what could be the
cause?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5716512.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by gonzalomp87 <go...@gmail.com>.
First of all thanks for the replies. I copy my code if anyone is useful

ImagenDetails.tml

   ${uploadedImage} 

ImagenDetails.java

 @InjectPage
     private ShowImagePage showImagePage;
@Property
	private Imagen imagen;

 public Link getUploadedImage(){
		    return showImagePage.getUploadedFile(imagen.getImagenId());
		} 

//Imagen is a entity with a property call ImagenId.


 showImagePage.java 
public class ShowImagePage {

    @Inject
    private LinkSource linkSource;
    
    @Inject
   private EstudioService estudioService;
   
   private String contentType = "image/jpeg";// this is the default

   private String extension = "jpg";

    private  String filename = "default";
    
    public Link getUploadedFile(String imageId) {
            return
linkSource.createPageRenderLink(ShowImagePage.class.getSimpleName(),false,
new Object[]{imageId});
    }

    public StreamResponse onActivate(final String imageId) {
        	this.filename = imageId;
        	return new StreamResponse() {

	            public String getContentType() {
	                return contentType;
	            }

	            public InputStream getStream() throws IOException {
	            	Imagen imagen=null;
	            	try {
						imagen = estudioService.findImagen(imageId);
						
					} catch (InstanceNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	                return estudioService.dicom2jpg(imagen.getNombreFich());
                  //dicom2jpg is a function that return a InputStream from a
.dcm file. 
	            }

	            public void prepareResponse(Response response) {
	                response.setHeader("Content-Disposition",
"inline;filename=" + filename+ ((extension == null) ? "" : ("."
+extension)));
	            }
			
        };
    }
    
 } 



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5714662.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by Howard Lewis Ship <hl...@gmail.com>.
You need to connect the dots between what you have and what the browser needs.

What you have is BufferedImage object.

What the browser needs is a URL.  When it uses HTTP GET on that URL,
it needs a stream of bytes.

You can't just return the BufferedImage object; there's no realistic
way that Tapestry could magically know how to create a URL directly
from that, and serve up the correct bytestream.

What you can do is create a URL that calls back into your page.  You
then can use the BufferedImage to generate that stream of bytes.

Tapestry's ComponentResources.createEventLink() method allows you to
create a callback URL.

Tapestry will invoke a method when the browser GETs that URL; the
method can return Tapestry's StreamResponse type, which is what will
provide the stream of bytes to go to the browser.

It is an explicit aspect of developing web applications with Tapestry
that you must have at least a cursory knowledge of how HTTP and
servlets and the like work;  Tapestry handles virtually all the busy
work for you, and hides lots and lots of details and edge cases you'd
rather not know about ... but it is explicitly a layer on top of HTTP
and Java servlets.

On Tue, Jul 17, 2012 at 1:33 PM, gonzalomp87 <go...@gmail.com> wrote:
> Sorry, I dont understand.
>
> I have a BufferedImage type variable in my .tml can not show, but I can
> convert into a variable of type InputStream.
> Suppose I call that variable myImage
>
> I was looking at that page but do not quite understand. I'm supposed to use
> the last paragraph "Creating a page for streaming files" but I have some
> questions.
>
> 1. function getuploadfile should  return  Link instead of a string?
> 2. I do not understand that I would have to put in the variable "final
> String uuid" or in this call ".... getUploadedFile (" SOMEUUID ")"
> 3. At these points would have to enter some code? return new StreamResponse
> () {... }
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5714522.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by gonzalomp87 <go...@gmail.com>.
Sorry, I dont understand.

I have a BufferedImage type variable in my .tml can not show, but I can
convert into a variable of type InputStream. 
Suppose I call that variable myImage

I was looking at that page but do not quite understand. I'm supposed to use
the last paragraph "Creating a page for streaming files" but I have some
questions.

1. function getuploadfile should  return  Link instead of a string?
2. I do not understand that I would have to put in the variable "final
String uuid" or in this call ".... getUploadedFile (" SOMEUUID ")"
3. At these points would have to enter some code? return new StreamResponse
() {... }

--
View this message in context: http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465p5714522.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BufferedImage in Tapestry

Posted by Michael Prescott <mi...@gmail.com>.
You cannot return the BufferedImage, but you can stream the data from the
image. You do not need to store it in a physical file.

Have a look at this wiki page:

http://wiki.apache.org/tapestry/Tapestry5HowToStreamAnExistingBinaryFile

On 15 July 2012 00:00, gonzalomp87 <go...@gmail.com> wrote:

> First of all sorry for the translation. My level of English is very bad.
>
> I'm trying to display an image on tapestry. In the .java  I have a variable
> of type BufferedImage. I can show directly or I need to store it in a
> physical file and then display it.
>
> In the first way I would be much more comfortable but does not work.
>
> I do not know to put in the code.   not work
>
> Thank you very much!
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/BufferedImage-in-Tapestry-tp5714465.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>