0

I have successfully built a python server which even works but when java from android studio tries to connect to it fails with whole bunch of errors. I have understood that it fails while creating a new socket object but why that I don't know.

This is Java client, see at the end of the code particularly for the issue where I have created new Socket object:

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.TextView;


import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.ml.vision.FirebaseVision;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import com.google.firebase.ml.vision.text.FirebaseVisionText;
import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector;

import org.w3c.dom.Text;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.net.*;
import java.io.*;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button btnRecognize;
    private SpeechRecognizer speechRecognizer;
    static EditText ET_ShowRecognized;
    String locality;
    private Intent intent;
    private String ET_ShowRecognizedText;
    private String ProcessingText;
    //private FusedLocationProviderClient fusedLocationProviderClient;
    //Geocoder geocoder;
    Python py;
    PyObject pyobj;
    PyObject obj;
    String currentDate;
    String currentTime;
    static TextToSpeech tts;
    Uri imageURI;
    ContentValues contentValues;
    Intent cameraIntent;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    Image mediaImage;
    FirebaseVisionImage firebaseVisionImage;
    static Bitmap  imageBitmap;
    FirebaseVisionTextDetector textDetector;
    String imgText;
    Intent CameraIntent;
    static Thread sent;
    static Thread receive;
    static Socket socket;
    InputStreamReader in;
    BufferedReader bf;
    String ServerOutput;
    PrintWriter writer;
    String ServerInput;


    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility", "MissingPermission"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, CAMERA}, PackageManager.PERMISSION_GRANTED);


        ET_ShowRecognized = findViewById(R.id.ET_ShowRecognized);
        btnRecognize = findViewById(R.id.btnRecognize);
        /*fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {

                Location location = task.getResult();
                if(location != null){

                    geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
                    try {

                        List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                        locality = address.get(0).getLocality();

                    } catch (IOException e) {
                        ;
                    }
                }
            }
        });


        if(!Python.isStarted()){

            Python.start(new AndroidPlatform(this));

        }
        py = Python.getInstance();
        pyobj = py.getModule("WolframAlpha");
        obj = pyobj.callAttr("main", locality);*/

        tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i == TextToSpeech.SUCCESS) {

                    tts.setLanguage(Locale.ENGLISH);

                }
                tts.speak("Hi you successfully ran me.", TextToSpeech.QUEUE_FLUSH, null, null);
                tts.speak("Seems good to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);
            }
        });


        //currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
        //currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
        //textToSpeech.speak("Hi! I am your personal assistant. Today date is something something ", TextToSpeech.QUEUE_FLUSH, null, null);
        //Speak("Today's weather forecast for the current location is " + obj.toString());


        intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        speechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

            @Override
            public void onBufferReceived(byte[] bytes) {

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                ArrayList<String> mathches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

                if (mathches != null) {
                    ET_ShowRecognized.setText(mathches.get(0));
                    process();
                }
            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

            @Override
            public void onEvent(int i, Bundle bundle) {

            }
        });


        btnRecognize.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {

                    case MotionEvent.ACTION_UP:
                        speechRecognizer.stopListening();

                        break;

                    case MotionEvent.ACTION_DOWN:
                        ET_ShowRecognized.setText(null);
                        ET_ShowRecognized.setText("Listening...");
                        speechRecognizer.startListening(intent);
                        break;
                    default:
                        break;
                }

                return false;
            }
        });
    }
   

    public void process() {

        ProcessingText = ET_ShowRecognized.getText().toString().toLowerCase();

        if(ProcessingText.contains("hello")) {

            tts.speak("Hi! I hope all is well.", TextToSpeech.QUEUE_FLUSH, null, null);

        }
        else if(ProcessingText.contains("hi")){

            tts.speak("Hello! Nice to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);

        }
        else if(ProcessingText.contains("your name")){

            tts.speak("My name is assistant.", TextToSpeech.QUEUE_FLUSH, null, null);

        }
        else if(ProcessingText.contains("recognise text")){

            tts.speak("Opening Camera.", TextToSpeech.QUEUE_FLUSH, null, null);
            dispatchTakePictureIntent();

        }
        else if(ProcessingText.contains("bye")){

            finish();
            System.exit(0);

        }
        else if(ProcessingText.contains("current temperature")){

            /*try {
                socket = new Socket("192.168.43.203",12345);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            sent = new Thread(new Runnable(){

                @Override
                public void run() {

                    try {
                        bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        while(true){


                            ServerOutput = bf.readLine().toString();
                            MainActivity.tts.speak(ServerOutput, TextToSpeech.QUEUE_FLUSH, null, null);
                            MainActivity.ET_ShowRecognized.setText(ServerOutput);

                        }
                    }

                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });

            sent.start();
            try {
                sent.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/
            recieve_data();

        }else {

            tts.speak(ProcessingText, TextToSpeech.QUEUE_FLUSH, null, null);

        }
    }

    private void dispatchTakePictureIntent() {
        CameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try {
            startActivityForResult(CameraIntent, REQUEST_IMAGE_CAPTURE);
        } catch (ActivityNotFoundException e) {
            // display error state to the user
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");
            //imageView.setImageBitmap(imageBitmap);
            detectTextFromImage();
        }
    }

    private void detectTextFromImage() {

        firebaseVisionImage = FirebaseVisionImage.fromBitmap(imageBitmap);
        textDetector = FirebaseVision.getInstance().getVisionTextDetector();
        textDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
            @Override
            public void onSuccess(FirebaseVisionText firebaseVisionText) {

                //speakTextFromImage(firebaseVisionText);
                getImgText(firebaseVisionText);


            }
        }).addOnFailureListener(new OnFailureListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onFailure(@NonNull Exception e) {
                tts.speak("Something went wrong. Please try again later or try with another image.", TextToSpeech.QUEUE_FLUSH, null, null);
                ET_ShowRecognized.setText("Something went wrong. Please try again later or try with another image.");
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void getImgText(FirebaseVisionText firebaseVisionText){

        List<FirebaseVisionText.Block> blockList = firebaseVisionText.getBlocks();
        if(blockList.size() == 0) {

            tts.speak("I think this image contains no text.", TextToSpeech.QUEUE_FLUSH, null, null);
            ET_ShowRecognized.setText("I think this image contains no text.");

        }else{

            for(FirebaseVisionText.Block block : firebaseVisionText.getBlocks()){

                imgText = block.getText().toString();
                tts.speak("The text in the image is as follows : " + imgText, TextToSpeech.QUEUE_FLUSH, null, null);
                ET_ShowRecognized.setText("The text in the image is as follows : " + imgText);

            }
        }
    }

    public void recieve_data(){


        ServerInput = "Java client is successfully connected with the server ";
        BackgroundTask bt = new BackgroundTask();
            bt.execute(ServerInput);
    }

    class BackgroundTask extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... voids) {
            try{

                String message = voids[0];
                socket = new Socket("myIP", 24224);
                writer = new PrintWriter(socket.getOutputStream());
                writer.write(message);
                writer.flush();
                writer.close();
                socket.close();


            }catch (IOException e){
                e.printStackTrace();
            }

            return null;
        }
    }

    @Override
    protected void onPause() {

        super.onPause();
    }

    @Override
    protected void onResume() {

        super.onResume();
    }
}

This is my python server code:



import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket successfully created")

try:
    port = 24224
    s.bind(("", port))
    print("socket binded to %s" %(port))
except socket.error as err:
    print('Bind failed. Error Code : ' .format(err))
    
s.listen(10)

while True:

    conn, addr = s.accept()
    print('Got connection from', addr)
    message = conn.recv(1024)
    print("Client : " + message)
    conn.close()


Now the run view in the android studio:

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.maitreyastudio.ai, PID: 17690
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maitreyastudio.ai/com.maitreyastudio.ai.MainActivity}: android.os.NetworkOnMainThreadException
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
       at android.os.Handler.dispatchMessage(Handler.java:110)
       at android.os.Looper.loop(Looper.java:203)
       at android.app.ActivityThread.main(ActivityThread.java:6251)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
    Caused by: android.os.NetworkOnMainThreadException
       at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1318)
       at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:340)
       at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
       at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
       at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
       at java.net.Socket.connect(Socket.java:616)
       at java.net.Socket.connect(Socket.java:548)
       at java.net.Socket.<init>(Socket.java:440)
       at java.net.Socket.<init>(Socket.java:223)
       at com.maitreyastudio.ai.MainActivity.onCreate(MainActivity.java:127)
       at android.app.Activity.performCreate(Activity.java:6712)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789) 
       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 
       at android.os.Handler.dispatchMessage(Handler.java:110) 
       at android.os.Looper.loop(Looper.java:203) 
       at android.app.ActivityThread.main(ActivityThread.java:6251) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 

1 Answer 1

1

Looking at the stack trace I see that you are trying to connect on the ui thread which is causing the crash. You need to move the connection logic in to its own thread

Here is a link to the documentation that will help you https://developer.android.com/guide/components/processes-and-threads#Threads

try this for main activity

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.TextView;


import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.ml.vision.FirebaseVision;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import com.google.firebase.ml.vision.text.FirebaseVisionText;
import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector;

import org.w3c.dom.Text;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.net.*;
import java.io.*;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;



public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";
    private Button btnRecognize;
    private SpeechRecognizer speechRecognizer;
    static EditText ET_ShowRecognized;
    String locality;
    private Intent intent;
    private String ET_ShowRecognizedText;
    private String ProcessingText;
    //private FusedLocationProviderClient fusedLocationProviderClient;
    //Geocoder geocoder;
    Python py;
    PyObject pyobj;
    PyObject obj;
    String currentDate;
    String currentTime;
    static TextToSpeech tts;
    Uri imageURI;
    ContentValues contentValues;
    Intent cameraIntent;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    Image mediaImage;
    FirebaseVisionImage firebaseVisionImage;
    static Bitmap  imageBitmap;
    FirebaseVisionTextDetector textDetector;
    String imgText;
    Intent CameraIntent;
    static Thread sent;
    static Thread receive;
    static Socket socket;
    InputStreamReader in;
    BufferedReader bf;
    String ServerOutput;
    PrintWriter writer;
    String ServerInput;



    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility", "MissingPermission"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, CAMERA}, PackageManager.PERMISSION_GRANTED);


        ET_ShowRecognized = findViewById(R.id.ET_ShowRecognized);
        btnRecognize = findViewById(R.id.btnRecognize);








        /*fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {

                Location location = task.getResult();
                if(location != null){

                    geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
                    try {

                        List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                        locality = address.get(0).getLocality();

                    } catch (IOException e) {
                        ;
                    }

                }
            }
        });


        if(!Python.isStarted()){

            Python.start(new AndroidPlatform(this));

        }
        py = Python.getInstance();
        pyobj = py.getModule("WolframAlpha");
        obj = pyobj.callAttr("main", locality);*/

        tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i == TextToSpeech.SUCCESS) {

                    tts.setLanguage(Locale.ENGLISH);

                }
                tts.speak("Hi you successfully ran me.", TextToSpeech.QUEUE_FLUSH, null, null);
                tts.speak("Seems good to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);
            }
        });


        //currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
        //currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
        //textToSpeech.speak("Hi! I am your personal assistant. Today date is something something ", TextToSpeech.QUEUE_FLUSH, null, null);
        //Speak("Today's weather forecast for the current location is " + obj.toString());


        intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        speechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

            @Override
            public void onBufferReceived(byte[] bytes) {

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                ArrayList<String> mathches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

                if (mathches != null) {

                    ET_ShowRecognized.setText(mathches.get(0));
                    process();


                }
            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

            @Override
            public void onEvent(int i, Bundle bundle) {

            }
        });


        btnRecognize.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {

                    case MotionEvent.ACTION_UP:
                        speechRecognizer.stopListening();

                        break;

                    case MotionEvent.ACTION_DOWN:
                        ET_ShowRecognized.setText(null);
                        ET_ShowRecognized.setText("Listening...");
                        speechRecognizer.startListening(intent);
                        break;
                    default:
                        break;
                }

                return false;
            }
        });


    }

   

    public void process() {

        ProcessingText = ET_ShowRecognized.getText().toString().toLowerCase();

        if(ProcessingText.contains("hello")) {

            tts.speak("Hi! I hope all is well.", TextToSpeech.QUEUE_FLUSH, null, null);

        }
        else if(ProcessingText.contains("hi")){

            tts.speak("Hello! Nice to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);

        }
        else if(ProcessingText.contains("your name")){

            tts.speak("My name is assistant.", TextToSpeech.QUEUE_FLUSH, null, null);

        }
        else if(ProcessingText.contains("recognise text")){

            tts.speak("Opening Camera.", TextToSpeech.QUEUE_FLUSH, null, null);
            dispatchTakePictureIntent();

        }
        else if(ProcessingText.contains("bye")){

            finish();
            System.exit(0);

        }
        else if(ProcessingText.contains("current temperature")){

            sendTemp();
            recieve_data();

        }else {

            tts.speak(ProcessingText, TextToSpeech.QUEUE_FLUSH, null, null);

        }


    }

    private void dispatchTakePictureIntent() {
        CameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try {
            startActivityForResult(CameraIntent, REQUEST_IMAGE_CAPTURE);
        } catch (ActivityNotFoundException e) {
            // display error state to the user
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");
            //imageView.setImageBitmap(imageBitmap);
            detectTextFromImage();
        }

    }

    private void detectTextFromImage() {

        firebaseVisionImage = FirebaseVisionImage.fromBitmap(imageBitmap);
        textDetector = FirebaseVision.getInstance().getVisionTextDetector();
        textDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
            @Override
            public void onSuccess(FirebaseVisionText firebaseVisionText) {

                //speakTextFromImage(firebaseVisionText);
                getImgText(firebaseVisionText);


            }
        }).addOnFailureListener(new OnFailureListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onFailure(@NonNull Exception e) {
                tts.speak("Something went wrong. Please try again later or try with another image.", TextToSpeech.QUEUE_FLUSH, null, null);
                ET_ShowRecognized.setText("Something went wrong. Please try again later or try with another image.");
            }
        });



    }

    @SuppressLint("SetTextI18n")
    private void getImgText(FirebaseVisionText firebaseVisionText){

        List<FirebaseVisionText.Block> blockList = firebaseVisionText.getBlocks();
        if(blockList.size() == 0) {

            tts.speak("I think this image contains no text.", TextToSpeech.QUEUE_FLUSH, null, null);
            ET_ShowRecognized.setText("I think this image contains no text.");

        }else{

            for(FirebaseVisionText.Block block : firebaseVisionText.getBlocks()){

                imgText = block.getText().toString();
                tts.speak("The text in the image is as follows : " + imgText, TextToSpeech.QUEUE_FLUSH, null, null);
                ET_ShowRecognized.setText("The text in the image is as follows : " + imgText);


            }
        }



    }
    public void recieve_data(){


        ServerInput = "Java client is successfully connected with the server ";
        BackgroundTask bt = new BackgroundTask();
            bt.execute(ServerInput);

    }

    public void sendTemp(){
        new TempBackgroundTask().execute();
    }


    class TempBackgroundTask extends AsyncTask<Void, String, Void>{

        @Override
        protected Void doInBackground(String... voids) {
            try {
                socket = new Socket("myIP",12345);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            sent = new Thread(new Runnable(){

                @Override
                public void run() {

                    try {
                        bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        while(true){


                            ServerOutput = bf.readLine().toString();
                            publishProgress(ServerOutput);

                        }
                    }

                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });

            sent.start();
            try {
                sent.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void onProgressUpdate(String... text) {

          MainActivity.tts.speak(text[0], TextToSpeech.QUEUE_FLUSH, null, null);
            MainActivity.ET_ShowRecognized.setText(text[0]);
        }
    }

    class BackgroundTask extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... voids) {
            try{

                String message = voids[0];
                socket = new Socket("192.168.43.203", 24224);
                writer = new PrintWriter(socket.getOutputStream());
                writer.write(message);
                writer.flush();
                writer.close();
                socket.close();


            }catch (IOException e){
                e.printStackTrace();
            }

            return null;
        }
    }

    @Override
    protected void onPause() {

        super.onPause();
    }

    @Override
    protected void onResume() {


        super.onResume();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.