Downscaling images by file size

The following code example shows how to downscale images by file size.

package com.nokia.evo.java.test.imagescale;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.nokia.mid.imagescale.ImageScaler;
import com.nokia.mid.imagescale.ImageScalerException;
import com.nokia.mid.imagescale.ImageScalerListener;

public class ImageScalingFileSizeExample extends MIDlet implements ImageScalerListener{

	private String srcFile = "file:///MemoryCard/big.jpg";
	private String destFile = "file:///MemoryCard/small.jpg";
	private int jpegQuality = 0;
	Form form = new Form("ImageScaling Example");
	private int maxFileSize = 100;   //Unit: K
	
	public ImageScalingFileSizeExample() {
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		Display.getDisplay(this).setCurrent(form);
		ImageScaler is = new ImageScaler(srcFile, destFile);
		is.addListener(this);
		if (jpegQuality != 0)
		{
			is.setJpegQuality(jpegQuality);
		}				
		int result = -1;
		try {
			result = is.scaleImage(maxFileSize);
		} catch (ImageScalerException e) {
		}
		if (result <= 0){
			Ticker ticker = new Ticker("Scale image " + srcFile + " fail!");
			form.setTicker(ticker);
		}
		

	}
	
	public void scaleFinished(int requestId, int result){
		if (result == 0){
			Ticker ticker = new Ticker("[" + requestId + "]: Scale OK!");
			form.setTicker(ticker);
			
		}else{
			Ticker ticker = new Ticker("[" + requestId + "]: Scale Fail! result=[" + result + "]");
			form.setTicker(ticker);
		}
			
	}

}