【问题标题】:Image looks blur after down sampling下采样后图像看起来模糊
【发布时间】:2015-01-02 04:28:12
【问题描述】:

我正在开发一个具有缩放功能的小项目,但是当我缩小图像时,它的质量会保持原来的质量,我使用以下代码进行图像缩小采样,但是当我缩小图像时,它看起来很模糊(看起来很沉闷)并且对于缩小规模需要大量时间,我需要快速缩小规模,请帮助我更正我的代码。提前谢谢..

Bitmap downscaleBitmap_new(Bitmap src,int targetWidth, int targetHeight, int off)
 {
     float r = (src.getWidth()-off)/(float)targetWidth;
     float s = (src.getHeight()-off)/(float)targetHeight;
     Bitmap target = Bitmap.createBitmap(Math.round(src.getWidth()/r), Math.round(src.getHeight()/s), Config.ARGB_8888);
     r = src.getWidth()/(float)target.getWidth();
     s = src.getHeight()/(float)target.getHeight();
     int argb;
     int red;
     int green;
     int blue;
     int alpha;
     float wx;
     float wy;
     float n;
     for(int i=0;i<target.getWidth();++i){
         for(int j=0;j<target.getHeight();++j){
             red = 0;
             green = 0;
             blue = 0;
             alpha = 0;
             n=0;
             for(int k =(int)(i*r);k<Math.round((i+1)*r);++k){
                 if(k<i*r){
                     wx = k-i*r+1;
                 }else{
                     if(k+1>(i+1)*r)
                         wx = (i+1)*r-k;
                     else
                         wx = 1;
                 }
                 for(int l=(int)(j*s);l<Math.round((j+1)*s);++l){
                     if(l<j*s){
                         wy = l-j*s+1;
                     }else{
                         if(l+1>(j+1)*s)
                             wy = (j+1)*s-l;
                         else
                             wy = 1;
                     }
                     n+=wy*wx;
                     argb=src.getPixel(k, l);
                     red += wx*wy*Color.red(argb);
                     green += wx*wy*Color.green(argb);
                     blue += wx*wy*Color.blue(argb);
                     alpha += wx*wy*Color.alpha(argb);

                 }
             }
             target.setPixel(i, j, Color.argb((int)(alpha/n), (int)(red/n), (int)(green/n), (int)(blue/n)));
         }
     }
     return target;
 }

【问题讨论】:

    标签: android image-processing bitmap


    【解决方案1】:

    为什么您认为缩小图像不会改变其质量?由于插值,您将获得较差的质量。没有办法保持图像的质量。您可以使用三次或双线性插值代替线性以获得更好的输出。

    Bitmap.createScaledBitmap(src, new_width, new_height, true);

    这将使用双线性过滤器,您将获得良好的结果。如果您想要更好的性能,您可能需要查看 renderscript 并实现更好的算法。您还可以使用 OpenCV 或其他图像处理库等 3rd 方库,但对应用程序来说会很重。

    【讨论】:

    • 感谢您的回答.. 实际上我在缩小图像时使用 createScaledBitmap,它的质量非常差.. 我需要在缩小结束时应用图像过滤器。请分享一些第 3 方库的链接,OpenCV或渲染脚本
    • 只是一个想法,当你缩小时,你可以缩小画布/图像视图的大小,并渲染一个位图。我认为android会照顾质量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2015-04-25
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多