How to use byte[] with AndEngine?!

  ... the case you feel the need for a new feature or want to submit one.

How to use byte[] with AndEngine?!

Postby vanste25 » Mon Dec 02, 2013 4:41 pm

I have image in PNG format and after decoding it, i want to display it on the screen, but i did not find proper way to do that. Data can be loaded from source, resources or assets, but what about the simplest byte[]?

Is it possible that such easy task is so hard to implement?

I have found a way, but it consumes too much time.
I decode byte[] and create Bitmap of it, and then convert Bitmap to textureRegion . After that, i create Sprite of TextureRegion.

ITextureRegion convertBmpToTextureRegion(byte [] byteArray) {
if (byteArray == null) {
return null;
}

/* convert byteArray to Bitmap */
mOutBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
if(mOutBitmap == null) {
return null;
}

/* convert bitmap to sprite */
this.mSource = new BitmapTextureAtlasSource(mOutBitmap);

this.mTexture = new BitmapTextureAtlas(getTextureManager(), mOutBitmap.getWidth(), mOutBitmap.getHeight());

mTexture.addTextureAtlasSource(mSource, 0, 0);
mTexture.load();
return (ITextureRegion) TextureRegionFactory.createFromSource(mTexture, mSource, 0, 0);
}

this.mSource = new BitmapTextureAtlasSource(mOutBitmap); consumes too much time, so i want to find another aproach.

Thank you!
vanste25
 
Posts: 2
Joined: Mon Dec 02, 2013 4:23 pm

Re: How to use byte[] with AndEngine?!

Postby smartus » Mon Dec 02, 2013 6:06 pm

It is possible. Look at the following:

BitmapTextureAtlasTextureRegionFactory.createFromAsset

there notice how the source is created:

AssetBitmapTextureAtlasSource.create

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public static AssetBitmapTextureAtlasSource create(final AssetManager pAssetManager, final String pAssetPath, final int pTextureX, final int pTextureY) {
  2.                 final BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
  3.                 decodeOptions.inJustDecodeBounds = true;
  4.  
  5.                 InputStream in = null;
  6.                 try {
  7.                         in = pAssetManager.open(pAssetPath);
  8.                         BitmapFactory.decodeStream(in, null, decodeOptions);
  9.                 } catch (final IOException e) {
  10.                         Debug.e("Failed loading Bitmap in AssetBitmapTextureAtlasSource. AssetPath: " + pAssetPath, e);
  11.                 } finally {
  12.                         StreamUtils.close(in);
  13.                 }
  14.  
  15.                 return new AssetBitmapTextureAtlasSource(pAssetManager, pAssetPath, pTextureX, pTextureY, decodeOptions.outWidth, decodeOptions.outHeight);
  16.         }
  17.  
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


You can see an input stream is used. You will have to recreate this chain of methods, but simply use ByteArrayInputStream to achieve what you need.

You will most likely have to check how the bitmap is decoded as well and maybe use your own decoder :)
User avatar
smartus
 
Posts: 510
Joined: Fri May 10, 2013 4:17 pm

Re: How to use byte[] with AndEngine?!

Postby vanste25 » Tue Dec 03, 2013 2:02 pm

Thank you! I figured out how to use ByteArrayInputStream.
This is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 ByteArrayInputStreamOpener byteArrayInput = new ByteArrayInputStreamOpener(byteArray);
  2.                
  3.                 try {
  4.                         this.mTexture = new BitmapTexture(getTextureManager(), byteArrayInput);
  5.                 } catch (IOException e) {
  6.                         e.printStackTrace();
  7.                 }
  8.                 this.mTexture.load();
  9.                
  10.                 this.mTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
  11.                
  12.                 this.mSprite = new Sprite(x, y, w, h, this.mTextureRegion, this.getVertexBufferObjectManager());
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
vanste25
 
Posts: 2
Joined: Mon Dec 02, 2013 4:23 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 6 guests