2 Replies Latest reply on May 11, 2006 12:15 PM by Tomdogggg

    Java Image Processing Troubles

      I'm a beginner at integrating java into coldfusion. I have a component which will scale and crop an image, but I'd also like to run it through a color-filter to turn it grayscale. I've found methods but can't seem to get them to work, any tips?

      P.S.: I'm using the 'java.awt.image' classes
        • 1. Re: Java Image Processing Troubles
          BKBK Adobe Community Professional & MVP
          Could we see your methods in action?

          • 2. Re: Java Image Processing Troubles
            Tomdogggg Level 1
            <cffunction name="getFileObject" access="private" hint="Loads a file into memory so it's ready for manipulation">
            <cfargument name="filePath" type="string" required="yes" hint="Absolute path the the file you'd like to load.">

            <!--- Loading the file into memory--->
            <cfset var result=CreateObject("java","java.io.File")>
            <cfset result.init(filePath)>

            <!---Return the file object to the caller function--->
            <cfreturn result>

            <cffunction name="memberPicture" output="true" hint="Turns a file into the correct size, color, and format for the site.">
            <cfargument name="sourcePath" type="string" required="yes" hint="Absolute path to the source image">
            <cfargument name="destPath" type="string" required="yes" hint="Absolute path to the place you want to put the processed image.">
            <cfargument name="new_width" type="numeric" required="no" default="220">
            <!---Load class to hold the input image in--->
            <cfset var ImageIO=CreateObject("java","javax.imageio.ImageIO")>
            <!---Load class to hold the output image--->
            <cfset var jbiDest=CreateObject("java","java.awt.image.BufferedImage")>
            <!---Load class that will be transforming the image--->
            <cfset var jatTransformer=CreateObject("java","java.awt.geom.AffineTransform")>

            <!---Call our local function to create the input and destination file streams--->
            <cfset var jFileIn=getFileObject(sourcePath)>
            <cfset var jFileOut=getFileObject(destPath)>

            <!---Load the input file into the imageIO object--->
            <cfset var jbiSource=ImageIO.read(jFileIn)>

            <!---See how big the input file is--->
            <cfset var jFileIn_height=jbiSource.getHeight()>
            <cfset var jFileIn_width=jbiSource.getWidth()>

            <!---Figure out the scales for our output file--->
            <cfset var scale=ARGUMENTS.new_width/jFileIn_width>
            <cfset var jFileOut_height=jFileIn_height * scale>
            <cfset var jFileOut_width=ARGUMENTS.new_width>

            <!---Create a place to put the destination image object--->
            <cfset jbiDest.init(JavaCast("int",jFileOut_width),JavaCast("int",jFileOut_height),jbiSource.get Type())>

            <!---Tell the transformer object how we want to transform the image--->
            <cfset jatTransformer.setToScale(scale,scale)>

            <!---User the transformer to transform the input file so that it's ready to be outputted--->
            <cfset jbiDest.getGraphics().drawRenderedImage(jbiSource,jatTransformer)>

            <!---Convert the image into a 'jpeg' format and save it to the desired destination--->
            <cfset ImageIO.write(jbiDest, "jpeg", jFileOut)>