zoukankan      html  css  js  c++  java
  • android平台 cocos2d-x 读取相册数据

    现已解决 方案如下:

    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;
        
    }
  • 相关阅读:
    js设计模式(一)---单例模式
    JavaScript---设计模式总结
    js设计模式---单例模式
    js设计模式---工厂模式
    react学习笔记01
    css常用布局
    js 继承
    vue路由动态加载
    vue 项目总结
    css文字两端对齐
  • 原文地址:https://www.cnblogs.com/mokey/p/3719467.html
Copyright © 2011-2022 走看看