图象的放缩与透明处理

来源:互联网 发布:我国外汇储备数据 编辑:程序博客网 时间:2024/06/09 16:09

/*这是一个图象放缩类,能处理图象的按指定宽高放缩,以及图象的颜色与操作,这样能改变图象透明度
 */
public class imageutil
{
    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);
    public static final int mode_point_sample = 0;
    public static final int mode_box_filter = 1;
   
   public 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;
    }
   
   public image drawpixels(int[] pixels, int w, int h)
    {
        return image.creatergbimage(pixels, w, h, true);
    }
   public 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;

 

            int[] tmppixels = new int[destw * srch]; 

 

            for (int y = 0; y < srch; ++y)
            {
                for (int destx = 0; destx < destw; ++destx)
                {
                    count = 0;
                    a = 0;
                    r = 0;
                    b = 0;
                    g = 0; // initialize color blending vars
                    int srcx = (destx * ratiow) >> fp_shift; // calculate
                                                             // beginning of
                                                             // sample
                    int srcx2 = ((destx + 1) * ratiow) >> fp_shift; // calculate
                                                                    // end of
                                                                    // sample

 

                                       do
                    {
                        argb = srcpixels[srcx + y * srcw];
                        a += ((argb & 0xff000000) >> 24); // alpha channel
                        r += ((argb & 0x00ff0000) >> 16); // red channel
                        g += ((argb & 0x0000ff00) >> 8); // green channel
                        b += (argb & 0x000000ff); // blue channel
                        ++count; // count the pixel
                        ++srcx; // move on to the next pixel
                    } while (srcx <= srcx2
                            && srcx + y * srcw < srcpixels.length);

 

                    // average out the channel values
                    a /= count;
                    r /= count;
                    g /= count;
                    b /= count;

 

                                      tmppixels[destx + y * destw] = ((a << 24) | (r << 16)
                            | (g << 8) | b);
                }
            }

 

            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

 

                    // now loop from srcy to srcy2 and add up the values for
                    // each channel
                    do
                    {
                        argb = tmppixels[x + srcy * destw];
                        a += ((argb & 0xff000000) >> 24); // alpha channel
                        r += ((argb & 0x00ff0000) >> 16); // red channel
                        g += ((argb & 0x0000ff00) >> 8); // green channel
                        b += (argb & 0x000000ff); // blue channel
                        ++count; // count the pixel
                        ++srcy; // move on to the next pixel
                    } while (srcy <= srcy2
                            && x + srcy * destw < tmppixels.length);

 

                    // average out the channel values
                    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;

 

                    // recreate color from the averaged channels and place it
                    // into the destination buffer
                    destpixels[x + desty * destw] = ((a << 24) | (r << 16)
                            | (g << 8) | b);
                }
            }
        }

 

        // return a new image created from the destination pixel buffer
        return drawpixels(destpixels, destw, desth);
    }
   
   public image setalphapixel(image src,int eliminatepixel,int alpha,int width,int height ){
        //use the source image to change the alpha pixel where the special point's pixel is the same with eliminatepixel
        //and return the changed image  可以用颜色的与操作改变透明度
         int pixels[]=getpixels(src);
           for(int i=0;i<pixels.length;i++){
               if(pixels[i]==eliminatepixel){
                   pixels[i]=pixels[i]&alpha;
               }
           }
         return drawpixels(pixels, width, height);
    }

 

}

原创粉丝点击