如何将字节数组中的图像发送到从android到python的url

如何将字节数组中的图像发送到从android到python的url,android,python,arrays,url,flask,Android,Python,Arrays,Url,Flask,我不想使用url(即HTTPpost)作为json对象从android向python jupyter笔记本发送图像。我有一个flask代码,可以预测图像并返回该图像的标签,我还想将结果发送回android 我尝试先在位图中编码图像,然后将其编码到字节数组中,并将其作为字符串json对象发送。但我不知道如何在python中接收该图像 pyhton文件: from flask import Flask from flask import request app = Flas

我不想使用url(即HTTPpost)作为json对象从android向python jupyter笔记本发送图像。我有一个flask代码,可以预测图像并返回该图像的标签,我还想将结果发送回android

我尝试先在位图中编码图像,然后将其编码到字节数组中,并将其作为字符串json对象发送。但我不知道如何在python中接收该图像

pyhton文件:

    from flask import Flask
    from flask import request

    app = Flask(__name__)

    @app.route('/')
    def index():

        return "Welcome to Contact Less PALM Authentication"

    @app.route('/authenticate',methods = ['POST', 'GET'])
    def authenticate():
        #image_name = request.args.get('image_name')
        json_string=request.get_json()
        print("JSON String "+str(json_string))

        #path = test_path + "/"+image_name
        #img= image.load_img(path, target_size=image_size)
        #x = image.img_to_array(img)

        return "JSON String "+str(json_string) #+ predict_label(x)

        if __name__ == '__main__':
        app.run(host='0.0.0.0')
Android代码:

    private JSONObject buidJsonObject() throws JSONException {

            JSONObject jsonObject = new JSONObject();
                    Bitmap bitmap =((BitmapDrawable)user_img.getDrawable()).getBitmap();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageInByte = baos.toByteArray();
            String img_array = Base64.encodeToString(imageInByte, Base64.DEFAULT);
           // String img_array = new String(imageInByte);
            try {
                baos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            jsonObject.accumulate("image_Array",img_array);

            return jsonObject;
        }

用Android代码发送图像

编码图像后,通过POST请求发送图像。我不太懂java,但你可以看看

在Flask服务器中接收图像

收到post请求后,使用库中的函数
decode('base64')
。然后,您可以将映像保存在服务器上,例如

base64字符串仅包含文件的内容,而不包含元数据。如果要在服务器上显示文件名,则必须通过其他参数发送

import base64

@app.route('/authenticate',methods = ['POST', 'GET'])
def authenticate():
    encodedImg = request.form['file'] # 'file' is the name of the parameter you used to send the image

    imgdata = base64.b64decode(encodedImg)

    filename = 'image.jpg'  # choose a filename. You can send it via the request in an other variable
    with open(filename, 'wb') as f:
        f.write(imgdata)

用Android代码发送图像

编码图像后,通过POST请求发送图像。我不太懂java,但你可以看看

在Flask服务器中接收图像

收到post请求后,使用库中的函数
decode('base64')
。然后,您可以将映像保存在服务器上,例如

base64字符串仅包含文件的内容,而不包含元数据。如果要在服务器上显示文件名,则必须通过其他参数发送

import base64

@app.route('/authenticate',methods = ['POST', 'GET'])
def authenticate():
    encodedImg = request.form['file'] # 'file' is the name of the parameter you used to send the image

    imgdata = base64.b64decode(encodedImg)

    filename = 'image.jpg'  # choose a filename. You can send it via the request in an other variable
    with open(filename, 'wb') as f:
        f.write(imgdata)