標籤:android class code java tar ext
現已解決 方案如下:
1、使用 jni 調用 java 方法 啟動相簿選擇框 2、使用java將擷取的圖片儲存到本地 3、使用Cocos2d-x中 CCImage 讀取
JAVA代碼如下:
//啟動圖片選擇框 private void launchCamera() { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setType("image/*");//set intent type intent.setAction(Intent.ACTION_GET_CONTENT); //取得圖片資訊返回MainActivity startActivityForResult(intent,1); } //圖片選擇回調 protected void onActivityResult(int requestCode,int resultCode,Intent data) { if(resultCode==RESULT_OK) { Uri uri = data.getData(); //通過URI擷取圖片絕對位址 String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri,proj,null,null,null); int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); //遊標跳到首位,防止越界 cursor.moveToFirst(); String img_path = cursor.getString(actual_image_column_index); //通過地址獲得位元影像資訊 Bitmap bitmap =BitmapFactory.decodeFile(img_path); saveMyBitmap("001", bitmap); } }
//儲存圖片到本地 private void saveMyBitmap(String bitName,Bitmap mBitmap) { File f = new File("/sdcard/" + bitName + ".png"); try { f.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block } FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } }
C++代碼如下:
//讀取本機存放區資料 CCSprite* LoadingLayer::loadImage() { CCSprite* tempsprite = NULL; const char* path = "/sdcard/001.png"; FILE* fp = fopen(path, "rb"); if (!fp) { return tempsprite; } fseek(fp,0,SEEK_END); int len = ftell(fp); fseek(fp,0,SEEK_SET); char* buf = (char*)malloc(len); fread(buf,len,1,fp); fclose(fp); if(len==0 || buf==NULL) { return tempsprite; } CCImage* img = new CCImage; img->initWithImageData(buf,len); free(buf); cocos2d::CCTexture2D* texture = new cocos2d::CCTexture2D(); texture->initWithImage(img); img->release(); tempsprite = CCSprite::createWithTexture(texture); texture->release(); return tempsprite; } |