作者:mingjava 文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=144
以前在SUN的论坛看到过关于图片缩放的处理,是基于MIDP2.0实现的。今天看到有网友在问这个问题,因此去翻了一下以前的帖子找到了源代码,自己写了一个测试程序验证了一下效果还算可以。希望可以解这位网友的燃眉之急。
源代码如下:
package com.j2medev.image;
import javax.microedition.lcdui.*;
public class ImageUtil
{
// fixed point constants
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);
// resampling modes - valid values for the mode parameter of resizeImage()
// any other value will default to MODE_BOX_FILTER because of the way the
// conditionals are set in resizeImage()
public static final int MODE_POINT_SAMPLE = 0;
public static final int MODE_BOX_FILTER = 1;
/**
* getPixels Wrapper for pixel grabbing techniques. I separated this step
* into it's own function so that other APIs (Nokia, Motorola, Siemens,
* etc.) can easily substitute the MIDP 2.0 API (Image.getRGB()).
*
* @param src
* The source image whose pixels we are grabbing.
* @return An int array containing the pixels in 32 bit ARGB format.
*/
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;
}
/**
* drawPixels Wrapper for pixel drawing function. I separated this step into
* it's own function so that other APIs (Nokia, Motorola, Siemens, etc.) can
* easily substitute the MIDP 2.0 API (Image.createRGBImage()).
*
* @param pixels
* int array containing the pixels in 32 bit ARGB format.
* @param w
* The width of the image to be created.
* @param h
* The height of the image to be created. This parameter is
* actually superfluous, because it must equal pixels.length / w.
* @return The image created from the pixel array.
*/
Image drawPixels(int[] pixels, int w, int h)
{
return Image.createRGBImage(pixels, w, h, true);
}