从Android中的烧瓶中检索图像

时间:2020-07-08 16:25:40

标签: python android flask

因此,我尝试编写代码以使用0.0.0.0从烧瓶服务器上的Android Studio发送img,处理图像,然后将图像处理结果发送回。我已经完成了将图像从android发送到python-flask并在flask中运行我的算法的工作。该算法大约需要3分钟的处理时间。

我在将图像结果检索到android时遇到一些问题。我已经在flask中使用base64对图像进行了编码,我尝试并成功。但是当我想检索图像并显示到imageView时,应用程序突然终止

这是postRequest方法

public void postRequest(String postUrl, RequestBody postBody) {
        OkHttpClient client = new OkHttpClient()
                .newBuilder()
                .connectTimeout(200, TimeUnit.SECONDS)
                .writeTimeout(200,TimeUnit.SECONDS)
                .readTimeout(400,TimeUnit.SECONDS)
                .build();

        Request request = new Request.Builder()
                .url(postUrl)
                .post(postBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                // Cancel the post on failure.
                call.cancel();
                // In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView responseText = findViewById(R.id.responseText);
                        responseText.setText("Failed to Connect to Server");
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                // In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView responseText = findViewById(R.id.responseText);
                        ImageView responseImg = findViewById(R.id.image_view2);

                        ResponseBody in = response.body();
                        InputStream inputStream = in.byteStream();
                        Log.i("inputStream","inputstream value = "+inputStream);
                   
                        byte[] decodedString = Base64.decode(String.valueOf(inputStream), Base64.URL_SAFE);
                        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                        Log.i("bitmap","bitmap value = "+decodedByte);

                        TextView pengenalan = findViewById(R.id.pengenalan);
                        pengenalan.setText("HASIL PENGENALAN CITRA");
                        MediaStore.Images.Media.insertImage(getContentResolver(), decodedByte, "test.jpg" , "none");
                        responseImg.setImageBitmap(decodedByte);
                    }
                });
            }
        });
    }

当我尝试仅返回文本时,它会成功。但是,当我尝试运行代码以从烧瓶返回图像时。它说base64不好

这是我的代码,用于在烧瓶中编码图像,我已经在浏览器中单独尝试过,成功了

app = Flask(__name__, static_url_path='/')

@app.route('/', methods = ['GET', 'POST'])
def handle_request():
    imagefile = flask.request.files['image']
    filename = werkzeug.utils.secure_filename(imagefile.filename)
    print("\nReceived image File name : " + imagefile.filename)
    imagefile.save(filename)
    #return "Image Uploaded Successfully"
    
    imageresult = omr2("D:/Data/kuliah/TA-doc/Code/FinalCopy/androidFlask.jpg")
    path = r'D:\Data\kuliah\TA-doc\Code\FinalCopy'
    data_uri = base64.b64encode(open('detected.png', 'rb').read()).decode('utf-8') 

    return jsonify(data_uri)

#if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)

这是错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.atna.capturenotation, PID: 5929
    java.lang.IllegalArgumentException: bad base-64
        at android.util.Base64.decode(Base64.java:161)
        at android.util.Base64.decode(Base64.java:136)
        at android.util.Base64.decode(Base64.java:118)
        at com.atna.capturenotation.Main3Activity$1$2.run(Main3Activity.java:136)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7266)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

请帮助我解决这个问题

0 个答案:

没有答案