androidandengine图片加密


androidandengine 是一款开源的2d游戏引擎,功能还是强大的,但是用它写游戏还是有诸多不足之处。其中一个就是如题  资源的加密问题。对于一个公司或者个人来说,资源的保护非常的总要的。看来微云有,u3d有。为什么我的andengine 就没有。经过一段时间对andengine的架构解剖。嘿嘿。屌丝版加密就出来了。。下面我具体道来。

我用的是master 版的andengine Z这个是前提。

  1)在Engine.java 中添加如下代码


 

[java]  view plain copy
  1. //是否解码    
  2. private boolean isDecodeResource = false;     
  3. public boolean isDecodeResource() {  
  4.     return isDecodeResource;  
  5. }  
  6.   
  7. public void setDecodeResource(boolean isDecodeResource) {  
  8.     this.isDecodeResource = isDecodeResource;  
  9. }  


 2)在IGameInterface.java 中添加一个解码的方法.你 游戏的activity 必须实现它。


[java]  view plain copy
  1. public interface IGameInterface {  
  2.     // ===========================================================  
  3.     // Final Fields  
  4.     // ===========================================================  
  5.   
  6.     // ===========================================================  
  7.     // Methods  
  8.     // ===========================================================  
  9.   
  10.     public Engine onLoadEngine();  
  11.     public void onLoadResources();  
  12.     public void onUnloadResources();  
  13.     public Scene onLoadScene();  
  14.     public void onLoadComplete();  
  15.   
  16.     public void onPauseGame();  
  17.     public void onResumeGame();  
  18. //解码方法  
  19.     public InputStream decodeResource(InputStream in);  
  20. }  

 


3)改写AssetBitmapTextureAtlasSource.java 中的构造方法public AssetBitmapTextureAtlasSource(final Context pContext, final String pAssetPath, final int pTexturePositionX, final int pTexturePositionY)  和 public Bitmap onLoadBitmap(final Config pBitmapConfig) 这两方法

 


 

 


[java]  view plain copy
  1. public AssetBitmapTextureAtlasSource(final Context pContext, final String pAssetPath, final int pTexturePositionX, final int pTexturePositionY) {  
  2.     super(pTexturePositionX, pTexturePositionY);  
  3.     this.mContext = pContext;  
  4.     this.mAssetPath = pAssetPath;  
  5.   
  6.     final BitmapFactory.Options decodeOptions = new BitmapFactory.Options();  
  7.     decodeOptions.inJustDecodeBounds = true;  
  8.   
  9.     InputStream in = null;  
  10.     try {  
  11.         in = pContext.getAssets().open(pAssetPath);  
  12.   
  13.         final BaseGameActivity context = (BaseGameActivity)this.mContext;  
  14.         if(context.getEngine().isDecodeResource()){  
  15.             in = context.decodeResource(in);  
  16.         }  
  17.         BitmapFactory.decodeStream(in, null, decodeOptions);  
  18.     } catch (final IOException e) {  
  19.         Debug.e("Failed loading Bitmap in AssetBitmapTextureAtlasSource. AssetPath: " + pAssetPath, e);  
  20.     } finally {  
  21.         StreamUtils.close(in);  
  22.     }  
  23.   
  24.     this.mWidth = decodeOptions.outWidth;  
  25.     this.mHeight = decodeOptions.outHeight;  
  26. }  

[java]  view plain copy
  1. @Override  
  2. public Bitmap onLoadBitmap(final Config pBitmapConfig) {  
  3.     InputStream in = null;  
  4.     try {  
  5.         final BitmapFactory.Options decodeOptions = new BitmapFactory.Options();  
  6.         decodeOptions.inPreferredConfig = pBitmapConfig;  
  7.   
  8.         in = this.mContext.getAssets().open(this.mAssetPath);  
  9.           
  10.         final BaseGameActivity context = (BaseGameActivity)this.mContext;  
  11.         if(context.getEngine().isDecodeResource()){  
  12.             in = context.decodeResource(in);  
  13.         }  
  14.         return BitmapFactory.decodeStream(in, null, decodeOptions);  
  15.     } catch (final IOException e) {  
  16.         Debug.e("Failed loading Bitmap in " + this.getClass().getSimpleName() + ". AssetPath: " + this.mAssetPath, e);  
  17.         return null;  
  18.     } finally {  
  19.         StreamUtils.close(in);  
  20.     }  
  21. }  

 

为什么在这个类中加入呢。

1.构造方法中要知道图片的宽和高。

2.onLoadBitmap 被BitmapTextureAtlas 中的writeTextureToHardware(final GL10 pGL) 方法调用加入到底层opengl es 中去。所以在被生产为Bitmap 之前。我们把他/它给解码咯。。以便引擎调用。


4)生成新的jar 包

 

如果你保存你的android master 版的项目。在bin文件夹里会生成一个jar包。如果不放心是否是新的包,可以删除bin文件夹下的jar包。反正它会自动生成一个新的jar ^ ^ !

 

 

5)编码自己的资源文件

自己写个Java app 程序就搞定

[html]  view plain copy
  1. public static void encrImg(File src, File dest) throws Exception {  
  2.     ImageInputStream fis = new FileImageInputStream(src);  
  3.     ImageOutputStream fos = new FileImageOutputStream(dest);  
  4.       
  5.     int read;  
  6.     while ((read = fis.read()) > -1) {  
  7.         fos.write(read ^ XOR_CONST);  
  8.     }  
  9.     fos.flush();  
  10.     fos.close();  
  11.     fis.close();  
  12. }  
  13. public static final int XOR_CONST = 0X99; //密钥  

[java]  view plain copy
  1.     public static void main(String[] args) {  
  2. //      encrImg("E:\\项目\\TowerDefense\\图片\\人物\\3d图\\peasant1.png", "E:\\项目\\TowerDefense\\图片\\人物\\3d图\\peasant1c.png");  
  3. //      encrImg("E:\\项目\\TowerDefense\\图片\\人物\\3d图\\peasant1c.png", "E:\\项目\\TowerDefense\\图片\\人物\\3d图\\peasant12.png");  
  4.             }  


 

可参考这篇文章 :Android简单加密保护自有图片资源


 

6)游戏中解码 

[java]  view plain copy
  1. @Override  
  2. public InputStream decodeResource(InputStream in){  
  3.     List<Byte> list = new ArrayList<Byte>();  
  4.   
  5.     int read;  
  6.     byte[] arr = null;  
  7.     try {  
  8.         while ((read = in.read()) > -1) {  
  9.             read = read ^ XOR_CONST;  
  10.             list.add((byte) read);  
  11.         }  
  12.         arr = new byte[list.size()];  
  13.         int i = 0;  
  14.         for (Byte item : list) {  
  15.             arr[i++] = item;  
  16.         }     
  17.     } catch (Exception e) {  
  18.         // TODO: handle exception  
  19.     }  
  20.     InputStream iis = new ByteArrayInputStream(arr);  
  21.     return iis;  
  22. }  


主 activity 中重写 decodeResource(InputStream in) 和 onLoadEngine() 中设置可以编码

[java]  view plain copy
  1. @Override  
  2. public Engine onLoadEngine() {  
  3.     instance = this;  
  4.   
  5.     Engine engine = AndEnviroment.createEngine(ScreenOrientation.LANDSCAPE,  
  6.             truetrue);  
  7.     // Attempt to set up multitouch support  
  8.     try {  
  9.         if (MultiTouch.isSupported(this)) {  
  10.             engine.setTouchController(new MultiTouchController());  
  11.         }  
  12.     } catch (final MultiTouchException e) {  
  13.     }  
  14.   
  15.     engine.setDecodeResource(true);  
  16.       
  17.     return engine;  
  18. }  

engine.setDecodeResource(true); 

这句,一定要加上,不然会报找不到bitmap的错误
onLoadResources()方法中还是那样 直接用就可以了 是不是非常有用呢!

[java]  view plain copy
  1. BitmapTextureAtlas tex = new BitmapTextureAtlas(pTextureWidth,  
  2.         pTextureHeight, TextureOptions.NEAREST_PREMULTIPLYALPHA);  
  3. TextureRegion texReg = BitmapTextureAtlasTextureRegionFactory  
  4.         .createFromAsset(tex, AndEnviroment.getInstance().getContext(),  
  5.                  pName, 00);  
  6. AndEnviroment.getInstance().getEngine().getTextureManager()  
  7.         .loadTexture(tex);  


以上的解码方式适合放在assets 文件下的图片资源。

 

 

 

 要修改其他加载方式的请看下图 大同小异啦。。

1.res 目录下的

类ResourceBitmaXXXXXXXSource.java  FileBitmapXXXXXSource.java 中的 onLoadBitmap 方法改写

 

2.texturepacker  扩展

TexturePackParser.java 中  parsetexture  方法中onGetInputStream() 方法改写

更多 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值