Resize the image in J2ME

It works with RGBArrays. First, we need a function to resize Arrays:


private int[] reescalaArray(int[] ini, int x, int y, int x2, int y2) {
int out[] = new int[x2*y2];
for (int yy = 0; yy < y2; yy++) {
int dy = yy * y / y2;
for (int xx = 0; xx < x2; xx++) {
int dx = xx * x / x2;
return out;

INI: Source Array
X: Width of Source Array
Y: Height of Source Array
X2: Width of the Output Array
Y2: Height of the Output Array
Returns OUT (Output Array)
How to use it:


//Need an image
Image temp=null;
try {
temp = Image.createImage("/tr1.gif");
} catch (IOException e) {
System.err.println("Can`t load the image : " + e.toString());
//Need an array (for RGB, with the size of original image)
int rgb[] = new int[temp.getWidth()*temp.getHeight()];
//Get the RGB array of image into "rgb"
//Call to our function and obtain RGB2
int rgb2[] = reescalaArray(rgb,temp.getWidth(),temp.getHeight(),newX,newY);
//Create an image with that RGB array
Image temp2 = Image.createRGBImage(rgb2,newX,newY,true);
author is LuisMiguel Rubio
Posted July 27, 2005 09:37 AM