[Cocos2d]如何从GlSurfaceView创建位图

发布于 2024-12-06 12:38:57 字数 260 浏览 1 评论 0原文

如何在 Cocos2d 中截取 Glsurfaceview 的屏幕截图?我尝试使用 GLsurfaceView 编写以下代码,

    GlsurfaceView glv=CCDirector.sharedDirector().getOpenGLView();
    glv.setDrawingCacheEnabled(true);
    Bitmap bitmap=glv.getDrawingCache();

但它返回透明图像。

How can I take screen shot of Glsurfaceview in Cocos2d. I tried with following code using GLsurfaceView

    GlsurfaceView glv=CCDirector.sharedDirector().getOpenGLView();
    glv.setDrawingCacheEnabled(true);
    Bitmap bitmap=glv.getDrawingCache();

but it return transparent image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烛影斜 2024-12-13 12:38:57

我从这个 anddev 论坛问题得到了答案,我附上了代码,希望有人能找到这个有帮助

请将此代码放入渲染器类的 onDraw 方法内启动。

public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{  
    int b[]=new int[w*h];
    int bt[]=new int[w*h];
    IntBuffer ib=IntBuffer.wrap(b);
    ib.position(0);
    gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

    /*  remember, that OpenGL bitmap is incompatible with 
        Android bitmap and so, some correction need.
     */   
    for(int i=0; i<h; i++)
    {         
        for(int j=0; j<w; j++)
        {
            int pix=b[i*w+j];
            int pb=(pix>>16)&0xff;
            int pr=(pix<<16)&0x00ff0000;
            int pix1=(pix&0xff00ff00) | pr | pb;
            bt[(h-i-1)*w+j]=pix1;
        }
    }              
    Bitmap sb=Bitmap.createBitmap(bt, w, h, true);
    return sb;
}

public static void SavePNG(int x, int y, int w, int h, String name, GL10 gl)
{
    Bitmap bmp=SavePixels(x,y,w,h,gl);
    try
    {
        FileOutputStream fos=new FileOutputStream("/sdcard/my_app/"+name);
        bmp.compress(CompressFormat.PNG, 100, fos);
        try
        {
            fos.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try
        {
            fos.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    catch (FileNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }              
}

I got answer from this anddev forum question I attached code along with this hope somebody will find this helpful

Please Put this code inside renderer class onDraw Method inside starting.

public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{  
    int b[]=new int[w*h];
    int bt[]=new int[w*h];
    IntBuffer ib=IntBuffer.wrap(b);
    ib.position(0);
    gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

    /*  remember, that OpenGL bitmap is incompatible with 
        Android bitmap and so, some correction need.
     */   
    for(int i=0; i<h; i++)
    {         
        for(int j=0; j<w; j++)
        {
            int pix=b[i*w+j];
            int pb=(pix>>16)&0xff;
            int pr=(pix<<16)&0x00ff0000;
            int pix1=(pix&0xff00ff00) | pr | pb;
            bt[(h-i-1)*w+j]=pix1;
        }
    }              
    Bitmap sb=Bitmap.createBitmap(bt, w, h, true);
    return sb;
}

public static void SavePNG(int x, int y, int w, int h, String name, GL10 gl)
{
    Bitmap bmp=SavePixels(x,y,w,h,gl);
    try
    {
        FileOutputStream fos=new FileOutputStream("/sdcard/my_app/"+name);
        bmp.compress(CompressFormat.PNG, 100, fos);
        try
        {
            fos.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try
        {
            fos.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    catch (FileNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }              
}
墨洒年华 2024-12-13 12:38:57

这是解决方案:

if (MainImageProcessingActivity.capture) {
        int width = MainImageProcessingActivity.w;
        int height = MainImageProcessingActivity.h;
        int screenshotSize = width * height;
        ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
        bb.order(ByteOrder.nativeOrder());
        gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA,
                GL10.GL_UNSIGNED_BYTE, bb);
        int pixelsBuffer[] = new int[screenshotSize];
        bb.asIntBuffer().get(pixelsBuffer);
        bb = null;
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.RGB_565);
        bitmap.setPixels(pixelsBuffer, screenshotSize - width, -width, 0,
                0, width, height);
        pixelsBuffer = null;

        short sBuffer[] = new short[screenshotSize];
        ShortBuffer sb = ShortBuffer.wrap(sBuffer);
        bitmap.copyPixelsToBuffer(sb);

        // 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.copyPixelsFromBuffer(sb);
        MainImageProcessingActivity.captureBmp = bitmap.copy(Bitmap.Config.ARGB_8888,false);
        MainImageProcessingActivity.capture=false;
    }

将代码放在 onDrawFrame(GL10 gl) 方法下,它可以工作!

Here is solution:

if (MainImageProcessingActivity.capture) {
        int width = MainImageProcessingActivity.w;
        int height = MainImageProcessingActivity.h;
        int screenshotSize = width * height;
        ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
        bb.order(ByteOrder.nativeOrder());
        gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA,
                GL10.GL_UNSIGNED_BYTE, bb);
        int pixelsBuffer[] = new int[screenshotSize];
        bb.asIntBuffer().get(pixelsBuffer);
        bb = null;
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.RGB_565);
        bitmap.setPixels(pixelsBuffer, screenshotSize - width, -width, 0,
                0, width, height);
        pixelsBuffer = null;

        short sBuffer[] = new short[screenshotSize];
        ShortBuffer sb = ShortBuffer.wrap(sBuffer);
        bitmap.copyPixelsToBuffer(sb);

        // 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.copyPixelsFromBuffer(sb);
        MainImageProcessingActivity.captureBmp = bitmap.copy(Bitmap.Config.ARGB_8888,false);
        MainImageProcessingActivity.capture=false;
    }

put the code under onDrawFrame(GL10 gl) method, IT WORKS!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文