9 Replies Latest reply on Jun 24, 2006 12:09 PM by Trensic

    Java Image as Byte Array

    Trensic
      I have a tiff image being coverted to a bmp and sent in a byte array via java. In coldfusion I can make the call and get the array but cannot figure out how to display the image. If I write the output to a file using cffile it creates the bmp from the array. So how do I use the array data to display the image?

      Oh, I would prefer not to write the image then call it from the web server and I am using coldfusion mx61 on top of JRUN update 6, Java 1.5+

      Thanks in advance


        • 1. Re: Java Image as Byte Array
          Trensic Level 1
          If anyone can help please let me know but this works for now. It still writes the file out then has to read it back in... Is there anyway to output the byte array as an image or output it to flash and build the image there?


          <cfobject action="create" type="java" class="net.revity.imaging.producer.TiffBMPConverter" name="obj">

          <cfset obj.setImageSource("\\yogi\revitydm$\cortex\Accounting1\1056.tif")>
          <cfset PageCount = obj.getNumPages()>
          <cfset ImageBytes = obj.getByteArray(0)>


          <cffile action="write" addnewline="no" file="\\yogi\revitydm$\cortex\Accounting1\test.bmp" output="#ImageBytes#">

          <cfcontent file="\\yogi\revitydm$\cortex\Accounting1\test.bmp" type="image/gif">
          • 2. Re: Java Image as Byte Array
            healey_mark
            Try using the "variable" property of cfcontent:

            <cfcontent type="image/gif" variable="imageBytes">

            I didn't try it, but according to the documentation it should work:
            http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/js/html/wwhelp.htm?hre f=part_cfm.htm
            • 3. Re: Java Image as Byte Array
              Trensic Level 1
              Thanks the response

              The problem I have is that I am using CFMX6.1 Enterprise so the variable property is not available. If there is another method please let me know. I could write a JSP much like we did for Flash 5 and Generator to convert Tiff images (with a socket server and custom Generator object) but I figured CFMX6.1 would have a way. I would love to figure out a way to get the byte array into flash and render that as an image if possible. I can get the array there with java or coldfusion but can not get an image to output.
              • 4. Re: Java Image as Byte Array
                healey_mark Level 1
                Hi,

                I think you can also use the <cfcontent> tag without a file property,
                then loop through your array and output it one byte at a time.

                <cfcontent type="image/gif">
                ...loop through array and output bytes...
                </cfcontent>

                You might want a cfheader tag at the very top of the file:
                <cfheader name="Content-Disposition" value="inline; filename=thefile.gif">
                I would try it both with and without this since it may not be necessary.

                I notice that you are mixing GIF and BMP in the above example. Be careful of that, make sure you send the content-disposition and the "type" property of the cfcontent tag to accurately reflect the type of information you are sending to the browser. Otherwise it will display garbage.

                You might need image/bmp (or whatever the mime type is for bitmaps) instead of image/gif if it is truly bitmap data. Try image/bmp or image/x-win-bmp I am not sure what is correct.

                • 5. Re: Java Image as Byte Array
                  BKBK Adobe Community Professional & MVP
                  <cfheader name="Content-Disposition" value="inline; filename=test.bmp">
                  <cfcontent file="#fullPathToBMPfile#" type="image/bmp">

                  • 6. Re: Java Image as Byte Array
                    Trensic Level 1
                    Thanks for the suggestion and I did try the loop...The problem is the image(s) is(are) big enough that the server takes its time returning any data assuming it does not time out at all.

                    I did do some looking and found this site:
                    http://weblogs.macromedia.com/cantrell/archives/2003/06/using_coldfusio.cfm

                    So I incorporated the script part into my logic and the image returned without an issue.

                    <cfobject action="create" type="java" class="net.revity.imaging.producer.TiffBMPConverter" name="obj">

                    <cfset obj.setImageSource("\\yogi\revitydm$\cortex\Accounting1\1056.tif")>
                    <cfset PageCount = obj.getNumPages()>

                    <cfset ImageBytes = obj.getByteArray(0)>

                    <cfscript>
                    context = getPageContext();
                    context.setFlushOutput(false);
                    response = context.getResponse().getResponse();
                    out = response.getOutputStream();
                    response.setContentType("image/bmp");
                    response.setContentLength(arrayLen(ImageBytes));
                    out.write(ImageBytes);
                    out.flush();
                    out.close();
                    </cfscript>

                    Thank You for responding and for the suggestions. Assuming I move to CF7 anytime soon it looks like I would have a easier time at this.

                    • 7. Re: Java Image as Byte Array
                      BKBK Adobe Community Professional & MVP
                      I wonder whether the following would also work. If so, it would give you some control.

                      • 8. Java Image as Byte Array
                        healey_mark Level 1
                        Trensic,

                        Good solution, though I am surprised (or would not be surprised) if there is some way to get ColdFusion to output a byte array efficiently. But whatever, you got it working. (update--I noticed that is CFScript you are using there--for some reason I thought it was java. So, perfect!)

                        I noticed this message at the bottom of the forum you got the solution from, you might want to reset the response() to have things absolutely correct:

                        Posted by: Christopher Rentz at June 7, 2005 12:13 PM

                        I know this is a really old post, but the following may be useful to others (especially on CFMX 7):

                        I've found that you need to reset the response before closing the OutputStream otherwise you leave the thread in an unusable state for future requests (which leads to the "The server encountered an internal error and was unable to complete your request." error from CF and, more specifically, a "Response has already been committed" error from JRun).

                        With that, this code cleans up the response correctly:

                        out.write(byteArray);
                        out.flush();
                        response.reset();
                        out.close();
                        • 9. Re: Java Image as Byte Array
                          Trensic Level 1
                          I did fix the code and it does work a bit better. The tiff image is now being converted to a png to load into flash and it takes all but a second to parse each tiff image page or a multi page tiff with the java cleaned up a bit as well.

                          So with this code I can go to the browser or the flash movie so very cool.

                          healey_mark, many thank for catching that last part. I had issues with that a while back while parsing txt files so that could have been a pain.

                          BKBK, thanks for your help as well. That will work and when I need to output to the web root which I need for another part of this...then this will help a bunch.

                          And for my next trick I will be attempting to cache the images on the users machine since this will be in a projector/zinc exe to cut down on the server time if the user loads the image again.


                          Thanks again to both of you