android - 更改位图的样本大小

标签 android opengl-es bitmap bitmapfactory glsurfaceview

我正在从 glsurfaceview 创建一个位图并将其添加到数组列表中,但是当我从 glsurfaceview 创建一个位图时它会出现 outofmemory 错误

代码:

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

对于分辨率更大的设备(例如 Samsung Galaxy S4),我的应用会崩溃。

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

最佳答案

首先我会推荐给你使用

android:largeHeap="true"

在您的 list 文件中。

中学,如果这对您没有帮助,请看https://stackoverflow.com/a/17839597/2956344

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

Get Maximum OpenGL ES 2.0 Texture Size Limit in Android

附言我想您找到了有关使用 GL10 的信息。

关于android - 更改位图的样本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24178863/

相关文章:

android - 如何检查 EGL 上下文是否被保留?

c - C 程序中的文本文件到位图

c++ - 从内存缓冲区创建 HBITMAP

android - 来自 androidx 和 com.android.support 的重复类

java - Android 对所有 Activity 使用独特的 onDestroy

javascript - WebGL 基本着色器困惑

android - 使用 libjpegturbo 压缩一批图片时出现奇怪的结果

android - 为什么在 Android 中缺少传感器类型 3?

android - 如何将我的 api key 与加载程序一起使用?

oop - 在面向对象的设置中使用OpenGL绘图操作?