更改位图的SampleSize

时间:2014-06-12 07:34:53

标签: android opengl-es bitmap bitmapfactory glsurfaceview

我正在从glsurfaceview创建一个位图并将其添加到arraylist但是当我从glsurfaceview创建一个位图时它会给outofmemory error

代码:

Bitmap bitmap = createBitmapFromGLSurface(0, 0, mEffectView.getWidth(),
            mEffectView.getHeight(), gl);
al_bitmaps.add(bitmap);

方法:

private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
        throws OutOfMemoryError {
    int bitmapBuffer[] = new int[w * h];
    int bitmapSource[] = new int[w * h];
    IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
    intBuffer.position(0);

    try {
        gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE,
                intBuffer);
        int offset1, offset2;
        for (int i = 0; i < h; i++) {
            offset1 = i * w;
            offset2 = (h - i - 1) * w;
            for (int j = 0; j < w; j++) {
                int texturePixel = bitmapBuffer[offset1 + j];
                int blue = (texturePixel >> 16) & 0xff;
                int red = (texturePixel << 16) & 0x00ff0000;
                int pixel = (texturePixel & 0xff00ff00) | red | blue;
                bitmapSource[offset2 + j] = pixel;
            }
        }
    } catch (GLException e) {
        return null;
    }

    return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}

对于较大分辨率的设备(例如三星Galaxy S4),我的应用程序崩溃了。

我想知道如何设置该位图的inSampleSize。

1 个答案:

答案 0 :(得分:4)

首先,我建议您使用

android:largeHeap="true"

在您的清单文件中。

次要的,如果没有帮助你看https://stackoverflow.com/a/17839597/2956344

另外,我建议您了解GLSufaceView的最大纹理尺寸,以便以编程方式检查位图分辨率的限制。

Get Maximum OpenGL ES 2.0 Texture Size Limit in Android

P.S。我想你找到有关使用GL10的信息。