Author Topic: How can I get a bitmap from fb.getpixel()?  (Read 2083 times)

Offline ScienceHill

  • byte
  • *
  • Posts: 3
    • View Profile
How can I get a bitmap from fb.getpixel()?
« on: November 29, 2017, 06:41:28 am »
Hello. Currently I am trying to take a screenshot use fb.getpixel() function. And here is my code:
```
int[] my_pixels = fb.getPixels();
Bitmap bitmap = Bitmap.createBitmap(fb.getWidth(), fb.getHeight(), Bitmap.Config.ARGB_4444);
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(my_pixels));
```
But I just got a black image. And I also tried this:
```
int[] tmpPixels = fb.getPixels();
for (int i = 0; i < tmpPixels.length; i++){
   int tmpInt = ((tmpPixels & 0xf8) >> 3);
   tmpInt += ((tmpPixels & 0xfc00) >> 5);
   tmpInt += ((tmpPixels & 0xf80000) >> 11);
  tmpPixels = tmpInt;
}
Bitmap lastImage = Bitmap.createBitmap(tmpPixels, fb.getWidth(), fb.getHeight(), Bitmap.Config.RGB_565);
```
But it still did not work. I know that the fb.getPixels() function return an int array. And it contains R, G and B channels value. But how can I actually use it? Any answer is appreciated and thanks in advance.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How can I get a bitmap from fb.getpixel()?
« Reply #1 on: November 29, 2017, 08:20:22 am »
Can you check the actual array content? Maybe it's all 0 at that stage already from some reason.

Offline ScienceHill

  • byte
  • *
  • Posts: 3
    • View Profile
Re: How can I get a bitmap from fb.getpixel()?
« Reply #2 on: December 02, 2017, 11:55:58 pm »
Thank you for your reply! And I had figured out why it didn't work. You are right. My frame buffer was empty. To solve this problem, I just put the getPixels() method in the onDrawFrame like this:
```
public int[] tmpPixels;// Global variance
public void onDrawFrame(GL10 gl) {
            tmpPixels = fb.getPixels();
            fb.clear(back);

            world.renderScene(fb);
            world.draw(fb);
            fb.display();
}
```
Then I convert the int array (tmpPixels) to bitmap and save it in my dispatchKeyEvent like this way:
```
case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    Bitmap lastImage = Bitmap.createBitmap(fb.getWidth(), fb.getHeight(), Bitmap.Config.ARGB_4444);
                    lastImage.copyPixelsFromBuffer(IntBuffer.wrap(tmpPixels));

                    File sdCard = Environment.getExternalStorageDirectory();
                    File dir = new File (sdCard.getAbsolutePath() + "/my_jpg_test");
                    Log.i("JPCT","My File path" + sdCard.getAbsolutePath() + "/my_jpg_test");
                    dir.mkdirs();

                    String fileName = String.format("The.jpg");
                    File outFile = new File(dir, fileName);
                    FileOutputStream outputStream = null;
                    try {
                        outputStream = new FileOutputStream(outFile);
                        int quality = 100;
                        lastImage.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                        outputStream.flush();
                        outputStream.close();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
}
```
And it works!
By the way, obviously, the program becomes extremely slow by using tmpPixels to record the pixel of image on onDrawFrame in this way. Of course I will use some flags to control this process. Thanks again!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How can I get a bitmap from fb.getpixel()?
« Reply #3 on: December 04, 2017, 08:22:33 am »
Yes, reading the pixels from GPU memory is a slow process...