Skip to Content
0
Former Member
Nov 29, 2005 at 04:09 AM

How to convert java code to Portal

25 Views

Hello,

I have java code which resizes the image. I want to know whether the code would work fine in PDK.if not what is the chanes to be done to the attached code.

import com.sun.image.codec.jpeg.*;

import java.awt.*;

import java.awt.image.*;

import java.io.*;

public class Thumbnail1

{

public static void main(String[] args) throws Exception

{

if (args.length != 4)

{

System.err.println("Usage: java Thumbnail INFILE " + "OUTFILE WIDTH HEIGHT ");

System.exit(1);

}

// load image from INFILE

Image image = Toolkit.getDefaultToolkit().getImage(args[0]);

MediaTracker mediaTracker = new MediaTracker(new Container());

mediaTracker.addImage(image, 0);

mediaTracker.waitForID(0);

// determine thumbnail size from WIDTH and HEIGHT

int newImageWidth = Integer.parseInt(args[2]);

int newImageHeight = Integer.parseInt(args[3]);

double newImageRatio = (double)newImageWidth / (double)newImageHeight;

int oldImageWidth = image.getWidth(null);

int oldImageHeight = image.getHeight(null);

double oldImageRatio = (double)oldImageWidth / (double)oldImageHeight;

if (newImageRatio > oldImageRatio)

{

newImageWidth = (int)(newImageHeight * oldImageRatio);

}

else

{

newImageHeight = (int)(newImageWidth / oldImageRatio);

}

// draw original image to thumbnail image object and

// scale it to the new size on-the-fly

BufferedImage newThumbImage = new BufferedImage(newImageWidth, newImageHeight, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics2D =newThumbImage.createGraphics();

graphics2D.drawImage(image, 0, 0, newImageWidth, newImageHeight, null);

// save thumbnail image to OUTFILE

BufferedOutputStream outFile = new BufferedOutputStream(new FileOutputStream(args[1]));

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outFile);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(newThumbImage);

encoder.setJPEGEncodeParam(param);

encoder.encode(newThumbImage);

outFile.close();

System.out.println("Successfully Saved.");

System.exit(0);

}

}