Android平台Cocos2d-x读取相册图片信息。
来源:互联网 发布:java课设心得体会 编辑:程序博客网 时间:2024/06/05 06:35
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)
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;
}
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)
{
//这里路径可以使用 FileUtils::getInstance()->getWritablePath()来获取
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;
}
0 0
- Android平台Cocos2d-x读取相册图片信息。
- android平台 cocos2d-x 读取相册数据
- cocos2d-x 保存图片到相册
- Cocos2d-x 3.x 头像选择器,本地相册与拍照+头像编辑功能(Android、IOS双平台实现)
- cocos2d-x Android平台移植
- cocos2d-x调用摄像头和相册并裁减图片
- Cocos2d-x 读取网络图片创建精灵
- cocos2d-x读取ZIP中的图片名称
- Cocos2d-x 读取网络图片创建精灵
- Cocos2d-x 读取网络图片创建精灵
- Android 调用系统相机拍照(适配7.0)、从图库选择图片;从数据库读取联系人信息、相册图片
- 【cocos2d-x 3.0】瞬间学会在Android平台读取SD卡数据包
- Android 读取图片方向信息
- Android手机读取相册图片,根据URI获取图片路径
- cocos2d-x交叉编译到android平台
- Cocos2d-x Android平台工程创建+Cygwin
- cocos2d-x移植到android平台下
- Cocos2d-x Android平台编程环境搭建
- 在Android程序中调用摄像头功能
- max系统的FTP简单命令
- 行业网站参考
- Linux环境下qt-everywhere-opensource-src-4.8.6配置
- ios tableview 那些事(二十六)UITableViewController 多选操作
- Android平台Cocos2d-x读取相册图片信息。
- jquery - 左边栏菜单隐藏/显示
- 我使用过的Linux命令之join - 根据关键字合并数据文件
- iOS平台添加Google Admob -1/2(Unity3D开发之七)
- R: anova或linear model 的 MAPE计算
- cocos2d开发学习一:开发环境搭建以及入门学习资源
- POJ 1836 Alignment
- freematics, arduino(open source hardware) + telematics for vehicle obd
- Hibernate_14_数据连接池的使用