0

I would like to implement a program to perform image recognition. If the image is processing locally, it sends to another computer to process, I set the Thread.sleep(1000) and expect the 1st image is processed locally and other will send out before the localprocessing variable set to false.

private class ProcessImageTask extends AsyncTask<ImageItem, Void, ImageItem>{
    @Override
    protected ImageItem doInBackground(ImageItem... params) {
        if(localProcessing==false){
            //**************processing locally*****************
            localProcessing = true;
            try {
                Bitmap bm = BitmapFactory.decodeFile(params[0].getBitmap());

                Bitmap croppedBitmap = getBitmap(getApplicationContext(), INPUT_SIZE, bm);
                final List<Classifier.Recognition> results = classifier.recognizeImage(croppedBitmap);

                String resultStr = results.toString();
                String trimResult = resultStr.substring(resultStr.indexOf("[")+1,resultStr.indexOf("]")).trim();

                String localId = params[0].getId();
                trimResult = trimResult.substring(0,trimResult.indexOf(")")) + " likely)";

                Bitmap thumbnail = getBitmap(getApplicationContext(), 50, bm);
                ImageItem tmp = new ImageItem(localId, imgToString(thumbnail), trimResult);

                Thread.currentThread();
                Thread.sleep(1000);
                localProcessing = false;
                return tmp;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            //****************processing on server*************************
            try {
                String ip = strIP;
                int port = 8195;
                Bitmap bm = BitmapFactory.decodeFile(params[0].getBitmap());
                Bitmap croppedBitmap = getBitmap(getApplicationContext(), INPUT_SIZE, bm);
                String encodedImage = "/ID-BEGIN/" + ID + "/ID-END" + imgToString(croppedBitmap);

                try {
                    //**********Send request to server*********
                    Socket socket = new Socket(ip,port);

                    DataInputStream dis = new DataInputStream(socket.getInputStream());
                    DataOutputStream dout = new DataOutputStream(socket.getOutputStream());

                    byte [] messageToServer = encodedImage.getBytes();
                    dout.writeInt(messageToServer.length);
                    dout.write(messageToServer);

                    //Receive response from server
                    int length = dis.readInt();

                    if(length>0) {
                        byte [] message = new byte[length];
                        dis.readFully(message, 0, message.length);

                        String response = new String(message);
                        //Handler updateHandler.post(new updateUIThread(response));

                        Bitmap thumbnail = getBitmap(getApplicationContext(), 50, bm);
                        ImageItem tmp = new ImageItem(params[0].getId(),imgToString(thumbnail), extractServerMessage(response)+"@@");
                        return tmp;
                    }
                    socket.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPostExecute(ImageItem imageItem) {
        super.onPostExecute(imageItem);
    }
}

I realize it is working sequentially, could you please tell me how should I fix and make the different task work as same time even I set the Thread.sleep.

Thank you so much.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
J Joe
  • 1
  • 1

1 Answers1

1

You probably want to execute all Asynctasks at the same time in ThreadPool

asyncTask1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ....);

AsyncTask.THREAD_POOL_EXECUTOR is a constant Executor. If you're not satisfied on AsyncTask's Thread pool. You can create your own thread pool by doing this.

Executor myThreadPool = Executors.newFixedThreadPool(numOfPools);

Usually, numOfPools is taken from available processors by Runtime.getRuntime().availableProcessors().

It is still up to you how many threads you want. Be aware of battery use. More computation power, more battery. Just a reminder.

Glenn
  • 12,741
  • 6
  • 47
  • 48