基于MIDP2.0实现图片的缩放功能[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

    /**
     * resizeImage Gets a source image along with new size for it and resizes
     * it.
     *
     * @param src
     *            The source image.
     * @param destW
     *            The new width for the destination image.
     * @param destH
     *            The new heigth for the destination image.
     * @param mode
     *            A flag indicating what type of resizing we want to do. It
     *            currently supports two type: MODE_POINT_SAMPLE - point sampled
     *            resizing, and MODE_BOX_FILTER - box filtered resizing
     *            (default).
     * @return The resized image.
     */
    Image resizeImage(Image src, int destW, int destH, int mode)
    {
        int srcW = src.getWidth();
        int srcH = src.getHeight();

        // create pixel arrays
        int[] destPixels = new int[destW * destH]; // array to hold destination
                                                   // pixels
        int[] srcPixels = getPixels(src); // array with source's pixels

        if (mode == MODE_POINT_SAMPLE)
        {
            // simple point smapled resizing
            // loop through the destination pixels, find the matching pixel on
            // the source and use that
            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
        {
            // precalculate src/dest ratios
            int ratioW = (srcW << FP_SHIFT) / destW;
            int ratioH = (srcH << FP_SHIFT) / destH;

本文关键:基于MIDP2.0实现图片的缩放功能
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top