Converting Android Bitmap to LibGdx's Texture

前端 未结 4 1247
失恋的感觉
失恋的感觉 2020-12-03 16:31

i am trying to convert bitmap into a libGDX Texture by converting:

  1. Android Bitmap to byte[]
  2. byte[] to libGDX Pi
相关标签:
4条回答
  • 2020-12-03 16:51

    Are you committed to that operational pipeline? Here's an alternate way to do the conversion you asked for:

    1. Implement com.badlogic.gdx.graphics.TextureData to take an android.graphics.Bitmap in the constructor.
    2. In prepare(), allocate and fill an IntBuffer using Bitmap.getPixels.
    3. Use bitmath to swap the data in the buffer from ARGB to RGBA: (arr[i] << 8) | (arr[i]>>24)
    4. In getType() return TextureData.TextureDataType.Compressed
    5. In consumeCompressedData() call glTexImage2D to feed the data into the (prebound) texture.

    Your pipeline above includes several copies and recompressions of pixel data; this pipeline allows you to feed your Bitmap data straight into the texture with only one copy (necessary for the byte format conversion anyway). Would that work for you?

    0 讨论(0)
  • 2020-12-03 17:11

    you have to code on a new thread because pixmap class takes time for byte conversion and sometimes returns a temporary pixmap in case the process hasnt finished so its better to run on a seperate thread and your problem will be solved.

    0 讨论(0)
  • 2020-12-03 17:16

    hmm another possibility is that you've got a threading issue. I've noticed this kind of problem when loading my own unmanaged textures on the UI thread while libgdx is doing its thing loading textures concurrently on the render thread. If this is the problem the simple solution is to synchronize the creation of the texture with the render thread using Gdx.app.postRunnable. i.e.:

    public void onByteArrayOfCroppedImageReciever(byte[] bytes) {
        try {
            pmap=new Pixmap(bytes, 0, bytes.length);
            Gdx.app.postRunnable(new Runnable() {
                @Override
                public void run() {            
                    tex=new Texture(pmap);
                    face=new Sprite(tex);
                }
            });
        } catch(Exception e) {
            Gdx.app.log("KS", e.toString());
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-12-03 17:16

    If the goal is to convert an Android Bitmap to a libgdx Texture, you don't need to use Pixmap at all. You can do it directly with the help of simple OpenGL and Android GLUtils. Try the followings; it is 100x faster than your solution. I assume that you are not in the rendering thread (you should not most likely). If you are, you don't need to call postRunnable().

           Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
              Texture tex = new Texture(bitmap.getWidth(), bitmap.getHeight(), Format.RGBA8888);
              GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex.getTextureObjectHandle());
              GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
              GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
              bitmap.recycle();
              // now you have the texture to do whatever you want
            }
          });
    
    0 讨论(0)
提交回复
热议问题