- 在进行微信分享操作之前先接入SDK
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/Android.html - 在游戏内进行截图,实际上cocos2dx截图之后还要在java中将其保存在用户可见目录也就是相册
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();
}
}
本文介绍如何在Cocos2dx游戏中实现截图功能,并通过Java与C++交互将截图保存至相册,最后利用微信SDK进行图片分享至朋友圈或好友。详细步骤包括使用CCRenderTexture进行屏幕截图,调用Java接口保存图片,以及利用微信API分享图片。
&spm=1001.2101.3001.5002&articleId=103477990&d=1&t=3&u=775eec88829d49e781f450646cf4a512)
338

被折叠的 条评论
为什么被折叠?



