cocos2dx保存截图至相册并分享(android 微信)

本文介绍如何在Cocos2dx游戏中实现截图功能,并通过Java与C++交互将截图保存至相册,最后利用微信SDK进行图片分享至朋友圈或好友。详细步骤包括使用CCRenderTexture进行屏幕截图,调用Java接口保存图片,以及利用微信API分享图片。
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCRenderTexture *screen = CCRenderTexture::create(size.width, size.height);
CCScene *scene = CCDirector::sharedDirector()->getRunningScene();   //获取整个scene,如有其他需要可以百度一下cocos2dx截图方法
screen->begin();
scene->visit();//将当前的整个scene绘出来
screen->end();
bool tag = screen->saveToFile("MyCurScene.png", kCCImageFormatPNG);   //保存到系统路径(不是相册,下面有解释保存在哪了)
if (tag)
{
	TipsMgr::GetInstance()->SetCenterTip("get screen access!!!");
	std::string str = CCFileUtils::sharedFileUtils()->getWritablePath() + "MyCurScene.png";
	CallJava_ShareBitmap(2, SHARE_TYPE_COPYTONGGUAN, str);   //分享并保存到相册
}

上面截取的图片命名为"MyCurScene.png",保存在/data/user/0/com.wxshare.mm/files/MyCurScene.png,中间com.wxshare.mm是你的包名,其实也不用在意,是系统获取的路径,用的时候在获取一次就好了。

  • 接下来是c++与java交互
void CallJava_ShareBitmap(int type, int style, string path)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo minfo;
	bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/wxshare/mm/shareactivity","BitmapToShare","(IILjava/lang/String;)V");
	jobject jobj;

	if(isHave)
	{
		CCLOG(">> Call java BitmapToShare()");
		jint jType = type;
		jint jStyle = style;
		jstring jpath = minfo.env->NewStringUTF(path.c_str());
		minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID,jType,jStyle,jpath);
		CCLOG(">> Call java BitmapToShare() end");
	}
#endif
}
  • 上面调用java接口,在java中实现接口BitmapToShare,int type分享渠道类型, int style分享的是好友列表还是朋友圈/qq空间,String path图片路径
public static void BitmapToShare(int type, int style, String path)
{
	if(type == 1)  //qq
	{
		sharebmp = path;
		//待续
	}
	else if(type == 2)  //微信
	{
		sharebmp = path;
		Log.d("图片地址path =", path);
		saveImageToGallery(m_instance, path);  //保存到相册
		showBitmapToWeChat(type, style);     //分享到微信好友或者朋友圈
	}
}
  • 保存到系统相册并刷新相册列表
private static String buildTransaction(final String type) {
	return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
//保存文件到指定路径保存到相册
public static String saveImageToGallery(Context context, String bmpPath) {
	// 
	File imgFile = new File(bmpPath);
	if (!imgFile.exists()) {
		return "fail";
	}
	//插入图片到系统相册        
	try {
		MediaStore.Images.Media.insertImage(context.getContentResolver(), bmpPath, "分享图片", "妖神传分享图片");
		//保存图片后发送广播通知更新数据库
		Uri uri = Uri.parse(bmpPath);
		context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
		Log.d("savepath =", bmpPath);
		return "success";
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return "fail";
}
  • 分享图片到好友列表或者好友圈
public static void showBitmapToWeChat(int type, int style)
{
	shareFrom = style;
	//分享图片		
	Bitmap bmp = BitmapFactory.decodeResource(m_instance.getResources(), R.drawable.icon1);
	if(sharebmp!=null)
	{
		Log.d("图片地址sharebmp =", sharebmp);
		bmp = BitmapFactory.decodeFile(sharebmp);
	}
	WXImageObject imgObj = new WXImageObject(bmp);
	
	WXMediaMessage msg = new WXMediaMessage();
	msg.mediaObject = imgObj;
	
	int height = THUMB_SIZE * 2 / 3;
	Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, height, true);
	msg.setThumbImage(thumbBmp);
	//bmp.recycle();
	//msg.thumbData = Util.bmpToByteArray(thumbBmp, true);  // 设置缩略图
	
	SendMessageToWX.Req req = new SendMessageToWX.Req();
	req.transaction = buildTransaction("img");
	Log.d("图片名字 =", req.transaction);
	req.message = msg;
	req.scene = (type == 2) ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
	
	api.sendReq(req);
}
  • 分享文本到好友列表或者好友圈
//分享到朋友圈 、好友  标注 1:好友 2:朋友圈
public static void shareTextToWechat(int type, int style)
{
	shareType = type;
	shareFrom = style;

	try{
		HttpPost httpPost = new HttpPost(WX_SHARE_URL);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response = httpClient.execute(httpPost);
		showShareResult(response);
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值