Cocos2d-x and Android mixed encoding for "avatar pictures"

Source: Internet
Author: User

Recently, I took over the personalized features of players, but it is hard to handle this problem by replacing the player's "Avatar" operation. After searching, we finally found the solution.

If you change the player's "Avatar" operation, you can call the local images in the android servers service, select and crop the images, and crop the images to upload them to the server, this allows you to change user portraits;

Then, we need to use JniHelper to call classes and methods in andriod java to achieve mixed encoding to meet our various requirements for operating the android system, such:Obtain the MAC address of the Local Machine and the name of the Local MachineYou can even perform complex operations on the android background services;

To put it bluntly, we need to compile the android Activity class. The main function of this class is to call the android local image, select, crop, and crop the game and upload the image to the server. Let's take a look at my directory structure:

Ui of the userinfo. xml View File:

Android platform, game entry Cocos2dxActivity (GuideLayer), as follows:

Public class GuideLayer extends Cocos2dxActivity {protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} public Cocos2dxGLSurfaceView onCreateView () {Cocos2dxGLSurfaceView glSurfaceView = new response (this); // GuideLayer shocould create stencel buffer surglfaceview. setEGLConfigChooser (5, 6, 5, 0, 16, 8); return glSurfaceView;} static {System. loadLibrary ("cocos2dcpp");} // execute the android page Jump to jump to my function class UserInfoActivity. java; public static Object gotoActivity () {Intent in = new Intent (GuideLayer. getContext (), UserInfoActivity. class); GuideLayer. getContext (). startActivity (in); return null ;}}
This is a static class; the return type is Object, but I don't need to return the value, because I execute the page Jump, if my need is to get the value of the android module code, you can enter the corresponding return type to transfer values between java and c ++;

Handle Avatar change function: UserInfoActivity. java

Package; import java. io. byteArrayOutputStream; import java. io. file; import r; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. intent; import android. content. pm. activityInfo; import android. graphics. bitmap; import android. graphics. color; import android. graphics. drawable. bitmapDrawable; import androi uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. view. windowManager; import android. widget. editText; import android. widget. imageButton; import android. widget. imageView; import android. widget. linearLayout; import android. widget. textView; public CIA Ss UserInfoActivity extends Activity {private ImageButton user_nan, user_nv; private TextView ut_nan, ut_nv, TV _usernumber, region, region, Region; private LinearLayout lf; private ImageView user_headImage; private ImageButton save_bc; private static final int REQUEST_CODE_GETIMAGE_BYSDCARD = 0; // Request Code for obtaining the image from the SD card private static final int REQUEST_CODE_GETIMAGE_BYCAMERA = 1; // Request Code for obtaining the image from the camera priva Te String thisLarge = null, theSmall = null; private byte [] picture2; // user information private EditText editText1; private String nowsex = "male"; Thread td_into; protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); // form status settings-set to untitled title bar status [full screen] this. getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); r EquestWindowFeature (Window. FEATURE_NO_TITLE); // force the setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); setContentView (R. layout. userinfo); lf. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubCharSequence [] items = {"cell phone album", "cell phone photo ", "cancel"}; imageChooseItem (items) ;}});}/*** operation select ** @ param items */public void imageChooseI Tem (CharSequence [] items) {AlertDialog imageDialog = new AlertDialog. builder (this ). setTitle ("add image "). setItems (items, new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int item) {if (item = 0) {// select image Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. setType ("image/*"); startActivityForResult (intent, REQUEST_CODE_GETIMAGE_BYSDCARD);} // photograph else if (item = 1) {Intent intentC = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); intentC. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (new File (Environment. getExternalStorageDirectory (), "temp.jpg"); startActivityForResult (intentC, REQUEST_CODE_GETIMAGE_BYCAMERA );}}}). create (); imageDialog. show () ;}// get the image @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = REQU EST_CODE_GETIMAGE_BYSDCARD) {Bitmap bitm = null; if (data! = Null) {Uri selectedImage = data. getData (); if (selectedImage! = Null) {final Intent intent1 = new Intent ("com. android. camera. action. CROP "); intent1.setDataAndType (selectedImage," image/* "); intent1.putExtra (" crop "," true "); intent1.putExtra (" aspectX ", 1 ); intent1.putExtra ("aspectY", 1); intent1.putExtra ("outputX", 235); intent1.putExtra ("outputY", 235); intent1.putExtra ("return-data", true ); startActivityForResult (intent1, 4) ;}} if (bitm! = Null) {user_headImage. setImageDrawable (new BitmapDrawable (this. getResources (), bitm) ;}// take the image else if (requestCode = REQUEST_CODE_GETIMAGE_BYCAMERA) {if (resultCode! = RESULT_ OK) {return;} // set the File storage path. Put File picture = new File (Environment. getExternalStorageDirectory () + "/temp.jpg"); startPhotoZoom (Uri. fromFile (picture);} else if (requestCode = 4) {if (data! = Null) {Bitmap bmp = data. getParcelableExtra ("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream (); bmp. compress (Bitmap. compressFormat. JPEG, 75, stream); // picture2 = stream. toByteArray (); user_headImage.setImageDrawable (new BitmapDrawable (this. getResources (), bmp) ;}} else if (requestCode = 5) {Bundle extras = data. getExtras (); if (extras! = Null) {Bitmap bmp = data. getParcelableExtra ("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream (); bmp. compress (Bitmap. compressFormat. JPEG, 75, stream); // picture2 = stream. toByteArray (); user_headImage.setImageBitmap (bmp) ;}} public static final String IMAGE_UNSPECIFIED = "image/*"; public void startPhotoZoom (Uri uri Uri) {System. out. println (uri); Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, IMAGE_UNSPECIFIED); intent. putExtra ("crop", "true"); // aspectX aspectY is a width-high ratio intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); // outputX outputY is the width and height intent of the cropped image. putExtra ("outputX", 235); intent. putExtra ("outputY", 235); intent. putExtra ("return-data", true); startActivityForResult (intent, 5);} public void saveok () {this. runOnUiThread (new Runnable () {@ Overridepublic void run () {}});} public void saveno () {this. runOnUiThread (new Runnable () {@ Overridepublic void run (){}});}}

Because we call an android Avatar and need to use the camera function, we can also enable this function. Add the following code in AndroidManifest. xml:

There is also a class, the activity Configuration:

OK, go to the win32 platform:

In HelloWorldScene. cpp, introduce:

  #include "platform/android/jni/JniHelper.h"#include #if 1#define  LOG_TAG    "JniHelper"#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)#else#define  LOGD(...)#endif#endif
Code for triggering jump:
Void HelloWorld: menuCloseCallback (CCObject * pSender) {CCLog ("change the Avatar after a player clicks"); # if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) JniMethodInfo minfo; jobject; // The getStaticMethodInfo method calls the static class, or you do not need to call the getMethodInfobool B = JniHelper: getStaticMethodInfo (minfo, " guideLayer ", // class path" gotoActivity ", // static method name" () Ljava/lang/Object; "); // parameters are in brackets, the following is the return value. If (! B) {CCLog ("JniHelper: getMethodInfo error... ");} else {CCLog (" ready to invoke method... "); jobj = minfo. env-> CallStaticObjectMethod (minfo. classID, minfo. methodID) ;}# endif}
OK. The effect is as follows:

: Http:// Container id = 1525180823 & uk = 4097703620 & third = 15

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.