天天看点

j2me 图片的缩放 图片的过滤 - 天末凉风

j2me 图片的缩放 图片的过滤

当时记得也是从网上搜半天,找到一个能缩放图片的算法实现,还有将选中的图片改变显示,用其它颜色过滤一下.代码如下:import javax.microedition.lcdui.Image;

public class GraphicsUtil {

public static final int SHIFT_RED_TO_GREEN = 0;

public static final int SHIFT_RED_TO_BLUE = 1;

public static final int SHIFT_GREEN_TO_BLUE = 2;

public static final int SHIFT_GREEN_TO_RED = 3;

public static final int SHIFT_BLUE_TO_RED = 4;

public static final int SHIFT_BLUE_TO_GREEN = 5;

public static int[] flipImageColor(int[] rgbData, int shiftType) {

// we start by getting the image data into an int array - the number

// of 32-bit ints is equal to the width multiplied by the height

// now go through every pixel and adjust it\'s color

for (int i = 0; i < rgbData.length; i++) {

int p = rgbData[i];

// split out the different byte components of the pixel by

// applying

// a mask so we only get what we need, then shift it to make it

// a normal number we can play around with

int a = ((p & 0xff000000) >> 24);

int r = ((p & 0x00ff0000) >> 16);

int g = ((p & 0x0000ff00) >> 8);

int b = ((p & 0x000000ff) >> 0);

int ba = a, br = r, bb = b, bg = g; // backup copies

// flip the colors around according to the operation required

switch (shiftType) {

case SHIFT_RED_TO_GREEN:

g = r;

r = bg;

break;

case SHIFT_RED_TO_BLUE:

b = r;

r = bb;

break;

case SHIFT_GREEN_TO_BLUE:

g = b;

b = bg;

break;

case SHIFT_GREEN_TO_RED:

g = r;

r = bg;

break;

case SHIFT_BLUE_TO_RED:

b = r;

r = bb;

break;

case SHIFT_BLUE_TO_GREEN:

b = g;

g = bb;

break;

}

// shift all our values back in

rgbData[i] = (a << 24) | (r << 16) | (g << 8) | b;

}

return rgbData;

}

public static int[] ZoomImage(Image srcImg,int desW,int desH){

int srcW = srcImg.getWidth(); //原始图像宽

int srcH = srcImg.getHeight(); //原始图像高

int[] srcBuf = new int[srcW * srcH]; //原始图片像素信息缓存

if (srcImg.isMutable()) { /*如果是可变图像*/

} else { /*如果是非可变图像*/

srcImg.getRGB(srcBuf, 0, srcW,0,0,srcW,srcH);

}

int[] desBuf = new int[desW * desH]; //缩放后图片像素信息缓存

// Image desImg = Image.createImage(desW, desH);

//计算插值表

int[] tabY = new int[desH];

int[] tabX = new int[desW];

int sb = 0;

int db = 0;

int tems = 0;

int temd = 0;

int distance = srcH > desH ? srcH : desH;

for (int i = 0; i <= distance; i++) { /*垂直方向*/

tabY[db] = (short) sb;

tems += srcH;

temd += desH;

if (tems > distance) {

tems -= distance;

sb++;

}

if (temd > distance) {

temd -= distance;

db++;

}

}

sb = 0;

db = 0;

tems = 0;

temd = 0;

distance = srcW > desW ? srcW : desW;

for (int i = 0; i <= distance; i++) { /*水平方向*/

tabX[db] = (short) sb;

tems += srcW;

temd += desW;

if (tems > distance) {

tems -= distance;

sb++;

}

if (temd > distance) {

temd -= distance;

db++;

}

}

int dx = 0;

int dy = 0;

int sy = 0;

int oldy = -1;

for (int i = 0; i < desH; i++) {

if (oldy == tabY[i]) {

System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);

} else {

dx = 0;

for (int j = 0; j < desW; j++) {

desBuf[dy + dx] = srcBuf[sy + tabX[j]];

dx++;

}

sy += (tabY[i] - oldy) * srcW;

}

oldy = tabY[i];

dy += desW;

}

return desBuf;

}