Screenshot Upload to REST webserver

E

ericbunese

Guest
Hello everyone.
I've found an obstacle in my path and I have not been able to pass it.

I'm working as a programmer on a mobile game (android, iOS) and I must create a subsystem to upload screenshots to the cloud. Since I'll be doing the webserver, the way I send the images is not a problem and I have decided to use one of the following options:

1) Receive data as multipart/form data with the whole file and save it to the server's hard drive
2) Receive the base64 encoded string of the file's content as part of an application/json data, decode it and save the file to the hard drive.

Since game maker does not have proper functions for dealing with image files other than sprites/backgrounds handling and I do not fully understand how could I solve my problem using buffers, I decided to write a short android extension to serialize a file into a base64 string. I knew this was probably the slowets performance-wise solution but I wanted to give it a try. Basically; I made an async function to return me a string whenever the file was encoded and it takes forever. (It is very possible that I made a major mistake, since I'm not familiarized with android, and followed some simple stackoverflow guides)

Anyway, here's the code to my extension: (I am almost sure that I have imported some useless libraries as I had no time to fix the code, I was only trying to serialize a file - I succeeded but it's unusable)

Code:
package ${YYAndroidPackageName};

import android.util.Log;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.String;
import android.util.Base64;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import android.os.Environment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import ${YYAndroidPackageName}.R;
import com.yoyogames.runner.RunnerJNILib;
import ${YYAndroidPackageName}.R;
import com.yoyogames.runner.RunnerJNILib;

public class ImageSerializer
{
    private static final int EVENT_OTHER_SOCIAL = 70;
    public void serializeImageAsync(String filename)
    {
        Log.i("yoyo", "Started Encoding");
        int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
        String path = Environment.getExternalStorageDirectory().toString()+"/";
        filename = path+filename;
        try
        {
            //png to bitmap
            Bitmap bitmap = BitmapFactory.decodeFile(filename);
            //bitmap to byte array
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream .toByteArray();
            //byte array to string
            String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
            //String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

            //return as async
            RunnerJNILib.DsMapAddString( dsMapIndex, "type", "finished");
            RunnerJNILib.DsMapAddString( dsMapIndex, "string", encoded);
            RunnerJNILib.DsMapAddString( dsMapIndex, "filename", filename);
            RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            //return as async
            RunnerJNILib.DsMapAddString( dsMapIndex, "type", "failed");
            RunnerJNILib.DsMapAddString( dsMapIndex, "string", "");
            RunnerJNILib.DsMapAddString( dsMapIndex, "filename", filename);
            RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
        }
    }
}
The other possibility would be sending the file as multipart/form data but I'm not sure how to do this with gamemaker's REST client functions. So I'm guessing I'd have to write an extension to send a different type of request. The problem now is that I have no idea how to locate these files from game maker and pass them to this extension. Since gamemaker's storage is sandboxed. And the second problem would be porting that to iOS.

If anybody is willing/capable to help me please contact me.

Thank you for the attention.

Eric.
 
E

ericbunese

Guest
Actually I've come up with a solution to send the data as base64 using buffers, which was actually much easier than writing an extension to do so.
I still recommend resizing the image down to minimize data transfer.
I'll try to look up the first solution to send the file as is, since base64 is extremely long.

Code:
var buf = buffer_load("image.png");
var sz = buffer_get_size(buf);

show_debug_message(buffer_base64_encode(buf, 0, sz))
buffer_delete(buf)
 
Top