Java Code Examples for javax.microedition.khronos.opengles.GL10#glReadPixels()

The following examples show how to use javax.microedition.khronos.opengles.GL10#glReadPixels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GLRenderWrapper.java    From AndroidRipper with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Extract the bitmap from OpenGL 
 * 
 * @param x the start column
 * @param y the start line
 * @param w the width of the bitmap
 * @param h the height of the bitmap
 * @param gl the current GL reference
 */

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

	for (int i = 0, k = 0; i < h; i++, k++) {
		// remember, that OpenGL bitmap is incompatible with Android bitmap
		// and so, some correction need.
		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 - k - 1) * w + j] = pix1;
		}
	}

	Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
	return sb;
}
 
Example 2
Source File: EffectsFilterActivity.java    From ImageEffects with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: BaseCameraActivity.java    From GPUVideo-android with MIT License 5 votes vote down vote up
private Bitmap createBitmapFromGLSurface(int w, int h, GL10 gl) {

        int bitmapBuffer[] = new int[w * h];
        int bitmapSource[] = new int[w * h];
        IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
        intBuffer.position(0);

        try {
            gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
            int offset1, offset2, texturePixel, blue, red, pixel;
            for (int i = 0; i < h; i++) {
                offset1 = i * w;
                offset2 = (h - i - 1) * w;
                for (int j = 0; j < w; j++) {
                    texturePixel = bitmapBuffer[offset1 + j];
                    blue = (texturePixel >> 16) & 0xff;
                    red = (texturePixel << 16) & 0x00ff0000;
                    pixel = (texturePixel & 0xff00ff00) | red | blue;
                    bitmapSource[offset2 + j] = pixel;
                }
            }
        } catch (GLException e) {
            Log.e("CreateBitmap", "createBitmapFromGLSurface: " + e.getMessage(), e);
            return null;
        }

        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    }
 
Example 4
Source File: BitmapUtil.java    From PhotoEditor with MIT License 5 votes vote down vote up
/**
 * Save filter bitmap from {@link ImageFilterView}
 *
 * @param glSurfaceView surface view on which is image is drawn
 * @param gl            open gl source to read pixels from {@link GLSurfaceView}
 * @return save bitmap
 * @throws OutOfMemoryError error when system is out of memory to load and save bitmap
 */
static Bitmap createBitmapFromGLSurface(GLSurfaceView glSurfaceView, GL10 gl) throws OutOfMemoryError {
    int x = 0, y = 0;
    int w = glSurfaceView.getWidth();
    int h = glSurfaceView.getHeight();
    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);
}
 
Example 5
Source File: BaseCameraActivity.java    From CameraRecorder-android with MIT License 5 votes vote down vote up
private Bitmap createBitmapFromGLSurface(int w, int h, GL10 gl) {

        int bitmapBuffer[] = new int[w * h];
        int bitmapSource[] = new int[w * h];
        IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
        intBuffer.position(0);

        try {
            gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
            int offset1, offset2, texturePixel, blue, red, pixel;
            for (int i = 0; i < h; i++) {
                offset1 = i * w;
                offset2 = (h - i - 1) * w;
                for (int j = 0; j < w; j++) {
                    texturePixel = bitmapBuffer[offset1 + j];
                    blue = (texturePixel >> 16) & 0xff;
                    red = (texturePixel << 16) & 0x00ff0000;
                    pixel = (texturePixel & 0xff00ff00) | red | blue;
                    bitmapSource[offset2 + j] = pixel;
                }
            }
        } catch (GLException e) {
            Log.e("CreateBitmap", "createBitmapFromGLSurface: " + e.getMessage(), e);
            return null;
        }

        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    }
 
Example 6
Source File: GSYVideoGLViewBaseRender.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 创建bitmap截图
 */
protected Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl) {
    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;
    }
    if (mHighShot) {
        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    } else {
        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.RGB_565);
    }
}
 
Example 7
Source File: OpenGLRenderer.java    From smartGL with Apache License 2.0 4 votes vote down vote up
private void doTakeScreenshot(GL10 gl) {

        if ((mScreenshotListener == null) || (mScreenshotHandler == null)) {
            return;
        }

        int width = getWidth();
        int height = getHeight();
        int source[] = new int[width * height];
        int dest[] = new int[width * height];
        IntBuffer wrap = IntBuffer.wrap(source);
        wrap.position(0);
        gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, wrap);

        int raw;
        int color;
        int offsetSrc;
        int offsetDst;
        for (int y = 0; y < height; y++) {
            offsetSrc = y * width;
            offsetDst = (height - y - 1) * width;
            for (int x = 0; x < width; x++) {
                raw = source[offsetSrc++];  // ABGR to ARGB
                color = (raw & 0xFF00FF00) | ((raw & 0x00FF0000) >> 16) | ((raw & 0x000000FF) << 16);
                dest[offsetDst++] = color;
            }
        }

        final Bitmap bitmap = Bitmap.createBitmap(dest, width, height, Bitmap.Config.ARGB_8888);
        mScreenshotHandler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    if (mScreenshotListener != null) {
                        if (bitmap != null) {
                            mScreenshotListener.screenshotTaken(bitmap);
                        }
                        mScreenshotListener = null;
                    }
                }
            }
        });

    }