如何从 GLSurfaceView 保存位图(仅位图,而不是整个纹理) [英] How to save bitmap from GLSurfaceView (Only bitmap, not whole texture)

查看:21
本文介绍了如何从 GLSurfaceView 保存位图(仅位图,而不是整个纹理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码对 GlSurfaceView 上的位图产生多种效果.

在效果画面中加载图片:

应用效果并保存后的图片:

这就是我想要的.

解决方案

如果能得到图片的尺寸,就可以得到位图的准确位置.

设图片尺寸为xy,按照这篇文章.假设是 ab.

您的起始位置将是 ((a-x)/2, (b-y)/2).

使用它来裁剪您的图像.

I am using this code to give multiple effect on bitmap that is on GlSurfaceView. apply-effects-on-image-using-effects

Now, I want to save the bitmap. They have given the code to save the bitmap but with that, whole GlSurfaceView is going to be saved as bitmap image. Instead I want to save only bitmap area to save as Image.

There is method that takes pixels and make bitmap from that and also make image. e.g.:

  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;
 }

I guess, I need to update it like, instead it to be start with 0, 0, that should be start with bitmap top left coordinate. And that should work till bitmap height and width.

So, with that I can able to resolved my issue but don't know how to get that coordinate of bitmap image in GLSurfaceView.

Please check below image for more clarification.

Original Image:

Loaded image in the effect screen:

Image after applying effect and saved it:

This is what i want.

解决方案

If you can get the dimensions of the image, you can get the exact position of the bitmap.

Let the image dimensions be x and y, and get the screen dimensions following this post. Let that be a and b.

Your starting position will be ((a-x)/2, (b-y)/2).

Use this to crop your image.

这篇关于如何从 GLSurfaceView 保存位图(仅位图,而不是整个纹理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆