Display black screen while capture screenshot of GLSurfaceView

后端 未结 1 1425
终归单人心
终归单人心 2021-01-03 09:48

I am wandering for seven hours for solving my issue. I have created project in which i m using GLSurfaceView on which i have set one image and afte

相关标签:
1条回答
  • 2021-01-03 10:26

    try this...

    private volatile boolean saveFrame;
    
    // called when Capture button is clicked.
    public void onClick(View view) {
        saveFrame = true;
        mEffectView.requestRender();
    }
    
    @Override
    public void onDrawFrame(GL10 gl) {
        if (!mInitialized) {
            // Only need to do this once
            mEffectContext = EffectContext.createWithCurrentGlContext();
            mTexRenderer.init();
            loadTextures();
            mInitialized = true;
        }
        if (mCurrentEffect != NONE) {
            // if an effect is chosen initialize it and apply it to the texture
            initEffect();
            applyEffect();
        }
        renderResult();
        if (saveFrame) {
            saveBitmap(takeScreenshot(gl));
            saveFrame = false;
        }
    }
    
    private void saveBitmap(Bitmap bitmap) {
        try {
            FileOutputStream stream = openFileOutput("image.png", MODE_PRIVATE);
            bitmap.compress(CompressFormat.PNG, 100, stream);
            stream.flush();
            stream.close();
            Log.i("TAG", "SAVED");
        } catch (Exception e) {
            Log.e("TAG", e.toString(), e);
        }
    }
    
    public Bitmap takeScreenshot(GL10 mGL) {
        final int mWidth = mEffectView.getWidth();
        final int mHeight = mEffectView.getHeight();
        IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
        IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight);
        mGL.glReadPixels(0, 0, mWidth, mHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
    
        // Convert upside down mirror-reversed image to right-side up normal image.
        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < mWidth; j++) {
                ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i * mWidth + j));
            }
        }
    
        Bitmap mBitmap = Bitmap.createBitmap(mWidth, mHeight,Bitmap.Config.ARGB_8888);
        mBitmap.copyPixelsFromBuffer(ibt);
        return mBitmap;
    }
    
    0 讨论(0)
提交回复
热议问题