Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

android - How to save bitmap from GLSurfaceView (Only bitmap, not whole texture)

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:

enter image description here

Loaded image in the effect screen:

enter image description here

Image after applying effect and saved it:

enter image description here

This is what i want.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...