androidandengine圖片加密


androidandengine 是一款開源的2d遊戲引擎,功能還是強大的,但是用它寫遊戲還是有諸多不足之處。其中一個就是如題  資源的加密問題。對於一個公司或者個人來說,資源的保護非常的總要的。看來微雲有,u3d有。爲什麼我的andengine 就沒有。經過一段時間對andengine的架構解剖。嘿嘿。屌絲版加密就出來了。。下面我具體道來。

我用的是master 版的andengine Z這個是前提。

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


 

[java] view plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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
上一篇:java byte數組位運算 爲什麼 & 0xff
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章