如何用GlSurfaceView创建位图?
澳门威尼斯人官网
澳门威尼斯人官网
当前位置 : 澳门威尼斯人官网 > 威尼斯人官网

如何用GlSurfaceView创建位图?

在Cocos2d里,我想用Glsurfaceview截取图像,但是返回的却是透明图像,代码如下:

GlsurfaceView glv=CCDirector.sharedDirector.getOpenGLView;
glv.setDrawingCacheEnabledtrue;
Bitmap bitmap=glv.getDrawingCache;

原问题:[Cocos2d]How to Create bitmap from GlSurfaceView

答案
Peter:解决方案如下。

if MainImageProcessingActivity.capture {
        int width = MainImageProcessingActivity.w;
        int height = MainImageProcessingActivity.h;
        int screenshotSize = width * height;
        ByteBuffer bb = ByteBuffer.allocateDirectscreenshotSize * 4;
        bb.orderByteOrder.nativeOrder;
        gl.glReadPixels0, 0, width, height, GL10.GL_RGBA,
                GL10.GL_UNSIGNED_BYTE, bb;
        int pixelsBuffer[] = new int[screenshotSize];
        bb.asIntBuffer.getpixelsBuffer;
        bb = null;
        Bitmap bitmap = Bitmap.createBitmapwidth, height,
                Bitmap.Config.RGB_565;
        bitmap.setPixelspixelsBuffer, screenshotSize - width, -width, 0,
                0, width, height;
        pixelsBuffer = null;

        short sBuffer[] = new short[screenshotSize];
        ShortBuffer sb = ShortBuffer.wrapsBuffer;
        bitmap.copyPixelsToBuffersb;

        // Making created bitmap from OpenGL points compatible with
        // Android bitmap
        for int i = 0; i < screenshotSize; ++i {
            short v = sBuffer[i];
            sBuffer[i] = short v & 0x1f << 11 | v & 0x7e0 | v & 0xf800 >> 11;
        }
        sb.rewind;
        bitmap.copyPixelsFromBuffersb;
        MainImageProcessingActivity.captureBmp = bitmap.copyBitmap.Config.ARGB_8888,false;
        MainImageProcessingActivity.capture=false;
    }

DroidBot(提问者):感谢Peter,我把这个代码用于我的渲染器和onDraw方法里,非常好用!

栏目列表

广告位

澳门威尼斯人官网