【问题标题】:How can I decrease the size of a bitmap without loss of resolution in Android?如何在不损失 Android 分辨率的情况下减小位图的大小?
【发布时间】:2011-08-23 14:19:54
【问题描述】:

我有一个应用程序可以从相机中拍摄一张照片,然后我对该照片进行一些图像处理操作。但这需要太长时间,所以我想在图像处理操作之前减小位图的大小。重要的部分是在较小的图像中分辨率也必须很好。 android中是否有库,或者是否有人知道该操作的算法?

【问题讨论】:

  • 您是要缩小整个图像的大小,还是要截取一小部分并保持原样?
  • @markransom 是的,我想减小整个图像的大小
  • 基本上你想要的是某种形式的无损压缩——图像是什么文件格式?如果它们在位图​​中,那么只需将它们保存为高质量的 jpeg 就可以大大减少文件大小。或者您可以尝试在它们上运行类似pngcrush 的库。
  • @josephearl 文件格式是 jpg,但为了进行一些图像处理操作,我在 android 中将其更改为位图。此位图的大小为 1700*1400。如您所见,它太大了,所以我想在图像处理操作之前将其尺寸减小到 800*600 之类的,我该怎么办?
  • 最简单的方法就是将BitmapFactory.decodeFile 与具有inSampleSizeBitmapFactory.Options 一起使用,例如2 - 这将导致您获得的图像为 850*700(即宽度和高度的一半,以及内存使用量的四分之一)。

标签: android image-processing bitmap android-camera


【解决方案1】:

一种方法是取每个 n*n 块的平均值并将其转换为单个像素。它不是绝对最清晰的方法,也不会完全没有伪影,但我认为您会发现真实世界的结果是可以接受的——而且速度非常快。

【讨论】:

  • 这个操作不会损坏图像的形状
  • @barzos,形状将保持不变,但细节会减少。
  • @Ashishsingh,抱歉,我不精通 Android 或 Java,所以我可能会搞砸。如果您对图像具有像素级访问权限,这个概念很简单。
【解决方案2】:
    { Bitmap bit=Shrinkmethod(arrpath1[position], 100, 100); 
       //This reduce the size of image in 1kb size so u can also consider VM nativeheap memory

                    iv.setImageBitmap(bit);//ImageView Obj.

    }


    //To reduce the byte size of image in program

//user defined method to reduce size of image without reducing the quality of img.
        Bitmap Shrinkmethod(String file,int width,int height){
            BitmapFactory.Options bitopt=new BitmapFactory.Options();
            bitopt.inJustDecodeBounds=true;
            Bitmap bit=BitmapFactory.decodeFile(file, bitopt);

            int h=(int) Math.ceil(bitopt.outHeight/(float)height);
            int w=(int) Math.ceil(bitopt.outWidth/(float)width);

            if(h>1 || w>1){
                if(h>w){
                    bitopt.inSampleSize=h;

                }else{
                    bitopt.inSampleSize=w;
                }
            }
            bitopt.inJustDecodeBounds=false;
            bit=BitmapFactory.decodeFile(file, bitopt);



            return bit;

        }

//Try to upload ur project code and idea so that like u and me can get that to develop more android application..

Regard,
pranav

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 2016-02-04
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2013-05-04
    相关资源
    最近更新 更多