博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图象的放缩与透明处理
阅读量:4045 次
发布时间:2019-05-24

本文共 3898 字,大约阅读时间需要 12 分钟。

/*这是一个图象放缩类,能处理图象的按指定宽高放缩,以及图象的颜色与操作,这样能改变图象透明度

 */
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);
    }

 

}

转载地址:http://udedi.baihongyu.com/

你可能感兴趣的文章
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
关于进制转换的具体实现代码
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
mysql 主从同步配置
查看>>
为什么很多程序员都选择跳槽?
查看>>
mongdb介绍
查看>>
mongdb安装使用
查看>>
mongdb在java中的应用
查看>>