站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题关于图象的缩放,用什么函数可以实现.

关于图象的缩放,用什么函数可以实现.

摘要: 关于图象的缩放,用什么函数可以实现.


关于图象的缩放,用什么办法可以实现.如放大图象2倍,或缩小怎么实现的.


up


自己实现,可以使用setclip实现像素切割


我是写手机程序的,有没有这方面的函数啊.


UP!:)


/*
*这几个参数必须
*
*/
private static final int FP_SHIFT = 13;
private static final int FP_ONE = 1 << FP_SHIFT;
private static final int FP_HALF = 1 << (FP_SHIFT - 1);

// 采集模式 - resizeImage() 中模式的有效参数
// 默认的模式可以为 MODE_BOX_FILTER 因为在resizeImage()被实现
public static final int MODE_POINT_SAMPLE = 0;
public static final int MODE_BOX_FILTER = 1;

/**
*得到像素
*将一个图像对象传入,将返回一个int型的像素数组。
*/
int[] getPixels(Image src) {
int w = src.getWidth();
int h = src.getHeight();
int[] pixels = new int[w * h];
src.getRGB(pixels,0,w,0,0,w,h);
return pixels;
}

/**
* 这个方法,可以用一个已有的像素数组,生成一个新的图象对象
*/
Image drawPixels(int[] pixels, int w, int h) {
return Image.createRGBImage(pixels,w,h,true);
}

/**
* resizeImage
* 传入已有图象作为参数,并且给出修改的参数,生成新的图象
* @参数 src 图形对象
* @参数 destW 目标图象的宽
* @参数 destH 目标图象的高
* @参数 mode 表示生成新图象的两种模式:
* MODE_POINT_SAMPLE - point sampled resizing, and MODE_BOX_FILTER - box filtered resizing (default).
* -点采样- -盒过滤-(默认)
* @返回一个修改后的图象.
*/
Image resizeImage(Image src, int destW, int destH, int mode) {
int srcW = src.getWidth();
int srcH = src.getHeight();

// 建立像素数组
int[] destPixels = new int[destW * destH]; // 目标图象的像素数组
int[] srcPixels = getPixels(src); // 源图像的像素数组

if (mode == MODE_POINT_SAMPLE) {
// 单点采样模式
// 遍历目标像素组,源像素经放大
for (int destY = 0; destY < destH; ++destY) {
for (int destX = 0; destX < destW; ++destX) {
int srcX = (destX * srcW) / destW;
int srcY = (destY * srcH) / destH;
destPixels[destX + destY * destW] = srcPixels[srcX + srcY * srcW];
}
}
}
else {
//源图象的密度系数
int ratioW = (srcW << FP_SHIFT) / destW;
int ratioH = (srcH << FP_SHIFT) / destH;

int[] tmpPixels = new int[destW * srcH]; // 水平重采样的临时缓存

// 执行添加混合的参数
int argb; // 源图象的提取颜色值
int a, r, g, b; // 被分的颜色通道
int count; //采样像素的平均值

// 重采样被分为简单的两步
// 第一步 将保持相同的高度 水平拉伸
// 第二步 垂直拉伸

// 水平采样
for (int y = 0; y < srcH; ++y) {
for (int destX = 0; destX < destW; ++destX) {
count = 0; a = 0; r = 0; b = 0; g = 0; //初始化颜色值
int srcX = (destX * ratioW) >> FP_SHIFT; // 计算开始采样
int srcX2 = ((destX + 1) * ratioW) >> FP_SHIFT; //计算计算采样

// 循环 并且添加颜色值到通道
do {
argb = srcPixels[srcX + y * srcW];
a += ((argb & 0xff000000) >> 24); // alpha 通道
r += ((argb & 0x00ff0000) >> 16); // 红色通道
g += ((argb & 0x0000ff00) >> 8); // 绿色通道
b += (argb & 0x000000ff); // 蓝色 通道
++count; // 像素的计算
++srcX; //移动到下一个像素
}
while (srcX <= srcX2 && srcX + y * srcW < srcPixels.length);

// 平均通道值
a /= count;
r /= count;
g /= count;
b /= count;

// 重建颜色通道值并放入缓存中
tmpPixels[destX + y * destW] = ((a << 24) | (r << 16) | (g << 8) | b);
}
}
// 垂直采样 (水平采样已经完成)
System.out.println("Vertical resampling...");
for (int x = 0; x < destW; ++x) {
for (int destY = 0; destY < destH; ++destY) {
count = 0; a = 0; r = 0; b = 0; g = 0; // initialize color blending vars
int srcY = (destY * ratioH) >> FP_SHIFT; // calculate beginning of sample
int srcY2 = ((destY + 1) * ratioH) >> FP_SHIFT; // calculate end of sample

// 循环 并且添加颜色值到通道
do {
argb = tmpPixels[x + srcY * destW];
a += ((argb & 0xff000000) >> 24); // alpha 通道
r += ((argb & 0x00ff0000) >> 16); // 红色通道
g += ((argb & 0x0000ff00) >> 8); // 绿色通道
b += (argb & 0x000000ff); //蓝色 通道
++count; // 像素的计算
++srcY; //移动到下一个像素
}
while (srcY <= srcY2 && x + srcY * destW < tmpPixels.length);

// 平均通道值
a /= count; a = (a > 255) ? 255 : a;
r /= count; r = (r > 255) ? 255 : r;
g /= count; g = (g > 255) ? 255 : g;
b /= count; b = (b > 255) ? 255 : b;

// 重建颜色通道值并放入缓存中
destPixels[x + destY * destW] = ((a << 24) | (r << 16) | (g << 8) | b);
}
}
}

// 用新的缓存返回新图象
return drawPixels(destPixels,destW,destH);
}


上面的方法太复杂了??

// 调整图片的大小
private Image creatNewImg(Image image) {

int sourceWidth = image.getWidth ();
int sourceHeight = image.getHeight ();
// 设置缩小图的宽度;
int newWidth = 168;
int newHeight = 168;
// if (newHeight == -1)
// //计算缩小图的高度
// newHeight = newWidth * sourceHeight / sourceWidth;
Image newImg = Image.createImage (newWidth, newHeight);
Graphics g = newImg.getGraphics ();
for(int y = 0; y < newHeight; y += 2) {
for(int x = 0; x < newWidth; x += 2) {
g.setClip (x, y, 2, 2);
int dx = x * sourceWidth / newWidth;
int dy = y * sourceHeight / newHeight;
// 按照比例计算原图片中的像素
g.drawImage (image, x - dx, y - dy, Graphics.LEFT
| Graphics.TOP);
}
}
Image fitImg = Image.createImage (newImg);
return fitImg;
}

一个方法搞定


可以使用SUN公司的JIMI,使用很方便


↑返回目录
前一篇: 跪求,Sony Ericsson手机上传文件的软件......
后一篇: 如何学习