【问题标题】:OutOfMemoryError When Apply Some effect filter on image在图像上应用某些效果过滤器时出现 OutOfMemoryError
【发布时间】:2016-02-24 07:37:57
【问题描述】:

我要制作像Retrica这样的应用程序

问题是我对图像应用了一些过滤器或效果,这会导致 java.lang.OutOfMemoryError 并需要更多时间来应用图像。

public class ImageEnhancemeane extends AppCompatActivity {

    private static final int[] FROM_COLOR = new int[]{49, 179, 110};
    private static final int THRESHOLD = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_enhancemeane);

        ImageView imgStatus = (ImageView) findViewById(R.id.backImage);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.testimg);

        imgStatus.setImageBitmap(createSepiaToningEffect(bm, 2, 0.2, 0.5, 0.59));

    }

    public static Bitmap createSepiaToningEffect(Bitmap src, int depth, double red, double green, double blue) {
        // image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // constant grayscale
        final double GS_RED = 0.3;
        final double GS_GREEN = 0.59;
        final double GS_BLUE = 0.11;
        // color information
        int A, R, G, B;
        int pixel;

        // scan through all pixels
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                // get pixel color
                pixel = src.getPixel(x, y);
                // get color on each channel
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // apply grayscale sample
                B = G = R = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);

                // apply intensity level for sepid-toning on each channel
                R += (depth * red);
                if (R > 255) {
                    R = 255;
                }

                G += (depth * green);
                if (G > 255) {
                    G = 255;
                }

                B += (depth * blue);
                if (B > 255) {
                    B = 255;
                }

                // set new pixel color to output image
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }
        src.recycle();
        src = null;
        // return final image
        return bmOut;
    }
}

这里是 createSepiaToningEffect() 方法是我对图像应用一些效果的过滤器,但它会导致 OutOfMemoryError。

我将在清单中使用 largeheap = true 解决 OutOfMemoryError,但我想要更好的解决方案。

当我调用过滤器并对图像应用一些效果时,这将需要一些时间。

请给出一些固体溶液。

【问题讨论】:

  • 你能请任何人帮助我吗

标签: android performance android-layout image-processing out-of-memory


【解决方案1】:

Here 是谷歌对此的评价:

但是,请求大堆的能力仅适用于 可以证明需要消耗更多 RAM 的一小部分应用程序(例如 作为大型照片编辑应用程序)。永远不要简单地请求大堆 因为你的内存已经用完了,你需要一个快速修复

避免使用位图浪费内存

当您加载位图时,仅以您需要的分辨率将其保存在 RAM 中 对于当前设备的屏幕,如果是原始屏幕,则将其缩小 位图是更高的分辨率。请记住,位图的增加 分辨率导致所需的内存相应(增加2), 因为 X 和 Y 维度都增加了。

为了更好地处理图像和内存,请参考以下开发人员建议:

Loading Large Bitmaps Efficiently

另外,建议您考虑使用一些 Android 图像库进行图像处理和加载。例如。毕加索,壁画,滑翔。每个都有自己的优点和缺点。选择最适合您并最适合您的需求,下面的帖子可能会引导您朝着正确的方向前进。

Picasso v/s Imageloader v/s Fresco vs Glide

Comparison of Image Library for Android (Picasso, Fresco, etc.)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 2013-01-29
    • 2014-12-20
    • 2017-01-14
    • 1970-01-01
    • 2015-02-21
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多