How to save bitmap from GLSurfaceView (Only bitmap, not whole texture)
Asked Answered
D

1

6

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

Diagnostician answered 16/9, 2015 at 6:27 Comment(8)
The GLSurfaceView's surface contains exactly what you've drawn on it. Since you specified the coordinates when you drew the image, I'm not understanding why you don't know the coordinates when you want to read the image back.Stylobate
@Stylobate I am not very good with OpenGL-es. My code is almost same as the link i have given in my question. Now can you please answer for it to get the Image coordinate and take only image part with effect?Diagnostician
Why not crop the image afterwards?Antimacassar
@jyoon cropping is not the solution of that. Even to crop that Image I need to have coordinate. That's the solution of my issue. But I am not getting coordinate of Image top-left portion.Diagnostician
@jyoon it seems like you have removed your answer. Why?Diagnostician
@jyoon cropping the bitmap is not solution. I need to have image in its own resolution.Diagnostician
Ala kevi rite chat karu....Misuse
@iDroidExplorer did you got a solution to this issue...if yes then share it pleaseChloroprene
A
0

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.

Antimacassar answered 16/9, 2015 at 11:13 Comment(12)
OK Thanks for answer. Let me try that.Diagnostician
I am not getting the proper image when I am going to save it with the given calculation.Diagnostician
Also its not always Landscape image, It might possible to have portrait image as well.Diagnostician
You are going to account for the stray pixels (±1 to the value I gave you), and the image size is the current size on the screen, not the actual size.Antimacassar
anyway, Thanks for reply and support.Diagnostician
Yeah use this method to crop the image.Antimacassar
that method is not working. I have already inform you for same.Diagnostician
Please show us more code so we can see what you are exactly doing there.Antimacassar
It seems like you have not read my question properly. Please read it carefully. You will get the whole code.Diagnostician
https://mcmap.net/q/57605/-crop-a-bitmap-image that's the code for cropping bitmaps. Come on. You have literally done nothing to resolve this problem.Antimacassar
you know what this line in your given reference: resizedbitmap1=Bitmap.createBitmap(bmp, 0,0,yourwidth, yourheight); Its clear that it will start cropping from (0,0) that's what my issue here. Sorry but I don't want to crop my image like that instead inly want to generate the image with respected coordinate. I have already try the way you have explain. I have already commented on it. Please don't stretch the issue if you don't know solution.Diagnostician
Let us continue this discussion in chat.Antimacassar

© 2022 - 2024 — McMap. All rights reserved.