【问题标题】:Android GLSurfaceView Saving bitmap causes exceptionAndroid GLSurfaceView 保存位图导致异常
【发布时间】:2016-06-23 14:16:20
【问题描述】:

我修改了this project,它使用 GLSurfaceView 和 Effects 来显示 ViewPager,其中一些效果应用于图像。

此外,我创建了一个叠加位图,在应用效果后将其覆盖在每个图像上。

到目前为止,该应用程序运行良好。但是现在我必须在按下按钮时将显示的图像保存在文件中。

所以我使用了这个代码:

 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) {
            e.printStackTrace();
            return null;
        }

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

获取位图。当按钮被按下时,我调用这个方法:

protected void onClick() {
        read = true;
        mEffectView.requestRender();
}

这会强制渲染,所以我生成位图并使用 AsyncTask 将其保存在文件中。 readonDrawFrame(GL10 gl) 中用作信号量,以便在我想保存位图时仅生成位图。

保存一张图片效果很好。当我保存第二个,然后我更改页面时,出现此错误:

A/Bitmap: Failed to acquire strong reference to pixels
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 20475 (GLThread 9540)

另一个问题是,尽管显示了叠加层,但并未将其保存在图像中。 这就是我应用它的方式:

一代

EffectFactory effectFactory = mEffectContext.getFactory();
overlayEffect = effectFactory.createEffect(EffectFactory.EFFECT_BITMAPOVERLAY);
overlayEffect.setParameter("bitmap", overlay);

效果应用

mEffect.apply(mTextures[0], mImageWidth, mImageHeight, mTextures[1]);
overlayEffect.apply(mTextures[1], mImageWidth, mImageHeight, mTextures[2]);

使用 mEffect 是保存图像时唯一可见的效果。

我做错了什么?

编辑 我解决了最后一个问题:我发现你必须releaserecreate你每次使用的每个Effect对象被称为mEffectView.requestRender() em>。

【问题讨论】:

    标签: android exception bitmap glsurfaceview


    【解决方案1】:

    显然,当使用

    overlayEffect = effectFactory.createEffect(EffectFactory.EFFECT_BITMAPOVERLAY);
    overlayEffect.setParameter("bitmap", overlay);
    

    传递的位图被回收了!​​strong>

    所以我解决了传递它的副本的问题:

    overlayEffect = effectFactory.createEffect(EffectFactory.EFFECT_BITMAPOVERLAY);
    overlayEffect.setParameter("bitmap", overlay.copy(overlay.getConfig(), false));
    

    希望这对其他人有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2016-04-21
      • 2012-12-15
      • 2011-06-07
      • 2011-07-16
      • 1970-01-01
      相关资源
      最近更新 更多